Today, when doing demo, we need to set the long press and click events of the ListView item, Onitemlongclicklistener and Onitemclicklistener, however, the Click event can be implemented, and the long time operation will trigger the Click event simultaneously. Then look at the relevant source of Android, you can see the system is the priority of the corresponding Clicklisener, he is no return value. For Onitemlongclicklistener, there is a return value identifier. For a long press operation, if False is returned, 2 Lisener are appropriate, and if true, the system only handles long-press events.
Therefore, if you want your onitemlongclicklistener to take effect as long as the event is, you need to set the return value to true.
Here is the source code
/** * Interface definition for a callback to is invoked when an item in this * Adapterview have been clicked. */Public interface Onitemclicklistener { /** * Callback method to is invoked when a item in this Adapterview Has * been clicked. * <p> * Implementers can call Getitematposition (position) if they need * To access the data associated with The selected item. * * @param parent The Adapterview where the click happened. * @param view the view within the Adapterview that is clicked (this * 'll be A view provided by the adapter)
* @param position the position of the view in the adapter. * @param ID of the row ID of the item that is clicked. * /void Onitemclick (adapterview<?> parent, view view, int position, long ID); }
/** * Interface definition for a callback to being invoked when an item in this * view has been clicked and held.
*/Public interface Onitemlongclicklistener { /** * Callback method to being invoked when a item in this view H As been * clicked and held. * * Implementers can call Getitematposition (position) if they need to access * The data associated with the Selec Ted Item. * * @param parent The Abslistview where the click happened * @param view the view within the Abslistview that WA S clicked * @param position the position of the view in the list * @param ID of the row ID of the item that is CLI cked * Source has been explained here, if the return value is set to True, then the system consumes the long press event * @return True if the callback consumed the long click, False otherwise * /Boolean Onitemlongclick (adapterview<?> parent, view view, int position, long ID); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Long press event and click event Problem handling, Onitemlongclicklistener and Onitemclicklistener