涉及到事件分发过程的方法一共有3个,其中2个是View类的方法:
这个方法是整个事件分发的开始,负责消费事件和分发事件(仅ViewGroup类有此作用),网上一般说返回true会消耗事件,使事件不向下分发(其实返回true的话事件确实不会向上传递了,也就是事件被消耗掉了,但是事件依旧会向下传递到最底部),实质上,这里的返回值是不应该由开发者来指定的,至于原因,留个心,下面会说到,如果开发者进行指定的话,无论返回什么,都不会向下分发。
这个方法是事件的处理方法,也是大部分时候重写的地方。这里返回true的话,事件会在这一层就结束,不会向上传递。不同于上面的dispatchTouchEvent,这个方法是View的,ViewGroup并没有重写这个方法。
最后一个,是ViewGroup独有的:
3、public boolean onInterceptTouchEvent(MotionEvent ev)这个方法最简单,运行在dispatchTouchEvent之后, 当返回true时,事件在这一层ViewGroup被拦截住了,不会向下传递了(这个才是阻止事件向下分发的地方)。
好了对现象的描述到此为止,现在开始对本质的探索。
为了方便理解,我先将整个流程梗概写出来,请带着梗概一起阅读,括号后面为该方法所属的类,V=View,VG=ViewGroup。
好了,一切准备就绪了,可以开始草源码了。
首先从第一个方法开始,事件分发的入口是最外层的ViewGroup的dispatchTouchEvent方法,该方法很复杂,这里我只清点分发逻辑相关的代码进行讲解。
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
intercepted = true;
...
if (!canceled && !intercepted) {...}//这里就是向下分发事件的代码
View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
? findChildWithAccessibilityFocus() : null;
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
final int actionIndex = ev.getActionIndex(); // always 0 for down
final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
: TouchTarget.ALL_POINTER_IDS;
// Clean up earlier touch targets for this pointer id in case they
// have become out of sync.
removePointersFromTouchTargets(idBitsToAssign);
final int childrenCount = mChildrenCount;
if (newTouchTarget == null && childrenCount != 0) {
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);
// Find a child that can receive the event.
// Scan children from front to back.
final ArrayList preorderedList = buildOrderedChildList();
final boolean customOrder = preorderedList == null
&& isChildrenDrawingOrderEnabled();
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = customOrder
? getChildDrawingOrder(childrenCount, i) : i;
final View child = (preorderedList == null)
? children[childIndex] : preorderedList.get(childIndex);
// If there is a view that has accessibility focus we want it
// to get the event first and if not handled we will perform a
// normal dispatch. We may do a double iteration but this is
// safer given the timeframe.
if (childWithAccessibilityFocus != null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus = null;
i = childrenCount - 1;
}
... //for循环的代码块还没有结束哦
newTouchTarget = getTouchTarget(child);
if (newTouchTarget != null) {
// Child is already receiving touch within its bounds.
// Give it the new pointer in addition to the ones it is handling.
newTouchTarget.pointerIdBits |= idBitsToAssign;
break;
}
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled = true;
} else {
final boolean cancelChild = resetCancelNextUpFlag(target.child)
|| intercepted;
if (dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)) {
handled = true;
}
if (cancelChild) {
if (predecessor == null) {
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
...
return handled;
好了,到这里ViewGroup的dispatchTouchEvent方法就结束了,其实从这个方法基本上就能看到整个事件分发机制的雏形了,还记得之前提到的为什么重写的时候不要手动给dispatchTouchEvent返回值么,因为事件分发的代码就是在super.dispatchTouchEvent里面啊。
那么,疑问来了,既然无法指定dispatchTouchEvent的返回值,那么要怎么去控制事件被消耗呢,根据之前的梗概流程,让我们看一下View类的dispatchTouchEvent:
boolean result = false;
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(event, 0);
}
final int actionMasked = event.getActionMasked();
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Defensive cleanup for new gesture
stopNestedScroll();
}
if (onFilterTouchEventForSecurity(event)) { //留个心
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {//看这里,看这里
result = true;
}
if (!result && onTouchEvent(event)) {//还有这里,这里
result = true;
}
}
...
return result;