1

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?

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179

2 Answers2

1

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

Community
  • 1
  • 1
Sid
  • 14,176
  • 7
  • 40
  • 48
0

You can use listView.performClick() on the ACTION_UP if the user has not met a certain threshold of movement.

Taylor Kline
  • 908
  • 3
  • 9
  • 30