Reputation: 1479
For implementation of onClick function which approach is better?
And why?
Upvotes: 0
Views: 130
Reputation: 29436
Depends on how many kind of touch events you want to support:
on Up : click
on Up : without moving much - > click , moved -> swipe
on Up : short duration - > click , long duration -> long press has been triggered, ignore.
on Up and long press triggered : without moving much - > ignore , moved -> drag n drop
You go into details of duration and displacement , when you really need more kinds of touch events.Best approach depends on scenario. So, if your touchscreen doesn't have a notion of swipe or long press or drag n drop, you might just fire a click on every up event, simplest scenario.
Upvotes: 1
Reputation: 39846
If you absolutely must implement your own, I would use the option 2.
if(motionEvent==MotionEvent.ACTION_UP){
long duration = motionEvent.getDownTime() - .getEventTime();
if(duration < THRESHOULD)
click();
}
Upvotes: 1