简体   繁体   中英

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

来自Java背景我习惯于处理动作虽然我不确定为什么该方法需要返回布尔值并且不完全理解网站上给出的解释:如果回调消耗了长按,则为true,false除此以外。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM