将触摸事件传递给父视图

编程入门 行业动态 更新时间:2024-10-03 08:28:31
本文介绍了将触摸事件传递给父视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个自定义的 ViewSwitcher,我在其中实现了触摸事件,所以我可以使用触摸屏滚动屏幕.

I have a custom ViewSwitcher in which I implemented touch events so I am able to scroll through screens using the touchscreen.

我的布局层次结构如下所示:

My layout hierarchy looks like this:

<ViewSwitcher> <LinearLayout> <ListView /> </LinearLayout> <LinearLayout> <ListView /> </LinearLayout> </ViewSwitcher>

现在,问题是触摸事件正在被ListViews 并且我无法切换视图.当我使用它时它工作正常没有 ListViews.我需要能够滚动浏览查看并滚动 ListView.

Now, the problem is that the touch events are being consumed by the ListViews and I am not able to switch the views. It works fine when I don't have the ListViews. I need to be able to scroll through the views and scroll the ListView.

我该如何解决这个问题?

How do I solve this?

EDIT:我还需要 ListView 项可以点击.

EDIT: I also need the ListView items to be clickable.

提前致谢!

推荐答案

感谢大家的回答.但我能够以更简单的方式弄清楚它.由于我的 ViewSwitcher 没有检测到触摸事件,我拦截了触摸事件,调用了 onTouchEvent() 并返回了 false.这里:

Thank you everyone for answering the question. But I was able to figure it out in a much simpler manner. Since my ViewSwitcher wasn't detecting the touch event, I intercepted the touch event, called the onTouchEvent() and returned false. Here:

@Override public boolean onInterceptTouchEvent(MotionEvent ev) { onTouchEvent(ev); return false; }

通过覆盖onInterceptTouchEvent(),我能够拦截活动中的触摸事件.然后我在 ViewSwitcher 中调用了 onTouchEvent() 来处理 ListViews 的切换.最后通过返回 false,确保 ViewGroup 不会消耗该事件.

By overriding the onInterceptTouchEvent(), I was able to intercept the touch event in the activity. Then I called the onTouchEvent() in the ViewSwitcher which handles the switching of the ListViews. And finally by returning false, it makes sure that the ViewGroup doesn't consume the event.

更多推荐

将触摸事件传递给父视图

本文发布于:2023-11-03 04:27:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!