Activity->Window->DecorView->ViewGroup->View
ACTION_DOWN、ACTION_MOVE、ACTION_UP、ACTION_CANCEL
通常一个事件序列是这样的ACTION_DOWN 事件是一个事件的起点,然后伴随着多个ACTION_DOWN事件,然后是ACTION_DOWN,中间可能会收到一个ACTION_DOWN事件
//Activity的dispatchTouchEvent方法
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
可以看出,Activity 其实是调用了 Window 的 superDispatchTouchEvent 方法,而 Window 的实现类是 PhoneWindow,因此我们直接查看 PhoneWindow 的 superDispatchTouchEvent 方法
//PhoneWindow的superDispatchTouchEvent方法
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
return mDecor.superDispatchTouchEvent(event);
}
发现是直接调用的 DecorView 的 superDispatchTouchEvent 方法,再进一步查看
//DecorView的superDispatchTouchEvent方法
public boolean superDispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
原来这儿就调用了 ViewGroup 的 dispatchTouchEvent 方法,也就是说界面上的事件直接传递给了根布局的 dispatchTouchEvent 方法
分析dispatchTouchEvent方法之前,先看一下dispatchTransformedTouchEvent方法,dispatchTouchEvent方法内部会多次调用到了dispatchTransformedTouchEvent方法
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
final boolean handled;
// 如果是取消操作,则直接分发取消事件
final int oldAction = event.getAction();
if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
event.setAction(MotionEvent.ACTION_CANCEL);
// 如果传入的 child 不为空,则调用 child 的 dispatchTouchEvent 方法,否则调用自身的 dispatchTouchEvent 方法
if (child == null) {
//相当于调用了View类的dispatchTouchEvent方法
handled = super.dispatchTouchEvent(event);
} else {
//继续分发事件到子View
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
return handled;
}
......
// 如果传入的 child 不为空,则调用 child 的 dispatchTouchEvent 方法,否则调用自身的 dispatchTouchEvent 方法
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent);
} else {
......
handled = child.dispatchTouchEvent(transformedEvent);
}
......
return handled;
}
可以看出 dispatchTransformedTouchEvent 方法主要做了两件事
下面分析dispatchTouchEvent的核心代码部分
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
......
boolean handled = false;
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
// 1. DOWN 事件进行初始化,清空 TouchTargets 和 TouchState mFirstTouchTarget=null,disallowIntercept=false
if (actionMasked == MotionEvent.ACTION_DOWN) {
cancelAndClearTouchTargets(ev);
resetTouchState();
}
// 2. 检查是否拦截
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
// 是否强制不允许拦截,子 View 可以设置 parent 强制不允许拦截,默认为 false
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 {
intercepted = true;
}
......
// 3. 如果没有被拦截, 先处理 DOWN 事件,主要是赋值 TouchTarget
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
if (!canceled && !intercepted) {
......
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
......
final int childrenCount = mChildrenCount;
if (newTouchTarget == null && childrenCount != 0) {
......
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = getAndVerifyPreorderedIndex(
childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(
preorderedList, children, childIndex);
......
// 找到 Visible 并且处于点击范围的子 View
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
......
// 相当于调用子 View 的 dispatchTouchEvent 方法
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
......
// 赋值 TouchTarget,刷新标志位 mFirstTouchTarget赋值了
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
......
}
......
}
......
}
}
// 4. 是自己处理事件还是交由子 View 处理事件
if (mFirstTouchTarget == null) {
// 没有子 View 消耗事件,则自己消耗,相当于调用 super.dispatchTouchEvent 方法
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
// 如果是 DOWN 事件,则上面已经调用了子 View 的 dispatchTouchEvent 方法,则什么都不用做
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled = true;
} else {
// 根据 intercepted 决定是否将事件强制改为 CANCEL 事件
final boolean cancelChild = resetCancelNextUpFlag(target.child)
|| intercepted;
// 相当于调用子 View 的 dispatchTouchEvent 方法。如果 intercepted=true,此时会强制将 action 改为 CANCEL;如果 intercepted=false,则
if (dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)) {
handled = true;
}
// 如果 intercepted=true,则将 mFirstTouchTarget 置为 null
if (cancelChild) {
if (predecessor == null) {
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}
}
......
}
......
return handled;
}
dispatchTouchEvent 方法主要由4个模块组成的
至此ViewGroup的dispatchTouchEvent说完了,总结下
down事件ViewGroup会清空mFirstTouchTarget并且重置disallowIntercept标记为false,所以每次down事件来临时,父容器第一步会调用onInterceptTouchEvent询问自己是否拦截.如果down事件拦截了,那么此次down事件会交给ViewGroup处理,此时mFirstTouchTarget=null的,所以下次move/up事件来临时,父容器直接将intercepted=true,所以ViewGroup一旦将down事件拦截,那么之后的事件都将由他自己处理,子View不会收到任何事件
当down事件ViewGroup没有拦截时,此时ViewGroup会遍历子View,寻找能处理这个事件的View,如果没有找到,同样这个事件依然被ViewGroup处理,后续事件都有他处理。当ViewGroup找到了处理down事件的子View,此时会将mFirstTouchTarget赋值,即下次move事件来临时mFirstTouchTarget!=null的
当move事件来临时,ViewGroup依然回调用onInterceptTouchEvent询问自己是否拦截,如果拦截了,此时mFirstTouchTarget!=null,根据上面的分析,此次子View会收到一个cancel事件,并且ViewGroup会清空mFirstTouchTarget=null,之后的事件由于mFirstTouchTarget=null,所以事件都给有ViewGroup自身处理。
public boolean dispatchTouchEvent(MotionEvent event) {
boolean result = false;
//这里判断是否设置了mOnTouchListener,是否是ENABLED,onTouch的返回值
//所以优先级的onTouch>onTouchEvent
if (onFilterTouchEventForSecurity(event)) {
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
//这里会调用onTouchEvent
if (!result && onTouchEvent(event)) {
result = true;
}
}
return result;
}
总结一下,首先如果设置了了mOnTouchListener,它的优先级是大于onTouchEvent的。当其onTouch方法返回true的时候onTouchEvent不会被调用
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
final int viewFlags = mViewFlags;
final int action = event.getAction();
//这里判断view是否是可以点击的
final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;
//如果view的状态是禁用的,只要它是可以点击的就可以消费事件,只是不做任何的响应
if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
// A disabled view that is clickable still consumes the touch
// events, it just doesn't respond to them.
return clickable;
}
//这里如何设置了mTouchDelegate,会先调用mTouchDelegate.onTouchEvent(event)
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}
//只要是可以点击的就会进来,且最终一定会return true 消费事件
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
switch (action) {
case MotionEvent.ACTION_UP:
...
//这里处理点击事件onClick
performClickInternal();
break;
case MotionEvent.ACTION_DOWN:
....
if (!clickable) {
checkForLongClick(0, x, y);
break;
}
....
if (isInScrollingContainer) {
....
postDelayed(mPendingCheckForTap,
} else {
....
checkForLongClick(0, x, y);
}
break;
case MotionEvent.ACTION_CANCEL:
....
case MotionEvent.ACTION_MOVE:
....
break;
}
return true;
}
return false;
}
总结一下,只要View是可以点击的就可以消费事件,并且如果设置了mTouchDelegate,则会先执行mTouchDelegate的onTouchEvent方法,onLongClick的执行是在action_down中,最终走到
performLongClick方法中,如下
public boolean performLongClick() {
return performLongClickInternal(mLongClickX, mLongClickY);
}
private boolean performLongClickInternal(float x, float y) {
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
boolean handled = false;
final ListenerInfo li = mListenerInfo;
if (li != null && li.mOnLongClickListener != null) {
handled = li.mOnLongClickListener.onLongClick(View.this);
}
if (!handled) {
final boolean isAnchored = !Float.isNaN(x) && !Float.isNaN(y);
handled = isAnchored ? showContextMenu(x, y) : showContextMenu();
}
if ((mViewFlags & TOOLTIP) == TOOLTIP) {
if (!handled) {
handled = showLongClickTooltip((int) x, (int) y);
}
}
if (handled) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
return handled;
}
得到调用。onClick的执行是在action_up中,最终走到
performClick方法中,如下
public boolean performClick() {
....
final boolean result;
final ListenerInfo li = mListenerInfo;
if (li != null && li.mOnClickListener != null) {
playSoundEffect(SoundEffectConstants.CLICK);
li.mOnClickListener.onClick(this);
result = true;
} else {
result = false;
}
return result;
}
,优先级onLongClick>onClick
当一个事件来临时,不只一个View可以处理此次事件,这时候父容器不知道将事件分发给谁处理,这时候就产生了事件冲突,通过以上分析,我们可以得出来两个思路,第一个外部拦截法,通过改变ViewGroup的onIntercept方法的返回值。第二个通过更改disallowIntercept标记,干扰ViewGroup的分发流程
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = false;
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
intercepted = false;
break;
case MotionEvent.ACTION_MOVE:
if (父容器需要事件) {
intercepted = true;
} else {
intercepted = false;
}
break;
case MotionEvent.ACTION_UP:
intercepted = false;
break;
default:
break;
}
return intercepted;
}
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
if (父容器需要事件) {
getParent().requestDisallowInterceptTouchEvent(false);
}
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}