Android - Why does onItemLongClick(…) return a boolean?

穿精又带淫゛_ 提交于 2019-12-04 15:36:05

问题


Coming from a Java background I am used to dealing with actions though I'm not really sure why the method requires a boolean is returned and don't fully understand the explanation given on the site: true if the callback consumed the long click, false otherwise.


回答1:


As you may know, the View hierarchy in Android is represented by a tree. When you return true from the onItemLongClick() - it means that the View that currently received the event is the true event receiver and the event should not be propagated to the other Views in the tree; when you return false - you let the event be passed to the other Views that may consume it. Hope this helps.




回答2:


I will further clarify this for you, by way of an example.

@Override
public boolean onLongClick(View view) {

//Do all you stuff here    

return true; // or you can return false;
}
  • return true means: that the event has been handled. No events will be fired after this point.
  • return false means: the event has NOT been handled. Any other events to do with this click will still fire.

So, after your onLongClick() has fired, if you don't want the regular onClick() to fire, then just return true from the onLongClick() event.



来源:https://stackoverflow.com/questions/12230469/android-why-does-onitemlongclick-return-a-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!