1.基本知识
1.1 Android事件分发简单可以分为三个部分,Activity,ViewGrop,View,所有的事件的入口时dispatchTouchEvent。
1.2 Activity的根视图实际是一个ViewGroup,我们的视图实际是加在这个ViewGroup的。
2.事件分发
2.1 Activity
事件最先是从Activity的dispatchTouchEvent开始的,在dispatchTouchEvent里面,会调用根视图的dispatchTouchEvent,也就是ViewGroup的dispatchTouchEvent,如果ViewGroup的dispatchTouchEvent返回的是true,事件传递就结束;否则返回Activity的onTouchEvent。
2.2 ViewGroup
ViewGroup的dispatchTouchEvent如果没有设置拦截,会调用子控件的dispatchTouchEvent,并且子控件dispatchTouchEvent返回了true,事件传递就结束;否则返回ViewGroup的基类的dispatchTouchEvent也就是View的dispatchTouchEvent。
ViewGroup有2种方式设置拦截一个是调用requestDisallowInterceptTouchEvent(true),另外一个是实现onInterceptTouchEvent并且返回true。
ViewGroup默认onInterceptTouchEvent返回的是false。
3 View
View的dispatchTouchEvent 如果View设置了setOnTouchListener(OnTouchListener )并且OnTouchListener.onTouch返回true,事件传递就结束;否则 return onTouchEvent。
onTouchEvent会执行setOnClickListener如果设置了话。