简体   繁体   English

当父视图具有触摸侦听器时处理单击事件

[英]Handle click events when parent view has a touch listener

I have a ListView where users can swipe and dismiss items. 我有一个ListView,用户可以在其中滑动和关闭项目。 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. 这应归因于每个项目都处理触摸事件,而不是将其传递给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 GestureDetector通过OnTouchListener附加到ListView,并且OnClickListener设置在各个列表项上,这些列表项是ListView的子视图


The problem here is how Listview, and all ViewGroup, handles touch order. 这里的问题是Listview和所有ViewGroup如何处理触摸顺序。
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 . 一个ViewGroup将触摸事件传递到它的子视图,然后当并且仅当没有任何子视图占用该事件时 ,由ViewGroup直接处理它。 A child view with an OnClickListener will always consume touch events and therefore your GestureDetector will not see any event ever. 具有OnClickListener的子视图将始终使用触摸事件,因此您的GestureDetector将永远不会看到任何事件。
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. 最简单,最快的解决方案是使用单个GestureDector将它们全部搅碎 (LOTR粉丝会开玩笑哈哈)并处理滑动和点击。 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()

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

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