事件很多 梳理一下(事件分发的那些事)

什么是事件

点击事件(Touch事件)

事件类型
image.png

怎么描述事件: MotionEvent

事件分发的本质: 将点击事件(MotionEvent)传递到某个具体的View & 处理的整个过程

事件分发的过程

事件的传递 activity ->viewgroup ->view


image.png

事件分发方法

dispatchTouchEvent() 、onInterceptTouchEvent()和onTouchEvent()


image.png

下面我们针对源码 对这几个方法进行分析

Activity的事件分发机制

image.png

getWindow().superDispatchTouchEvent(ev)

window只有一个实现类PhoneWindow,所以我们看PhoneWindow里边是怎么实现的


image.png

mDecor是DecorView 我们点进去看DecorView会发现它继承FrameLayout


image.png

即将事件传递到ViewGroup去处理。
我们总结一下就是
/**
 * 源码分析:Activity.dispatchTouchEvent()
 */
public boolean dispatchTouchEvent(MotionEvent ev) {

    // 仅贴出核心代码

    // ->>分析1
    if (getWindow().superDispatchTouchEvent(ev)) {

        return true;
        // 若getWindow().superDispatchTouchEvent(ev)的返回true
        // 则Activity.dispatchTouchEvent()就返回true,则方法结束。即 :该点击事件停止往下传递 & 事件传递过程结束
        // 否则:继续往下调用Activity.onTouchEvent

    }
    // ->>分析3
    return onTouchEvent(ev);
}

/**
 * 分析1:getWindow().superDispatchTouchEvent(ev)
 * 说明:
 *     a. getWindow() = 获取Window类的对象
 *     b. Window类是抽象类,其唯一实现类 = PhoneWindow类
 *     c. Window类的superDispatchTouchEvent() = 1个抽象方法,由子类PhoneWindow类实现
 */
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {

    return mDecor.superDispatchTouchEvent(event);
    // mDecor = 顶层View(DecorView)的实例对象
    // ->> 分析2
}

/**
 * 分析2:mDecor.superDispatchTouchEvent(event)
 * 定义:属于顶层View(DecorView)
 * 说明:
 *     a. DecorView类是PhoneWindow类的一个内部类
 *     b. DecorView继承自FrameLayout,是所有界面的父类
 *     c. FrameLayout是ViewGroup的子类,故DecorView的间接父类 = ViewGroup
 */
public boolean superDispatchTouchEvent(MotionEvent event) {

    return super.dispatchTouchEvent(event);
    // 调用父类的方法 = ViewGroup的dispatchTouchEvent()
    // 即将事件传递到ViewGroup去处理,详细请看后续章节分析的ViewGroup的事件分发机制

}
// 回到最初的分析2入口处

/**
 * 分析3:Activity.onTouchEvent()
 * 调用场景:当一个点击事件未被Activity下任何一个View接收/处理时,就会调用该方法
 */
public boolean onTouchEvent(MotionEvent event) {

    // ->> 分析5
    if (mWindow.shouldCloseOnTouch(this, event)) {
        finish();
        return true;
    }

    return false;
    // 即 只有在点击事件在Window边界外才会返回true,一般情况都返回false,分析完毕
}

/**
 * 分析4:mWindow.shouldCloseOnTouch(this, event)
 * 作用:主要是对于处理边界外点击事件的判断:是否是DOWN事件,event的坐标是否在边界内等
 */
public boolean shouldCloseOnTouch(Context context, MotionEvent event) {

    if (mCloseOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN
        && isOutOfBounds(context, event) && peekDecorView() != null) {

        // 返回true:说明事件在边界外,即 消费事件
        return true;
    }

    // 返回false:在边界内,即未消费(默认)
    return false;
}

ViewGroup的事件分发

刚刚分析了activity的事件分发,最后会分发到ViewGroup 实现了activity-->ViewGroup

public boolean dispatchTouchEvent(MotionEvent ev) {


        // 分析1:ViewGroup每次事件分发时,都需调用onInterceptTouchEvent()询问是否拦截事件
        //我们可以通过改变disallowIntercept的值来让ViewGroup不对事件进行拦截 是否禁用事件拦截的功能(默认是false),
        // 可通过调用requestDisallowInterceptTouchEvent()修改
        // Check for interception.
        final boolean intercepted;
        if (actionMasked == MotionEvent.ACTION_DOWN
                || mFirstTouchTarget != null) {
            final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
            if (!disallowIntercept) {
                //onInterceptTouchEvent 默认返回 false,不拦截
                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;
        }

        //没有取消也没有拦截,则分发给子View
        if (!canceled && !intercepted) {
                //如果 Touch 状态为空并且子 View 个数不为 0
                if (newTouchTarget == null && childrenCount != 0) {
                    final float x = ev.getX(actionIndex);
                    final float y = ev.getY(actionIndex);
                    // Find a child that can receive the event.
                    // Scan children from front to back.
                    final ArrayList<View> preorderedList = buildTouchDispatchChildList();
                    final boolean customOrder = preorderedList == null
                            && isChildrenDrawingOrderEnabled();
                    final View[] children = mChildren;
                    //循环查找
                    for (int i = childrenCount - 1; i >= 0; i--) {
                        //分发事件,记录状态
                        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;
                        }

}

总结如下


image.png

View的事件分发机制

下面我们看源码分析一下

                    public boolean dispatchTouchEvent(MotionEvent event) {

                        if (onFilterTouchEventForSecurity(event)) {
                            if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
                                result = true;
                            }
                            //noinspection SimplifiableIfStatement
                            ListenerInfo li = mListenerInfo;
                            //判断是否注册 OnTouchListener
                            //判断 OnTouchListener 中 onTouch 是否返回 true
                            //当 onTouch 返回 true 时,事件被 onTouch 处理了,不会执行 onTouchEvent 方法,说明 onTouch 的优先级高于 onTouchEvent
                            if (li != null && li.mOnTouchListener != null
                                    && (mViewFlags & ENABLED_MASK) == ENABLED
                                    && li.mOnTouchListener.onTouch(this, event)) {
                                result = true;
                            }

                            if (!result && onTouchEvent(event)) {
                                result = true;
                            }
                        }

                    }

onTouchEvent

                    public boolean onTouchEvent(MotionEvent event) {
...
                        if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
                            switch (action) {
                                case MotionEvent.ACTION_UP:
            ...
                                    //如果不能点击则直接返回
                                    if (!clickable) {
                                        removeTapCallback();
                                        removeLongPressCallback();
                                        mInContextButtonPress = false;
                                        mHasPerformedLongPress = false;
                                        mIgnoreNextUpEvent = false;
                                        break;
                                    }
                                    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)) {
                                                performClickInternal();
                                            }
                                        }
                                    }
            ...
                                    break;
                                case MotionEvent.ACTION_DOWN:
            ...
                                    break;
                                case MotionEvent.ACTION_CANCEL:
            ...
                                    break;
                                case MotionEvent.ACTION_MOVE:
            ...
                                    break;
                            }
                        }
...
                    }

View 事件分发总结
1.事件分发首先会调用dispatchTouchEvent()
2.事件分发执行的顺序为 onTouch -> onTouchEvent -> onClick,可以理解成 优先级 onTouch > onTouchEvent > onClick
3.当 OnTouchListener 为 true 时,会消费当前事件,只会执行 onTouch 方法,不会传递到 onTouchEvent 方法。当 OnTouchListener 为 false 时,则会将事件传递
onClick 事件触发的前提条件是可点击的,onClick 触发在 onTouchEvent 的UP 事件中

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容