由家长抓住onTouch事件,处理它,并传给孩子 [英] Catch onTouch event by parent, handle it and pass to children

查看:104
本文介绍了由家长抓住onTouch事件,处理它,并传给孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解处理触摸如何Android的事件,并得到了一个有点困惑。从我的理解触摸事件被发送到根视图,并传递到孩子们。 我有一个的FrameLayout 这是一个容器片段。 第一个片段视图是一个滚动型,第二个是某种图库(Horizo​​ntalListView)和最后一个也是的FrameLayout 。只有每次一个片段中的布局。 我想要做的就是找​​出用户刷卡在屏幕上,为应用程序使用。我想算刷卡刷卡和一定数量后做一些事情。

I tried to understand how Android handle touch event and got a little bit confused. From what I understand touch event are send to the root view and pass down to the children. I have a FrameLayout that is a container for Fragment. First fragment view is a ScrollView, second one is some kind of Gallery (HorizontalListView) and the last one is also FrameLayout. Only one fragment in the layout each time. What I want to do is to identify user swipes on the screen, for the app use. I want to count the swipes and do something after some number of swipes.

我试图把一个 OnTouchListener 顶部的FrameLayout ,但它不会被调用时,孩子在滚动型图库。我试图返回在年底 onTouch ,但我得到同样的结果 - 它从来没有被称为

I tried to put a OnTouchListener on the top FrameLayout but it doesn't get called when the child is the ScrollView or the Gallery. I tried to return false and also true in the end of onTouch, but I get same result - it's never being called.

我该怎么办呢? 我只是想透明处理触摸事件,并通过他们就像我甚至没有碰他们。

How can I do it? I just want to "transparently" handle the touch events and passing them on like I didn't even touch them.

在此先感谢, 埃拉德

推荐答案

我的理解是,它实际上是去了另一个方向。孩子的意见得到他们的第一个事件触发(排序)。根视图得到的是 dispatchTouchEvent()调用,它将该事件传播到孩子们的的onTouchEvent(),然后,取决于他们是否返回true或false,父母的的onTouchEvent()之称。

My understanding is that it actually goes the other direction. The Child views get their event triggered first (sort of). The root view get's it's dispatchTouchEvent() called, which propagates the event down to the children's onTouchEvent(), and then, depending on whether they return true or false, the parent's onTouchEvent() is called.

请原谅我没有测试这一点,但我觉得对于拦截这样的事情正常的解决方案是覆盖 dispatchTouchEvent(MotionEvent EV)于一体的活动,像这样:

Forgive me for not testing this, but I think the normal solution for intercepting things like this is to override dispatchTouchEvent(MotionEvent ev) in one's activity like so:

@Override
public boolean dispatchTouchEvent (MotionEvent ev) {
  // Do your calcluations
  return super.dispatchTouchEvent(ev);
}

该文档这个人是<一个href="http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent%28android.view.MotionEvent%29"相对=nofollow>此处。请注意,您也可以覆盖在该方法中的任何的ViewGroup (如的FrameLayout 等)

The documentation for this one is here. Note that you can also override that method in any ViewGroup (such as a FrameLayout, etc)

这篇关于由家长抓住onTouch事件,处理它,并传给孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆