将触摸事件传递给父视图 [英] Passing touch events to the parent view
问题描述
我有一个自定义的 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.
这篇关于将触摸事件传递给父视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!