内容简介:注意:在阅读之前,请看View的事件分发。ViewGroup是View的子类。但是ViewGroup在Android中又是作为View控件的容器存在。ViewGroup拥有dispatchTouchEvent、onTouchEvent和onInterceptTouchEvent三个方法。相对于View添加了一个onInterceptTouchEvent方法。
注意:在阅读之前,请看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
微信打赏
支付宝打赏
感谢您对作者Bob的打赏,我们会更加努力! 如果您想成为作者,请点我
以上所述就是小编给大家介绍的《ViewGroup的事件分发【原创】》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- View的事件分发(一)分发流程
- Flex 事件分发(FlexViewer事件机制)剥离过程
- Android事件分发机制
- View事件分发机制分析
- 【透镜系列】看穿 > 触摸事件分发
- Android事件分发源码归纳
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Smashing Book
Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90
The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!