Touch事件分发逻辑

Touch事件分发逻辑_第1张图片
Touch事件分发逻辑_第2张图片

仔细看下就可以发现,只要onTouchEvent()有一次返回true,那么不管之后的返回是不是true都会继续进入onTouchEvent(),而不会向下分发。

补充
  1. 进入 View 的 onTouch 之前会先判断是否有设置 TouchListener,有则直接调用 TouchListener 的 onTouch
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;
}
  1. ViewGroup 如果被设置成 disallowIntercept 则不会进入 onInterceptTouchEvent
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;
}
  1. 但是在此之前还有段代码,如果是 ACTION_DOWN 则会移除 disallowIntercept 的标志,这就代表每次 ACTION_DOWN 都必定进入 onInterceptTouchEvent
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();
}

private void resetTouchState() {
    clearTouchTargets();
    resetCancelNextUpFlag(this);
    mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
    mNestedScrollAxes = SCROLL_AXIS_NONE;
}

你可能感兴趣的:(Touch事件分发逻辑)