Android事件分发源码剖析(一)之事件处理

主要写三个方面:

1,事件分发分发的是什么事件(ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL(事件被上层拦截的时候触发))

2,事件是怎么处理(通过对onTouch,onclick之间的关系解析)

举一个案例,一个按钮同时监听onTouch,onclick,在onTouch()返回true,这时发现onclick没有被触发。通过这个现象我看了下源码中的事件处理流程,可以在源码中找到以下的代码段。

    public boolean dispatchTouchEvent(MotionEvent event) {
        // If the event should be handled by accessibility focus first.
        if (event.isTargetAccessibilityFocus()) {
            // We don't have focus or no virtual descendant has it, do not handle the event.
            if (!isAccessibilityFocusedViewOrHost()) {
                return false;
            }
            // We have focus and got the event, then use normal event dispatch.
            event.setTargetAccessibilityFocus(false);
        }
        boolean result = false;

        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onTouchEvent(event, 0);
        }

        final int actionMasked = event.getActionMasked();
        if (actionMasked == MotionEvent.ACTION_DOWN) {
            // Defensive cleanup for new gesture
            stopNestedScroll();
        }

        if (onFilterTouchEventForSecurity(event)) {
            if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
                result = true;
            }
            //noinspection SimplifiableIfStatement
            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;
            }
        }

        if (!result && mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
        }

        // Clean up after nested scrolls if this is the end of a gesture;
        // also cancel it if we tried an ACTION_DOWN but we didn't want the rest
        // of the gesture.
        if (actionMasked == MotionEvent.ACTION_UP ||
                actionMasked == MotionEvent.ACTION_CANCEL ||
                (actionMasked == MotionEvent.ACTION_DOWN && !result)) {
            stopNestedScroll();
        }

        return result;
    }

这个现象造成的原因就在上面,我把主要的语句摘录出来

            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;
            }

可以看到当onTouch()返回true时,result会被赋值为true,而按钮的的onclick()其实是在onTouchEvent()执行的,所以不会执行到,这就是onclick()没有执行的原因了。(如果大家想验证下我们可以看看onTouchEvent()这个函数的实现。在case MotionEvent.ACTION_UP:可以找到performClick();这个函数,我们再次进入这个函数就可以看到我们的我们的li.mOnClickListener.onClick(this);)

也就是说我们事件具体的处理其实主要在View的dispatchTouchEvent()和View的onTouchEvent().这个时候如果我们要进入onclick()那么我们就把onTouch()返回false就可以啦。而如果我们想让onTouch()返回true,还想触发onclick(),那么我们可以在需要的动作中手动调用v.performClick();

3,分析源码,了解事件冲突的原因以及解决

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值