简体   繁体   中英

Handle click events when parent view has a touch listener

I have a ListView where users can swipe and dismiss items. To implement swipe dismiss, I have set a touch listener to the list view.

I want to handle clicks on each item in the list view too. But, if I set an on click listener to the items, swipe dismiss doesn't work. This should be due to each item handling touch events and not passing them to conainer ListView.

Can someone suggest a way which I can intercept click events on items without disrupting swipe dismiss?

The GestureDetector is attached to the ListView via an OnTouchListener, and the OnClickListener is set on the individual list items--which are child views of the ListView


The problem here is how Listview, and all ViewGroup, handles touch order.
A ViewGroup delivers touch events to its child views and then handles it by itseld directly if and only if no child views consume the event . A child view with an OnClickListener will always consume touch events and therefore your GestureDetector will not see any event ever.
The easiest and fastest solution it to use one single GestureDector to rull them all (LOTR fans will get the joke hahah) and handle swipe and click. And set each item in the listview as consumer and not the listview itself.

Have a look here for another valid solution

如果用户未达到特定的移动阈值,则可以在ACTION_UP上使用listView.performClick()

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