Android: Why does a long click also cause a normal click?

I have a ListView with listeners for a long click and a regular click.

Why, when I click on a list item for a long time, does the regular click event fire as well?

I need to have two separate functions for different clicks.

+82
android events listview onlongclicklistener
Mar 25 2018-11-11T00:
source share
3 answers

From Event Listeners :

onLongClick () - This returns a boolean value indicating whether you used the event and cannot be carried forward. That is, return true to indicate that you have processed the event, and it should stop here; return false if you did not process it, and / or the event should continue until any other listener.

Are you returning true from your onLongClick() and still getting a normal click event?

Edited to add . For ListView you can use OnItemLongClickListener . onItemLongClick() uses a similar boolean value to indicate whether it consumed an event.

+206
Mar 25 2018-11-11T00:
source share

Make sure you override OnClickListener for your onClick method. Also make sure that you override OnLongClickListener for your onLongClick method. And make sure your onLongClick method returns true , as this will consume onClick .

+13
Jun 26 2018-12-12T00:
source share

Repeating the answer is easier:

Given:

 @Override public boolean onLongClick(View view) { return true; // or false } 
  • return true means the event is destroyed. It is being processed. No other click events will be notified.
  • return false means the event is not being consumed. Any other click events will continue to receive notifications.

So, if you do not want onClick also run after onLongClick , then you should return true from the onLongClick event.

+11
Nov 29 '16 at 6:50
source share



All Articles