事件分发机制
举个例子:
ViewGroup1----->ViewGroup----->View
-
默认的时候:(View可点击)
点击事件会由外向内传递,每一个动作都会发生事件的传递
//-----------------Down--------------------------- E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onInterceptTouchEvent: viewgroup---- E/Button1: dispatchTouchEvent: button1---- E/Button1: onTouchEvent: button1---- E/Button1: onTouchEventDown: button1---- //===================Up======================== E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onInterceptTouchEvent: viewgroup---- E/Button1: dispatchTouchEvent: button1---- E/Button1: onTouchEvent: button1---- E/Button1: onTouchEventUp: button1----
-
默认的时候:(View不可点击)
当点击事件分发到最底层的时候,并且最底层的子View没有消费时间的能力时,此时会将Down事件交由它的父布局进行处理,如果父布局处理不了,逐级上传,一直到Activity
E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onInterceptTouchEvent: viewgroup---- E/Button1: dispatchTouchEvent: button1---- E/Button1: onTouchEvent: button1---- E/Button1: onTouchEventDown: button1---- E/ViewGroup: onTouchEvent: viewgroup---- E/ViewGroup: onTouchEventDown: viewgroup---- E/ViewGroup1: onTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEventDown: viewgroup1----
-
View的父布局拦截事件(onInterceptTouchEvent ------> true)
当点击事件在ViewGroup处进行拦截的时候,Down事件将不会再传递到他的子View,同时,他回交由自己的onTouchEvent方法进行事件的处理,如果没有处理能力,则将时间逐级上传,直到Activity
E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onInterceptTouchEvent: viewgroup---- E/ViewGroup: onTouchEvent: viewgroup---- E/ViewGroup: onTouchEventDown: viewgroup---- E/ViewGroup1: onTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEventDown: viewgroup1----
-
View的父布局拦截事件(onInterceptTouchEvent ------> true ,onTouchEvent----->true)
onTouchEvent----->true:表示事件可以在此方法内消费掉,所以ViewGroup不会再将Down事件交由它的上级进行处理。
//=======================Down======================= E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onInterceptTouchEvent: viewgroup---- E/ViewGroup: onTouchEvent: viewgroup---- E/ViewGroup: onTouchEventDown: viewgroup---- //======================Up====================== E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onTouchEvent: viewgroup---- E/ViewGroup: onTouchEventUp: viewgroup----
-
最外层的ViewGroup1拦截事件(onInterceptTouchEvent ------> true)
事件将到此为止,交给自己的onTouchEvent进行处理,不能处理的话则交由上级。
可以在布局里设置clickable="true",或者是onTouchEvent返回true
//====================clickable="false"============================ E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEventDown: viewgroup1---- //=========================clickable="true"=============================== E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onInterceptTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEventDown: viewgroup1---- E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEventMove: viewgroup1---- E/ViewGroup1: dispatchTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEvent: viewgroup1---- E/ViewGroup1: onTouchEventUp: viewviewgroup1----
当ViewGroup和View都有消费事件的能力时,情况和第一种其实是一样的,因为时间能够不被拦截,一直传递,所以在View的时候,已经把他消费掉了,就没有了。
-
当ViewGroup1的dispatchTouchEvent------>false时:
E/ViewGroup1: dispatchTouchEvent: viewgroup1----
-
当ViewGroup的dispatchTouchEvent------>false时:
表示事件不在分发下去,Down事件交由它的父布局进行处理,父布局处理不了,则再往上递交,一直到Activity
//---------0表示的是Down事件-----1表示Up事件 E/ViewGroup1: dispatchTouchEvent: viewgroup1----0 E/ViewGroup1: onInterceptTouchEvent: viewgroup1----0 E/ViewGroup: dispatchTouchEvent: viewgroup----0 //-----------ViewGroup1的onTouchEvent返回false时 E/ViewGroup1: onTouchEvent: viewgroup1----0 E/ViewGroup1: onTouchEventDown: viewgroup1----0 //-----------ViewGroup1的onTouchEvent返回true时 E/ViewGroup1: onTouchEvent: viewgroup1----0 E/ViewGroup1: onTouchEventDown: viewgroup1----0 E/ViewGroup1: dispatchTouchEvent: viewgroup1----1 E/ViewGroup1: onTouchEvent: viewgroup1----1 E/ViewGroup1: onTouchEventUp: viewviewgroup1----1
-
View.setOnClickListener/View.setOnTouchListener-----false
onTouch方法要优先于onClick方法执行
E/ViewGroup1: dispatchTouchEvent: viewgroup1----0 E/ViewGroup1: onInterceptTouchEvent: viewgroup1----0 E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onInterceptTouchEvent: viewgroup---- E/Button1: dispatchTouchEvent: button1---- E/MainActivity: onTouch: button1 E/Button1: onTouchEvent: button1---- E/Button1: onTouchEventDown: button1---- E/ViewGroup1: dispatchTouchEvent: viewgroup1----1 E/ViewGroup1: onInterceptTouchEvent: viewgroup1----1 E/ViewGroup: dispatchTouchEvent: viewgroup---- E/ViewGroup: onInterceptTouchEvent: viewgroup---- E/Button1: dispatchTouchEvent: button1---- E/MainActivity: onTouch: button1 E/Button1: onTouchEvent: button1---- E/Button1: onTouchEventUp: button1---- E/MainActivity: onClick: button1
View.setOnClickListener/View.setOnTouchListener-----true
E/ViewGroup1: dispatchTouchEvent: viewgroup1----0
E/ViewGroup1: onInterceptTouchEvent: viewgroup1----0
E/ViewGroup: dispatchTouchEvent: viewgroup----
E/ViewGroup: onInterceptTouchEvent: viewgroup----
E/Button1: dispatchTouchEvent: button1----
E/MainActivity: onTouch: button1
E/ViewGroup1: dispatchTouchEvent: viewgroup1----1
E/ViewGroup1: onInterceptTouchEvent: viewgroup1----1
E/ViewGroup: dispatchTouchEvent: viewgroup----
E/ViewGroup: onInterceptTouchEvent: viewgroup----
E/Button1: dispatchTouchEvent: button1----
E/MainActivity: onTouch: button1
说说原理:
一段伪代码开路
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean consume = false;//事件是否被消费
if (onInterceptTouchEvent(ev)){//调用onInterceptTouchEvent判断是否拦截事件
consume = onTouchEvent(ev);//如果拦截则调用自身的onTouchEvent方法
}else{
consume = child.dispatchTouchEvent(ev);//不拦截调用子View的dispatchTouchEvent方法
}
return consume;//返回值表示事件是否被消费,true事件终止,false调用父View的onTouchEvent方法
}
//来自于:https://www.jianshu.com/p/238d1b753e64
特别强调
- 子View可以通过requestDisallowInterceptTouchEvent方法干预父View的事件分发过程(ACTION_DOWN事件除外)
- 对于View(注意!ViewGroup也是View)而言,如果设置了onTouchListener,那么OnTouchListener方法中的onTouch方法会被回调。onTouch方法返回true,则onTouchEvent方法不会被调用(onClick事件是在onTouchEvent中调用)所以三者优先级是onTouch->onTouchEvent->onClick
- View 的onTouchEvent 方法默认都会消费掉事件(返回true),除非它是不可点击的(clickable和longClickable同时为false),View的longClickable默认为false,clickable需要区分情况,如Button的clickable默认为true,而TextView的clickable默认为false。