手势检测器不适用于可滚动列表活动
我有一个简单的应用程序,有两个视图,一个是 TableView,另一个是 ListView。我使用 GestureDetector 来检测屏幕上的滑动,类似于此处< /a>.如果列表视图仅填充了几个项目,则一切正常,但是当 ListView 填满整个屏幕时,手势检测将停止工作。在屏幕上滑动只会突出显示列表项之一。
我认为发生这种情况是因为 ListView 以某种方式从 GestureListener 窃取了触摸事件。有办法防止这种情况吗?
I have a simple application with two views, one is a TableView and the other is ListView. I use GestureDetector to detect the swipes across the screen similarly to how it is done here. Everything works OK, if the list view is populated with just a few items, however when the ListView fills up the whole screen the gesture detection stops working. Doing the swipe across the screen simply shows highlights one of the list items.
I think this is happening because ListView somehow steals the touch events from the GestureListener. Is there a way to prevent this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现,如果您遍历相当准确的水平路径,GestureDetector 在 ListItems 中工作得很好。但是,如果您稍微偏离,列表就会滚动并且手势不会完成。发生的事情如下:
我需要我的应用程序表现得好像水平滑动仍在继续,即使触摸稍微偏离水平方向也是如此。
解决方案:
这涉及 ViewGroup.requestDisallowInterceptTouchEvent (boolean disallowIntercept),它阻止父级查看运动事件。该方法涉及实现 onTouchListener 来检测轻微的滑动(10 像素左右),然后停止父拦截运动事件。然后,父级将不会滚动,手势检测器将继续完成。
这是代码:
I found that GestureDetector works fine in ListItems if you traverse a fairly accurate horizontal path. However if you stray slightly, the list scrolls and the gesture does not complete. What is going on is as follows:
I needed my app to behave as if the horizontal swipe continued even if the touch strays from horizontal slightly.
SOLUTION:
This involves ViewGroup.requestDisallowInterceptTouchEvent (boolean disallowIntercept) which stops the parent being able to peek at the motion events. The method involves implementing onTouchListener to detect a slight swipe (10 pixels or so) then stopping the parent intercepting motion events. The parent will then not scroll and the gesture detector continues to completion.
Here's the code:
您可以创建一个自定义列表视图,然后在该列表的每一行上实现手势检测器。可能值得一试。
You could create a custom listview and then implement the gesture detector inside of this i.e. on each row of the list. Could be worth a try.