关于View的事件分发,移步 View事件分发
ViewGroup事件分发的起点和View相同都是dispatchTouchEvent这个方法,只不过ViewGroup重写了View中的dispatchTouchEvent方法。
疑问:
1、ViewGroup如何把时间分发给子View?
2、若要ViewGroup不把任何时间分发给子View,该如何做?
3、从手指按下到事件分发到View,这中间经历了哪些过程?
4、在一个事件流中,如果想要在中途使得ViewGroup拦截事件该如何做?
ViewGroup第一段代码的逻辑是对于事件拦截的处理:
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Throw away all previous state when starting a new touch gesture.
// The framework may have dropped the up or cancel event for the previous gesture
// due to an app switch, ANR, or some other state change.
cancelAndClearTouchTargets(ev);
resetTouchState();
}
// Check for interception.
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;
}
使用intercepted这个变量来确定当前ViewGroup是否拦截事件,一旦拦截,那么后续事件都不会分发给子View。
对于DOWN这个事件,在这段逻辑中是一定不会拦截的,拦截的条件是disallowIntercept为false,并且onInterceptTouchEvent方法返回true。
这就为上面第2个问题提供了方法,也就是把mGroupFlags的FLAG_DISALLOW_INTERCEPT设置为0,并且重写onInterceptTouchEvent方法
返回true,ViewGroup的onInterceptTouchEvent方法默认返回false。这样任何事件都不能传递给子View了,其实disallowIntercept可以不用设置在上面判断是否为DOWN事件时,已经在resetTouchState方法把FLAG_DISALLOW_INTERCEPT这个标志位置为0了。
接着看下一段逻辑,如果没有被ViewGroup拦截的话,那么就会把事件派发给子View
if (!canceled && !intercepted) {
// If the event is targeting accessiiblity focus we give it to the
// view that has accessibility focus and if it does not handle it
// we clear the flag and dispatch the event to all children as usual.
// We are looking up the accessibility focused host to avoid keeping
// state since these events are very rare.
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<View> preorderedList = buildTouchDispatchChildList();
final boolean customOrder = preorderedList == null
&& isChildrenDrawingOrderEnabled();
final View[] children = mChildren;
//遍历子View,查找消耗事件的子View
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = getAndVerifyPreorderedIndex(
childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(
preorderedList, children, 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;
}
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
//(1)这里比较关键,如果目标已经不为null,也就是这个事件流中已经找到了一个View消耗事件
//那么就直接break;不需要再往下找了
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;
}
resetCancelNextUpFlag(child);
//(2)关键:如果返回true,说明事件被该child消耗,将该child加入到mFirstTouchTarget
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();
//把child加入到mFirstTouchTarget
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
// The accessibility focus didn't handle the event, so clear
// the flag and do a normal dispatch to all children.
ev.setTargetAccessibilityFocus(false);
}
if (preorderedList != null) preorderedList.clear();
}
if (newTouchTarget == null && mFirstTouchTarget != null) {
// Did not find a child to receive the event.
// Assign the pointer to the least recently added target.
newTouchTarget = mFirstTouchTarget;
while (newTouchTarget.next != null) {
newTouchTarget = newTouchTarget.next;
}
newTouchTarget.pointerIdBits |= idBitsToAssign;
}
}
}
这一段逻辑比较长,其核心就是位于for循环中,倒着遍历,这时因为越往后面加入的子View,其图层越位于外面,
这里主要注意两个点,第56行:如果目标已经不为null,也就是这个事件流中已经找到了一个View消耗事件那么就直接break;不需要再往下找了,
以后同一个事件流里面的事件都是分发给这个view,具体分发步骤在下一段逻辑;第67行,这句代码还是处于探测阶段,也就是
一直找,知道找到dispatchTransformedTouchEvent返回true,也就是被事件该child消耗,那么记录该child,跳出for循环。
接着下面就要看是否找到消耗事件的子View了,如果没有找到,就要看ViewGroup是否消耗事件;如果找到了消耗事件的子View,就把事件派发
给该子View。
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
//ViewGroup是否消耗事件,如果拦截的话,mFirstTouchTarget也为null,因为在最开始处理
//事件如果为DOWN的话,也就是一个事件流的开始,mFirstTouchTarget会被置为null
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {
//派发给子View,调用子View的dispatchTouchEvent方法,一步步分发下去
// Dispatch to touch targets, excluding the new touch target if we already
// dispatched to it. Cancel touch targets if necessary.
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
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;
}
}
predecessor = target;
target = next;
}
}