如何处理点击子视图,和触摸父ViewGroups? [英] How to handle click in the child Views, and touch in the parent ViewGroups?
问题描述
在我的布局,我有这样的结构:
- RelativeLayout的
|
--FrameLayout
|
--Button,EditText上...
我要处理的RelativeLayout的和的FrameLayout触摸事件,所以我在这两个视图组设置onTouchListener。但是,只有触摸的RelativeLayout的被捕获。
要尝试解决这个问题,我写我自己CustomRelativeLayout,并覆盖 onInterceptTouchEvent
,现在点击在孩子的ViewGroup(的FrameLayout)被捕获,但点击按钮和其他视图不作任何影响。
在我自己的自定义布局,我有这样的:
公共布尔onInterceptTouchEvent(MotionEvent EV){
返回true;
}
您需要重写 onInterceptTouchEvent()
为每一个孩子,否则将仍然是一个<$ C C>的onTouchEvent $父。
@覆盖
公共布尔onInterceptTouchEvent(MotionEvent EV){
/ *
*此方法只决定了我们是否要拦截的议案。
*如果我们返回true,的onTouchEvent将被调用,我们做实际的
*滚动出现。
* /
...
//在一般情况下,我们不希望拦截触摸事件。它们应该是
//通过子视图处理。
返回false;
}
您需要返回false有孩子处理它,否则你将其返回到母公司。
In my layout I have a structure like that:
--RelativeLayout
|
--FrameLayout
|
--Button, EditText...
I want to handle touch events in the RelativeLayout and in the FrameLayout, so I set the onTouchListener in these two view groups. But only the touch in the RelativeLayout is captured.
To try solve this, I wrote my own CustomRelativeLayout, and override the onInterceptTouchEvent
, now the click in the child ViewGroup (FrameLayout) is captured, but the click in the buttons and other views doesn't make any effect.
In my own custom layout, I have this:
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
You need to override the onInterceptTouchEvent()
for each child, otherwise it will remain an onTouchEvent
for the parent.
Intercept Touch Events in a ViewGroup
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
/*
* This method JUST determines whether we want to intercept the motion.
* If we return true, onTouchEvent will be called and we do the actual
* scrolling there.
*/
...
// In general, we don't want to intercept touch events. They should be
// handled by the child view.
return false;
}
You need to return false to have the child handle it, otherwise you are returning it to the parent.
这篇关于如何处理点击子视图,和触摸父ViewGroups?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!