Returning true and false in OnTouch?
Asked Answered
L

1

15

Does it matter if i return true or false in onTouch() of an OnTouchListener? I can't see any difference between returning true or false in this example: Android Swipe on List

Libyan answered 18/6, 2014 at 13:46 Comment(0)
G
35

The return value determines if you consumed the touch event.

In other words true means that this touch event is interesting to you and all follow up calls of this touch event like ACTION_MOVE or ACTION_UP will be delivered to you.

If you return false than the touch event will be passed to the next View further up in the view hierarchy and you will receive no follow up calls. The touch event will continue to be passed further up the view hierarchy until someone consumes it.

If you have any further questions please feel free to ask!

Garmon answered 18/6, 2014 at 13:50 Comment(6)
Ok thats explains it very well but let me be more specific... I was asking on the provided link, the answer returns false in some cases (on the mouse_down for example)... What i notice if i used simillar coding is that the onTouch captures only the down then doesnt capture anything else (action_move or action_up) because it already returned falseLibyan
Yes, as I explained in my answer if you return false you get the initial events and not the follow up events, sometimes you need to do it this way if for example you want to do something on ACTION_DOWN but don't want to consume the touch event because some other View depends on it.Garmon
In the answer you link to notes there is a comment in the code which states that the reason for returning false is that he wants the View to still be able to process click events. If he returned true there the touch event would be consumed by his touch handler and the View would never receive the touch event and as such wouldn't be clickable.Garmon
But yes, your observation is true, the code in the answer you are linking to wouldn't work. The OnTouchListener would never receive ACTION_UP after returning false on ACTION_DOWN.Garmon
We're all going on the same line till now.... then why exactly are you using a <b>return false</b> when it will stop the onTouch everytime after the ACTION_DOWN @XaverLibyan
I explained it twice now, read my comments, read my answer, it's all there.Garmon

© 2022 - 2024 — McMap. All rights reserved.