事件分发源码解析+总结
2021-06-21 本文已影响0人
allsunny
1、View 只有消费了 ACTION_DOWN 事件,才能接收到后续的事件(可点击控件会默认消费所有事件),并且会将后续所有事件传递过来,不会再传递给其他 View,除非上层 View 进行了拦截。
结论:如果在自定义View中重写了onTouchEvent(),且case:MotionEvent.ACTION_DOWN中返回了false,则onClick()无法生效,因为onClick在MotionEvent.ACTION_UP中。
2、parent.requestDisallowInterceptTouchEvent()决定了是否进入ViewGroup.onInterceptTouchEvent()方法。
代码解释:
boolean ViewGroup.dispatchTouchEvent(){
// Handle an initial down. 仅对于ACTION_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(); // 重置mGroupFlags对FLAG_DISALLOW_INTERCEPT的状态,可以理解成把disallowIntercept重置为false->intercepted = false;
}
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; // disallowIntercept默认false
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false; // disallowIntercept = false->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;
}
// 如果拦截了。则mFirstTouchTarget == null,直接进入ViewGroup.onTouchEvent()处理事件阶段
if (!canceled && !intercepted) {
// 只有down和move事件
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE){
/**
* 如果View不消费 ACTION_DOWN 事件(onTouchEvent()的ACTION_DOWN返回false),则dispatchTransformedTouchEvent()返回false,
* 则mFirstTouchTarget = null,后续事件的dispatchTransformedTouchEvent()方法的child参数为null,不会将事件传给子View
**/
if (dispatchTransformedTouchEvent()){
// addTouchTarget()中给mFirstTouchTarget赋值
newTouchTarget = addTouchTarget(child, idBitsToAssign);
}
}
}
// 如果拦截了。则mFirstTouchTarget == null,直接进入ViewGroup.onTouchEvent()处理事件阶段
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
// child参数为null,相当于ViewGroup.onTouchEvent()
handled = dispatchTransformedTouchEvent(ev, canceled, child:null,
TouchTarget.ALL_POINTER_IDS);
} else {
if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)) {
handled = true;
}
}
return handled;
}
---------------------ViewGroup.dispatchTransformedTouchEvent---------------------------------------
boolean ViewGroup.dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, child, int desiredPointerIdBits) {
// 没有 child,将当前 ViewGroup 当作普通的 View 处理。
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent); //即ViewGroup.onTouchEvent() -> View.onTouchEvent()
}
return handled;
}
------------------View.dispatchTouchEvent----------------------------
public boolean dispatchTouchEvent(MotionEvent event) {
if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED // 注意ENABLED标志
&& mOnTouchListener.onTouch(this, event)){ //处理
return true;
}
return onTouchEvent(event)); //处理
}
-------------源码补充ViewGroup.requestDisallowInterceptTouchEvent-------
@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
// We're already in this state, assume our ancestors are too
return;
}
if (disallowIntercept) {
// 0|0=0 0|1=1
// true,则mGroupFlags被FLAG_DISALLOW_INTERCEPT置位。
mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
} else {
// 0&0=0 0&1=0 1&0=0 1&1=1
// 先取反(优先级高),再与。
// 重置mGroupFlags对FLAG_DISALLOW_INTERCEPT的状态,可以理解成把disallowIntercept重置为false
// 即resetTouchState()
mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
}
// Pass it up to our parent
if (mParent != null) {
mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
}
}