android 事件分发

在android 中事件的分发 是执行

 /**
     * Pass the touch screen motion event down to the target view, or this
     * view if it is the target.
     *
     * @param event The motion event to be dispatched.
     * @return True if the event was handled by the view, false otherwise.
     */
    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)) {
            //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;
    }

我门在代码中实现 setOnTouchListener 在onTouch() 方法中实现 返回 true

注意代码

if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {
                result = true;
                }

所以 当返回为true 的时候,当前的事件是自己处理,

1、事件入口是dispatchTouchEvent(),它会先执行注册的onTouch监听,如果一切顺利的话,接着执行onTouchEvent,在onTouchEvent里会执行onClick监听。
2、无论是dispatchTouchEvent还是onTouchEvent,如果返回true表示这个事件已经被消费、处理了,不再往下传了。在dispathTouchEvent的源码里可以看到,如果onTouchEvent返回了true,那么它也返回true。如果dispatch***在执行onTouch监听的时候,onTouch返回了true,那么它也返回true,这个事件提前被onTouch消费掉了就不再执行onTouchEvent了,更别说onClick监听了。**
3、我们通常在onTouch监听了设置图片一旦被触摸就改变它的背景、透明度之类的,这个onTouch表示事件的时机。而在onClick监听了去具体干某些事。

  View.setOnTouchListener(new View.OnTouchListener() {
                                private boolean mMoved = false;
                                private float startX;
                                private float startY;
                                @Override
                                public boolean onTouch(View v, MotionEvent event) {
                                    int width = v.getMeasuredWidth();
                                    int x = (int) event.getX();
                                    int y = (int) event.getY();
                                    int minWitdth = (int) (width * 0.75f);
                                    if (x > minWitdth) {
                                        return false;
                                    }
                                    if (MotionEvent.ACTION_DOWN == event.getAction()) {
                                        mMoved = false;
                                        startX = x;
                                        startY = y;
                                        return true;
                                    }
                                    if (MotionEvent.ACTION_MOVE == event.getAction()) {
                                        int slop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop() * 2;
                                        if ((x - startX) * (x - startX) + (y - startY) * (y - startY) > slop) {
                                            mMoved = true;
                                        }
                                        return true;
                                    }
                                    if (MotionEvent.ACTION_UP == event.getAction()) {

                                        return true;
                                    }
                                    return true;
                                }
                            });
                        }

在touchEvent 中返回fasle 是在点点击事件中是可以处理的,但是要是true 是自己消费,不再朝下传递

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值