当ViewGroup不拦截事件时,事件将会向子View传递,那到底是怎样把事件传递到子View的呢?接下来将分析这个流程,先看代码
ViewGroup事件分发
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = getAndVerifyPreorderedIndex(
childrenCount, i, customOrder);//得到View的索引
final View child = getAndVerifyPreorderedView(
preorderedList, children, childIndex);////得到View
//判断该View是否可以接收事件,而且点击的左边在此View内
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
//........................
newTouchTarget = getTouchTarget(child);//得到了Touch的子View
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的dispatchTouchEvent
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;
}
}
上面代码的主要逻辑大概是,for循环遍历所有的view,找到点击坐标所在区域的子View且这个View可以被点击,就将事件交给它处理,canViewReceivePointerEvents(child)方法可以判断View是否允许被点击。
接下来会调用
dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)
此方法就是调用了子View的dispatchTouchEvent,看代码
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
此时如果子View的dispatchTouchEvent返回true,会执行下面的代码,看到了mFirstTouchTarget被赋值,同时跳出了for循环。
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
/**
* 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;
}
如果遍历完所有的子元素后事件都没有被处理,有两种情况:ViewGroup没有子View或者子View处理了点击事件,但是dispatchTouchEvent返回了false,看代码
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {
}
此时mFirstTouchTarget为空,会调用dispatchTransformedTouchEvent上面分析过此方法最终会调用
handled = super.dispatchTouchEvent(event);
因为ViewGroup的是继承子View,所以会调用View的dispatchTouchEvent,点击事件交给了View处理。
View的事件处理
根据上面的分析可以得知事件分发到了View的dispatchTouchEvent方法内,下面看代码。
if (onFilterTouchEventForSecurity(event)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//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;
}
}
可以看到会先判断是否设置了onTouchListener,如果设置了并且onTouchListener的onTouch方法返回了true,此时result=true,不会再执行onTouchEvent方法,可以得出结论 onTouchListener的优先级高于onTouchEvent,这样做的好处是可以在外界处理点击事件。
if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
// A disabled view that is clickable still consumes the touch
// events, it just doesn't respond to them.
return (((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);
}
从上面的注释可以知道,当一个View为不可用状态时,仍然会消费事件只是没有响应(没有执行performClick)。
接下来看onTouchEvent的ACTION_UP的事件处理
if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
(viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
switch (action) {
case MotionEvent.ACTION_UP:
boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (prepressed) {
// The button is being released before we actually
// showed it as pressed. Make it show the pressed
// state now (before scheduling the click) to ensure
// the user sees it.
setPressed(true, x, y);
}
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
// This is a tap, so remove the longpress check
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}
break;
return true;
}
可以看到View的CLICKABLE和 LONG_CLICKABDLE有一个为true,就会消费点击事件,如果设置了点击事件performClick会触发onClickListener。
View的LONG_CLICKABLE默认为false,CLICKABLE属性根据默认值和具体的View有关,例如Button是可点击的为true,TextView不可以点击的通过
seOnClickListener会讲CLICKABLE改成true。
到此Android事件分发机制就完啦。