简体   繁体   English

Android - 为什么onItemLongClick(...)返回一个布尔值?

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

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

As you may know, the View hierarchy in Android is represented by a tree. 您可能知道,Android中的View层次结构由树表示。 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; 当您从onItemLongClick()返回true - 这意味着当前接收到该事件的View是真正的事件接收者,并且该事件不应传播到树中的其他Views ; when you return false - you let the event be passed to the other Views that may consume it. 当您返回false - 您将事件传递给可能使用它的其他Views 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. return true表示:事件已被处理。 No events will be fired after this point. 此后不会触发任何事件。
  • return false means: the event has NOT been handled. return false表示:事件尚未处理。 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. 因此,在你的onLongClick()被触发之后,如果你不希望触发常规onClick() ,那么只需从onLongClick()事件返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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