最新消息:文章中包含代码时,请遵守代码高亮规范!

ViewGroup的事件分发【原创】

Android 王 伟 280浏览 0评论

注意:在阅读之前,请看View的事件分发。

ViewGroup是View的子类。但是ViewGroup在Android中又是作为View控件的容器存在。ViewGroup拥有dispatchTouchEvent、onTouchEvent和onInterceptTouchEvent三个方法。相对于View添加了一个
onInterceptTouchEvent方法。

现在看一下示例:写一个MyRelativeLayout继承RelativeLayout

public class MyRelativeLayout extends RelativeLayout {
    private static final String TAG = "MyRelativeLayout";
    public MyRelativeLayout(Context context) {
        super(context);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "dispatchTouchEvent   ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "dispatchTouchEvent   ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "dispatchTouchEvent   ACTION_UP");
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onTouchEvent   ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "onTouchEvent   ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onTouchEvent   ACTION_UP");
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onInterceptTouchEvent   ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "onInterceptTouchEvent   ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onInterceptTouchEvent   ACTION_UP");
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}

将布局文件中的 RelativeLayout换成 MyRelativeLayout,点击MyTextView,查看log:

com.bob.viewtestdemo I/MainActivity: dispatchTouchEvent ACTION_DOWN
com.bob.viewtestdemo I/MyRelativeLayout: dispatchTouchEvent ACTION_DOWN
com.bob.viewtestdemo I/MyRelativeLayout: onInterceptTouchEvent ACTION_DOWN
com.bob.viewtestdemo I/MyTextView: dispatchTouchEvent ACTION_DOWN
com.bob.viewtestdemo I/MainActivity: MyTextView onTouch ACTION_DOWN
com.bob.viewtestdemo I/MyTextView: onTouchEvent ACTION_DOWN
com.bob.viewtestdemo I/MainActivity: dispatchTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MyRelativeLayout: dispatchTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MyRelativeLayout: onInterceptTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MyTextView: dispatchTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MainActivity: MyTextView onTouch ACTION_MOVE
com.bob.viewtestdemo I/MyTextView: onTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MainActivity: dispatchTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MyRelativeLayout: dispatchTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MyRelativeLayout: onInterceptTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MyTextView: dispatchTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MainActivity: MyTextView onTouch ACTION_MOVE
com.bob.viewtestdemo I/MyTextView: onTouchEvent ACTION_MOVE
com.bob.viewtestdemo I/MainActivity: dispatchTouchEvent ACTION_UP
com.bob.viewtestdemo I/MyRelativeLayout: dispatchTouchEvent ACTION_UP
com.bob.viewtestdemo I/MyRelativeLayout: onInterceptTouchEvent ACTION_UP
com.bob.viewtestdemo I/MyTextView: dispatchTouchEvent ACTION_UP
com.bob.viewtestdemo I/MainActivity: MyTextView onTouch ACTION_UP
com.bob.viewtestdemo I/MyTextView: onTouchEvent ACTION_UP
com.bob.viewtestdemo I/MainActivity: MyTextView onclick

总结一下:

  • 触摸事件的传递顺序是由Activity到ViewGroup,再由ViewGroup递归传递给它的子View。
  • ViewGroup通过onInterceptTouchEvent方法对事件进行拦截,如果该方法返回true,则事件不会继续传给子View,如果返回false或者super.onInterceptTouchEvent,则事件会继续传递给子View。
  • 在子View中对事件进行消费后,ViewGroup将收不到任何事件。

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/25100.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Bob的打赏,我们会更加努力!    如果您想成为作者,请点我

发表我的评论
取消评论

表情