通过触摸事件父视图的onTouchListener在Android中 [英] Pass touch event to Parent View's onTouchListener in Android

查看:200
本文介绍了通过触摸事件父视图的onTouchListener在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的产品有一个按钮,它的子视图的LinearLayout列表视图。我想要的LinearLayout的ontouchLIstener工作。我不想使用 onInterceptTouchEvent 。有没有一种方式,我可以通过对触摸形式的按钮列表视图父。我想这
- 从按钮的onTouchListener返回true

I have a list view whose item is a linearlayout which has a button as it's child view. I want the ontouchLIstener of the linearlayout to work. I don't want to use onInterceptTouchEvent. Is there a way a I can pass on the touch form the button to the parent listview. I tried this - returning true from the button's onTouchListener

private View.OnTouchListener buttonListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            Log.i(TAG, "Button On Touch");
            return true;
        }
};

但是,这并不正常工作。它没有通过该触摸事件到的LinearLayout的 onTouchListener

有一定好歹它应该工作。

There must be someway it should work.

推荐答案

在View群体性事件的链条是这样的 - 在则dispatchEvent 被调用。然后则dispatchEvent 要求 onInterceptTouchEvent 。如果返回的的,那么触摸事件被传递到的ViewGroup的孩子。如果任何的儿童消耗事件(在此情况下,按钮消耗的情况下)即,如果它们返回的的那么motionevent上没有其他方法通过。由于按钮点击它在这种情况下返回true。搜索结果如果 onInterceptTouchEvent 方法,那么孩子的意见也得不到运动的事件,而是返回true ViewGroup中的 onTouchListener onTouch 方法被调用。结果因此通过在触摸事件父(查看组) onTouchListener 确保返回的真正的在家长(的ViewGroup)的onInterceptTouchEvent 方法。您不要必须重写 onDispatchTouchEvent()

The chain of events for a View group works like this - The dispatchEvent is called. Then dispatchEvent calls onInterceptTouchEvent. if it returns false, then the touch events are passed on to the children of the ViewGroup. If any of the children consume the event (in this case the button consumes the event) i.e if they return true then the motionevent is not passed on to other methods. Since the button is clickable it returns true in this case.

If the onInterceptTouchEvent method returns true then the child views are not given the motion event and instead that ViewGroup's onTouchListener or onTouch method are called.
Hence to pass on the touch event to the parent's (View Group) onTouchListener make sure to return true in the onInterceptTouchEvent method of the Parent (ViewGroup). You don't have to override onDispatchTouchEvent()

@Override
    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {

        Log.i(TAG,"PARENT.onInterceptTouchEvent");

        return true;
}

有关触摸导航是怎么做的更多详细信息,请参阅<一个href=\"http://stackoverflow.com/questions/9586032/android-difference-between-onintercepttouchevent-and-dispatchtouchevent\">this叠后

For more details about how the touch navigation is done, please see this stack post

这篇关于通过触摸事件父视图的onTouchListener在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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