2. ViewGroup的事件分发
一.案例
测试ViewGroup事件分发,首先新建类然后继承RelativeLayout,然后重写相应方法
public class TestRelativeLayout extends RelativeLayout {
private static final String TAG = "-EVENT";
public TestRelativeLayout(Context context) {
super(context);
}
public TestRelativeLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TestRelativeLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d(TAG, "onInterceptTouchEvent: viewGroup "+ev.getAction());
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d(TAG, "dispatchTouchEvent: viewGroup "+ev.getAction());
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "onTouchEvent: viewGroup "+event.getAction());
return super.onTouchEvent(event);
}
@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Log.d(TAG, "requestDisallowInterceptTouchEvent: viewGroup "+disallowIntercept);
super.requestDisallowInterceptTouchEvent(disallowIntercept);
}
}
ViewGroup继承于View,相较于View,ViewGroup多了一个onInterceptTouchEvent的方法
同样在Activity中设置
ll_parent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "onTouch: viewGroup "+event.getAction());
return false;
}
});
ll_parent.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.d(TAG, "onLongClick: viewGroup ");
return false;
}
});
ll_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: viewGroup ");
}
});
然后布局文件
<com.jiang.eventtest.TestRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jiang.eventtest.MainActivity">
<com.jiang.eventtest.TestButton
android:id="@+id/btn_test"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:text="button" />
</com.jiang.eventtest.TestRelativeLayout>
点击中间的按键并在其上滑动,可以看到以下信息:
03-21 10:33:50.865 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: viewGroup 0
03-21 10:33:50.865 9701-9701/com.jiang.eventtest D/-EVENT: onInterceptTouchEvent: viewGroup 0
03-21 10:33:50.865 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: View 0
03-21 10:33:50.865 9701-9701/com.jiang.eventtest D/-EVENT: onTouch: View 0
03-21 10:33:50.865 9701-9701/com.jiang.eventtest D/-EVENT: onTouchEvent: View 0
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: viewGroup 2
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: onInterceptTouchEvent: viewGroup 2
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: View 2
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: onTouch: View 2
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: onTouchEvent: View 2
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: viewGroup 1
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: onInterceptTouchEvent: viewGroup 1
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: View 1
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: onTouch: View 1
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: onTouchEvent: View 1
03-21 10:33:50.965 9701-9701/com.jiang.eventtest D/-EVENT: onClick: View
如果点击空白部分:
03-21 10:44:13.745 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: viewGroup 0
03-21 10:44:13.745 9701-9701/com.jiang.eventtest D/-EVENT: onInterceptTouchEvent: viewGroup 0
03-21 10:44:13.745 9701-9701/com.jiang.eventtest D/-EVENT: onTouch: viewGroup 0
03-21 10:44:13.745 9701-9701/com.jiang.eventtest D/-EVENT: onTouchEvent: viewGroup 0
03-21 10:44:13.795 9701-9701/com.jiang.eventtest D/-EVENT: dispatchTouchEvent: viewGroup 1
03-21 10:44:13.795 9701-9701/com.jiang.eventtest D/-EVENT: onTouch: viewGroup 1
03-21 10:44:13.795 9701-9701/com.jiang.eventtest D/-EVENT: onTouchEvent: viewGroup 1
03-21 10:44:13.795 9701-9701/com.jiang.eventtest D/-EVENT: onClick: viewGroup
重上面可以看到调用的顺序为:
ViewGroup
---dispatchTouchEvent-->onInterceptTouchEvent-->
View
------>dispatchTouchEvent-->onTouch-->onTouchEvent
二.源码分析
1.dispatchTouchEvent
分段贴出重要代码:
首先提示,需要特别注意几个变量
mFirstTouchTarget、newTouchTarget以及alreadyDispatchedToNewTouchTarget
Part 1
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = false;
//检查是否符合安全策略,即窗口是否可见,是返回true
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
//这里MotionEvent.ACTION_MASK为0xff,任何数与它&都为其本身
final int actionMasked = action & MotionEvent.ACTION_MASK;
//当为ACTION_DOWN时,重置状态
// Handle an initial down.
if (actionMasked == MotionEvent.ACTION_DOWN) {
cancelAndClearTouchTargets(ev);
resetTouchState();
}
首先,在动作为ACTION_DOWN时,会调用cancelAndClearTouchTargets与resetTouchState
Part 1.1
private void cancelAndClearTouchTargets(MotionEvent event) {
if (mFirstTouchTarget != null) {
...
for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
resetCancelNextUpFlag(target.child);
dispatchTransformedTouchEvent
(event, true, target.child, target.pointerIdBits);
}
clearTouchTargets();
...
}
}
当mFirstTouchTarget不为null时会调用dispatchTransformedTouchEvent
Part 1.1.1
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel为true,所以在这里向本身或者,
View child, int desiredPointerIdBits) {
final boolean handled;
// Canceling motions is a special case. We don't need to perform any transformations
// or filtering. The important part is the action, not the contents.
final int oldAction = event.getAction();
if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
event.setAction(MotionEvent.ACTION_CANCEL);
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
return handled;
}
...
}
在dispatchTransformedTouchEvent中,调用传递的参数cancel为true,所以在这里向本身或者子View传递ACTION_CANCEL
Part 1.1.2
private void clearTouchTargets() {
TouchTarget target = mFirstTouchTarget;
if (target != null) {
do {
TouchTarget next = target.next;
target.recycle();
target = next;
} while (target != null);
mFirstTouchTarget = null;
}
}
clearTouchTargets执行的最后结果就是mFirstTouchTarget = null。
Part 1.2
private void resetTouchState() {
clearTouchTargets();
resetCancelNextUpFlag(this);
...
}
在Part 1总最后的执行救过就是在ACTION_DOWN时,先重置ViewGroup的状态,使是mFirstTouchTarget = null。
然后回到dispatchTouchEvent中
Part 2
//检查拦截状态
// Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
//允许拦截的话调用onInterceptTouchEvent检查是否拦截
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;
}
在为ACTION_DOWN时,或者mFristTouchTarget=null时判断拦截
其中disallowIntercept表示“不允许拦截”,
当disallowIntercept为true时通过onInterceptTouchEvent判断是否拦截事件
Part 2.1
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
默认是不拦截的,可以通过重写该方法拦截相应的事件。
在Part 2中检查了是否对事件进行拦截,设置intercepted的值。
// If intercepted, start normal event dispatch. Also if there is already
// a view that is handling the gesture, do normal event dispatch.
if (intercepted || mFirstTouchTarget != null) {
ev.setTargetAccessibilityFocus(false);
}
//检查是否取消
// Check for cancelation.
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL;
// Update list of touch targets for pointer down, if needed.
final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
Part 3
将newTouchTarget = null与alreadyDispatchedToNewTouchTarget = false
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
Part 4
if (!canceled && !intercepted) {
...
//当ACTION_DOWN时
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) {
Part 4.1
Part 4.2
}
...
}
}
当ACTION_DOWN时,此时newTouchTarget进入if内,
Part 4.1
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 = 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.
//检查该View是否是获取当前焦点的View,不是continue
if (childWithAccessibilityFocus != null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus = null;
i = childrenCount - 1;
}
//如果该View是不可见,或者触点不在该View内,continue
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
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);
倒序循环找到触点所在的View,若未找到则跳出循环,不会执行循环体中后续分发事件的部分
Part 4.1.1
private TouchTarget getTouchTarget(View child) {
for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
if (target.child == child) {
return target;
}
}
return null;
}
因为在经过Part 1之后,mFirstTouchTarget=null,则newTouchTarget = null;
Part 4.2
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;
}
// 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();
}
这里重点是dispatchTransformedTouchEvent和addTouchTarget,
Part 4.2.1
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) {
...
}
final int oldPointerIdBits = event.getPointerIdBits();
final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;
if (newPointerIdBits == 0) {
return false;
}
final MotionEvent transformedEvent;
if (newPointerIdBits == oldPointerIdBits) {
...
} else {
...
}
// Perform any necessary transformations and dispatch.
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent);
} else {
final float offsetX = mScrollX - child.mLeft;
final float offsetY = mScrollY - child.mTop;
transformedEvent.offsetLocation(offsetX, offsetY);
if (! child.hasIdentityMatrix()) {
transformedEvent.transform(child.getInverseMatrix());
}
handled = child.dispatchTouchEvent(transformedEvent);
}
// Done.
transformedEvent.recycle();
return handled;
}
传递的参数是dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)
Part 4.2.2
private TouchTarget addTouchTarget(View child, int pointerIdBits) {
TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
target.next = mFirstTouchTarget;
mFirstTouchTarget = target;
return target;
}
根据事件的处理结果来为mFirstTouchTarget赋值,并且newTouchTarget = mFirstTouchTarget。
重Part 4中可知,当ACTION_DOWN,且事件不被拦截时,将事件分发下去,如果触点所在区域是有可操作的View,则事件分发给子View,如无则跳出循环不会执行后面事件的分发,如果事件被处理为mFirstTouchTarget赋值并且newTouchTarget = mFirstTouchTarget,alreadyDispatchedToNewTouchTarget = true,否则mFirstTouchTarget仍为null。
Part 5
// Dispatch to touch targets.
if (mFirstTouchTarget == null) {
Part 5.1
} else {
Part 5.2
}
Part 5.1
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
如果前面Part 4.1 未能找到触点对应的子View,mFirstTouchTarget == null 则会进入到Part 5.1。事件将会交于自己处理。
当前面的ACTION_DOWN事件被拦截,mFirstTouchTarget为null,在dispatchTransformedTouchEvent中将ACTION_DOWN事件交给自己处理,
并且由于前面的ACTION_DOWN被拦截,mFirstTouchTarget仍为null,所以后续的ACTION仍然会被自己处理。
或者当分发给子View的事件未被消费,mFirstTouchTarget为null,事件仍会交于自己处理。
Part 5.2
// 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;
}
在这里,如果是执行完前面ACTION_DOWN中的内容之后,执行,newTouchTarget = mFirstTouchTarget,并且alreadyDispatchedToNewTouchTarget=true,所以执行到handled = true。
如果是其它ACTION,在执行过ACTION_DOWN里的内容之后,会直接进else,将事件分发下去。
Part 6
// Update list of touch targets for pointer up or cancel, if needed.
if (canceled
|| actionMasked == MotionEvent.ACTION_UP
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
resetTouchState();
} else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
final int actionIndex = ev.getActionIndex();
final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
removePointersFromTouchTargets(idBitsToRemove);
}
在这里主要是在ACTION_UP和取消时将状态回置。包括将mFirstTouchTarget = null;
三.总结
流程:
- 每次ACTION,alreadyDispatchedToNewTouchTarget=false
- ACTION_DOWN时,对ViewGroup进行初始化,mFirstTouchTarget=null。
- 如果不对ACTION_DOWN进行拦截,会先查找触点是否有合适的子View,如果有,则将事件分发给子View。
- 如果子View消费了事件,mFirstTouchTarget!=null,alreadyDispatchedToNewTouchTarget=true,返回true,后续事件如果被拦截,则首先事件会交于ViewGroup处理,然后对应的子View会被分配ACTION_CANCEL.
- 如果子View没有消费事件,mFirstTouchTarget=null,alreadyDispatchedToNewTouchTarget=false,首先ACTION_DOWN会交于ViewGroup处理,并且后续事件也将交于ViewGroup处理。
- 如果过没有找到对应的子View,mFirstTouchTarget=null,ACTION_DOWN将会交于ViewGroup处理。并且后续事件仍会交于ViewGroup处理
- 如果对ACTION_DOWN进行拦截,事件将交由ViewGroup自己处理,调用super.dispatchTouchEvent,此时mFirstTouchTarget=null,在后续的ACTION事件仍会交由ViewGroup处理。
- 最后ACTION_UP时,回置ViewGroup的状态,mFirstTouchTarget=null。
总结:
- 若果对ACTION_DOWN进行拦截,ACTION_DOWN及后续事件都将会被ViewGroup自己处理;
- 如果子View消费了ACTION_DOWN,后续事件如果不被拦截,继续交于子View,如果被拦截则事件交由ViewGroup处理,子View被分配ACTION_CANCEL;
- 如果子View为消费ACTION_DOWN,ACTION_DOWN以及后续事件都将给予ViewGroup处理。