Touch事件分发逻辑

作者: 风风风筝 | 来源:发表于2016-06-30 17:12 被阅读365次

    仔细看下就可以发现,只要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事件分发逻辑

        本文链接:https://www.haomeiwen.com/subject/fmcfjttx.html