美文网首页
Android @ 事件分发机制

Android @ 事件分发机制

作者: sneider | 来源:发表于2017-08-29 17:08 被阅读0次

    dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent

    MotionEvent ev;//down or move or up or others...
    viewgroup.dispatchTouchEvent(ev);
    
    public boolean dispatchTouchEvent(MotionEvent ev){
        boolean isConsumed = false;
        if(onInterceptTouchEvent(ev)){
            isCousumed = this.onTouchEvent(ev);
        }else{
            isConsumed = childView.dispatchTouchEvent(ev);
        }
        return isConsumed;
    }
    

    ViewGroup 中的相关源码:

    public boolean dispatchTouchEvent(MotionEvent ev) {
        ......
        // Check for interception.
        final boolean intercepted;
        if (actionMasked == MotionEvent.ACTION_DOWN
                || mFirstTouchTarget != null) {
            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;
        }
        ......
    }
    

    参考:

    1. 十分钟彻底弄明白 View 事件分发机制
    2. 图解 Android 事件分发机制

    未完成...

    相关文章

      网友评论

          本文标题:Android @ 事件分发机制

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