上篇文章已经分析了Android
的Touch
事件分发。如果没看的建议先看一下。Android View的Touch事件分发。
接下来我们开始写几种场景,得出一个初步的执行顺序,然后我们按照这个顺序开始分析。
首先我们自定义一个ViewGroup
和一个View
,然后重写相关事件进行打印:
场景一:正常返回super
,TouchView
设置click
和onTouchListener
事件(onTouch
返回false
)
这时候我们点击一下TouchView
,触发事件:
可以看到触发的DOWN MOVE UP
事件顺序都为:
ViewGroup.dispatchTouchEvent -> ViewGroup.onInterceptTouchEvent -> View.dispatchTouchEvent -> View.onTouch -> View.onTouchEven
只是在UP
事件的时候最后多了一个click
事件。
场景二:在场景一
的基础上取消TouchView
的onClick
事件
这时候发现除了,执行的顺序变为了:
ViewGroup.dispatchTouchEvent -> ViewGroup.onInterceptTouchEvent -> View.dispatchTouchEvent -> View.onTouch -> View.onTouchEven->ViewGroup.onTouchEven
并且只有DOWN
事件,其他事件就没有了。
场景三:在场景二
的基础上TouchViewGroup
的onInterceptTouchEvent
里面返回true
这个时候就只有DOWN
事件,并且顺序为:
ViewGroup.dispatchTouchEvent -> ViewGroup.onInterceptTouchEvent -> ViewGroup.onTouchEvent
接下来我们通过源码来分析:
首先从ViewGroup
的dispatchTouchEvent
入手
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//...
boolean handled = false;
//...
//1.取消之前的手势
// Handle an initial down.
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();
}
//2.判断是否拦截
// Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) { //DOWN
//父类是否拦截 getParent().requestDisallowInterceptTouchEvent();来改变值
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;
}
//....
//3.0 如果是不取消不拦截为down,并且dispatchTransformedTouchEvent返回为true的时候会为 mFirstTouchTarget赋值
// 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;
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
//3.1 如果不取消并且不拦截的情况下,
if (!canceled && !intercepted) {
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {// 3.2 DOWN的时候
//...
if (newTouchTarget == null && childrenCount != 0) {
//...
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {//3.3 反序for循环,为了先拿到上层的view
//...
//3.4 拿到child
final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
//...
//3.5 根据child给newTouchTarget赋值 DOWN的时候因为 mFirstTouchTarget==null 所以进不去 返回的是null
newTouchTarget = getTouchTarget(child);
}
//...
//3.6. 执行操作 是执行自己的dispatchTouchEvent还是child的dispatchTouchEvent
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
//...
//3.7 子View如果返回true添加一个newTouchTarget 并且为mFirstTouchTarget赋值
newTouchTarget = addTouchTarget(child, idBitsToAssign);
//....
}
}
}
}
//...
// Dispatch to touch targets.
if (mFirstTouchTarget == null) {//执行自身的dispatchTouchEvent
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {// mFirstTouchTarget已经赋值
// 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) {//执行完3.7操作的
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;
}
}
return handled;
}
/**
* Cancels and clears all touch targets.
*/
private void cancelAndClearTouchTargets(MotionEvent event) {
if (mFirstTouchTarget != null) {
boolean syntheticEvent = false;
if (event == null) {
final long now = SystemClock.uptimeMillis();
event = MotionEvent.obtain(now, now,
MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
syntheticEvent = true;
}
for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
resetCancelNextUpFlag(target.child);
dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
}
clearTouchTargets();
if (syntheticEvent) {
event.recycle();
}
}
}
//清楚所有的TouchTarget
/**
* Clears all touch targets.
*/
private void clearTouchTargets() {
TouchTarget target = mFirstTouchTarget;
if (target != null) {
do {
TouchTarget next = target.next;
target.recycle();
target = next;
} while (target != null);
mFirstTouchTarget = null;
}
}
//根据childVie得到TouchTarget
/**
* Gets the touch target for specified child view.
* Returns null if not found.
*/
private TouchTarget getTouchTarget(@NonNull View child) {
// DOWN的时候因为 mFirstTouchTarget==null 所以进不去 返回的是null
for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
if (target.child == child) {
return target;
}
}
return null;
}
/**
* Transforms a motion event into the coordinate space of a particular child view,
* filters out irrelevant pointer ids, and overrides its action if necessary.
* If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
*/
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
//伪代码
final boolean handled;
if (child == null) {//执行View.dispatchTouchEvent 也就是自己的dispatchTouchEvent
handled = super.dispatchTouchEvent(event);
} else {//执行child的dispatchTouchEvent
handled = child.dispatchTouchEvent(event);
}
return handled;
}
//添加TouchTarget 并且给mFirstTouchTarget赋值
/**
* Adds a touch target for specified child to the beginning of the list.
* Assumes the target child is not already present.
*/
private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
target.next = mFirstTouchTarget;
mFirstTouchTarget = target;
return target;
}
DOWN
的时候,从注释和方法名可以看出,会调用cancelAndClearTouchTargets
,然后在调用clearTouchTargets
使mFirstTouchTarget = null
用来废弃上一次的触摸手势。(mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0
来判断,在这里可以通过getParent().requestDisallowInterceptTouchEvent(boolean disallowIntercept)
来改变值,如果上面为判断为false
再通过onInterceptTouchEvent
的返回值来确定,这个函数默认情况下返回false
。DOWN
事件的时候,就会对childView
一个反序的for
循环来遍历,并且执行dispatchTransformedTouchEvent
操作,这个操作用来执行dispatchTouchEvent
,如果childView
是null
的话将执行View.dispatchTouchEvent
,也就是自己的dispatchTouchEvent
,反之执行childView
的dispatchTouchEvent
,如果执行dispatchTransformedTouchEvent
返回的值是true
那么将会调用addTouchTarget()
为这个childView
生成一个TouchTarget
并且执行mFirstTouchTarget = target
将之赋值于mFirstTouchTarget
,然后跳出for
循环遍历,这个mFirstTouchTarget
是用于判断后续的事件move up
等事件是否进行拦截触发函数。mFirstTouchTarget
是否为null
,如果是DOWN
事件,不拦截不取消并且dispatchTransformedTouchEvent
返回了true
,那么将会不进入这个判断,如果不是,那么将会在这执行自身的dispatchTouchEvent
函数并且将返回值赋于handled
返回。进入else
语句,在里面将其mFirstTouchTarget
进行next
遍历,里面的if
语句则是DOWN
事件下的dispatchTransformedTouchEvent
返回true
的情况,直接将其赋值,然后返回,里面的else
语句则是,调用dispatchTransformedTouchEvent
,然后将其返回值返回。到这里,ViewGroups事件分发源码的流程就分析了,我们根据这个来说说上面的场景。
场景一:我们在TouchViewGroup
的dispatchTouchEvent
正常返回super
,DOWN
事件先触发TouchViewGroup
的dispatchTouchEvent
,然后就执行onInterceptTouchEvent
是否拦截,onInterceptTouchEvent
返回的是super
,也就是false
,所以就会通过dispatchTransformedTouchEvent
来执行TouchView
的dispatchTouchEvent
,后面就是View
的Touch
事件分发了,View
流程将会按照dispatchTouchEvent->onTouchListener - > onTouchEvent
的顺序执行,因为设置了点击事件,所以在这里就返回了true
,这个时候就会通过addTouchTarget()
给mFirstTouchTarget
赋值,下面就直接返回了true
。然后在MOVE
和UP
事件的时候,也是首先执行dispatchTouchEvent
,调用super
然后调用onInterceptTouchEvent
询问是否拦截,还是false
,但是这里因为不是DOWN
事件,所以就不会进入判断对其childView
反遍历,因为在DOWN
的时候mFirstTouchTarget
赋值了,所以这时候进入第4步的else
语句里面,这时候就对其遍历执行dispatchTransformedTouchEvent
,也就是dispatchTouchEvent
,然后将其返回。
场景2:我们取消了点击事件,那么在DOWN
的时候就不会给mFirstTouchTarget
赋值,这个时候将会进入第4步的if
判断里面,直接调用dispatchTransformedTouchEvent
,所以事件就不会有拦截,最终返回false
,所以后续将不会接受到任何事件
场景3:我们在TouchViewGroup
的时候是在onInterceptTouchEvent
返回true
,所以我们intercepted=true
,这时候就不会给mFirstTouchTarget
赋值,这个时候就调用自身的dispatchTransformedTouchEvent
,同样的返回false
,后续将不会接受到事件。
通过源码的角度我们也知道了为什么会这么执行,初步有点模糊,我们需要通过项目慢慢的来完善对它的认知。希望对大家有所帮助。
参考链接: http://www.jianshu.com/p/98d1895c409d http://www.jianshu.com/p/e99b5e8bd67b