自定义组件点击只有down事件,原理深度解析

来源:互联网 发布:foxmail mac邮件位置 编辑:程序博客网 时间:2024/06/08 02:37

开始博主遇到这个问题也很奇怪

博主开始是这么设置的

  @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:               x = (int) getX();               y = (int) getY();                break;            case MotionEvent.ACTION_MOVE:                int motionX = (int) (getX()- x);                int motionY = (int) (getY()-y);                animate().translationY(motionY);                animate().translationX(motionX);                break;            case MotionEvent.ACTION_UP:                break;        }        return super.onTouchEvent(event);    }

这样他只会执行down 其他的都不会跑

解决这个方法 问别人也许会得到 很多种答案

没有设置监听啊
得返回true啊

其实那这些说法都是对的

 @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:               x = (int) getX();               y = (int) getY();                break;            case MotionEvent.ACTION_MOVE:                int motionX = (int) (getX()- x);                int motionY = (int) (getY()-y);                animate().translationY(motionY);                animate().translationX(motionX);                break;            case MotionEvent.ACTION_UP:                break;        }        return true;    }

以上的代码跑起来就是没有问题的

设置监听的就不演示了 那个只要初始化对象设置下就好了

现在问题解决了,但是为什么那? 为什么这样就好了那

先让我们还原 开始的 写法 关键的地方 他在最后

return super.onTouchevent(event);

既然这样 我们就去看看TouchEvent的源码 是怎么处理的

 public boolean onTouchEvent(MotionEvent event) {        final float x = event.getX();        final float y = event.getY();        final int viewFlags = mViewFlags;        final int action = event.getAction();        if ((viewFlags & ENABLED_MASK) == DISABLED) {            if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {                setPressed(false);            }            // A disabled view that is clickable still consumes the touch            // events, it just doesn't respond to them.            return (((viewFlags & CLICKABLE) == CLICKABLE                    || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)                    || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);        }        if (mTouchDelegate != null) {            if (mTouchDelegate.onTouchEvent(event)) {                return true;            }        }        if (((viewFlags & CLICKABLE) == CLICKABLE ||                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||                (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {            switch (action) {                case MotionEvent.ACTION_UP:                    boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;                    if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {                        // take focus if we don't have it already and we should in                        // touch mode.                        boolean focusTaken = false;                        if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {                            focusTaken = requestFocus();                        }                        if (prepressed) {                            // The button is being released before we actually                            // showed it as pressed.  Make it show the pressed                            // state now (before scheduling the click) to ensure                            // the user sees it.                            setPressed(true, x, y);                       }                        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)) {                                    performClick();                                }                            }                        }                        if (mUnsetPressedState == null) {                            mUnsetPressedState = new UnsetPressedState();                        }                        if (prepressed) {                            postDelayed(mUnsetPressedState,                                    ViewConfiguration.getPressedStateDuration());                        } else if (!post(mUnsetPressedState)) {                            // If the post failed, unpress right now                            mUnsetPressedState.run();                        }                        removeTapCallback();                    }                    mIgnoreNextUpEvent = false;                    break;                case MotionEvent.ACTION_DOWN:                    mHasPerformedLongPress = false;                    if (performButtonActionOnTouchDown(event)) {                        break;                    }                    // Walk up the hierarchy to determine if we're inside a scrolling container.                    boolean isInScrollingContainer = isInScrollingContainer();                    // For views inside a scrolling container, delay the pressed feedback for                    // a short period in case this is a scroll.                    if (isInScrollingContainer) {                        mPrivateFlags |= PFLAG_PREPRESSED;                        if (mPendingCheckForTap == null) {                            mPendingCheckForTap = new CheckForTap();                        }                        mPendingCheckForTap.x = event.getX();                        mPendingCheckForTap.y = event.getY();                        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());                    } else {                        // Not inside a scrolling container, so show the feedback right away                        setPressed(true, x, y);                        checkForLongClick(0);                    }                    break;                case MotionEvent.ACTION_CANCEL:                    setPressed(false);                    removeTapCallback();                    removeLongPressCallback();                    mInContextButtonPress = false;                    mHasPerformedLongPress = false;                    mIgnoreNextUpEvent = false;                    break;                case MotionEvent.ACTION_MOVE:                    drawableHotspotChanged(x, y);                    // Be lenient about moving outside of buttons                    if (!pointInView(x, y, mTouchSlop)) {                        // Outside button                        removeTapCallback();                        if ((mPrivateFlags & PFLAG_PRESSED) != 0) {                            // Remove any future long press/tap checks                            removeLongPressCallback();                            setPressed(false);                        }                    }                    break;            }            return true;        }        return false;    }

以上就是view的onTouchEvent的源码

有一个很长的判断   if (((viewFlags & CLICKABLE) == CLICKABLE ||                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||                (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {点击 追踪 LONE_CLICKABLE   * <p>     * Indicates this view can be long clicked. When long clickable, a View     * reacts to long clicks by notifying the OnLongClickListener or showing a     * context menu.     * </p>     * {@hide}     */    static final int LONG_CLICKABLE = 0x00200000;这段英文注解 的意思是  这个标记表明是否可以长按点击的 当长按点击的时候 这个view 长按点击的反馈会通知onLongClickListener 显示到 上下文的菜单上在onTouchEvent的判断条件 里面 其他的类似的标记 里面的注解都是大同小异都是各种监听是否被设置的标记如果没有设置就会返回 false   如果设置了监听就会返回 true 源码 看到这里就解决了第一种说法的原理  只要设置监听了 事件就会完整传递

现在我们来理解第二种说法
只要在 复写 Touchevent 里面 直接返回 true
就会解决只会 传递down事件的问题
return true ;
那么 就和 Touchevent的源码没关系了
根本就没调用 touchevent
我们去看 决定是否事件分发的关键
View的 dispatchTouchEvent 的源码

  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;    }//我们一直获得down的原因 默认给了一个 down事件       if (mInputEventConsistencyVerifier != null) {            mInputEventConsistencyVerifier.onTouchEvent(event, 0);        }//这段代码最关键 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;            }        } onFilterTouchEventForSecurity 是判断view上面是否有遮盖物 如果有遮盖物判断直接跳过 return 默认的 result 就是 false 那么这个事件就 挂掉了  只会传递一个 down事件给view   我们正常情况下 上面没有view 这个判断都会进去   里面有2种情况的判断 第一种判断是设置过监听的情况 那么这个事件会被拦截 处理掉  result = true ;第二种情况就是 我们在复写的TouchEvent 直接返回 true 我们会得到接下来事件的原因这里取了 onTouchEvent的返回值if (!result && onTouchEvent(event)) {                result = true;    }最后 dispatch 会return result  给他的调用者 public final boolean dispatchPointerEvent(MotionEvent event) {        if (event.isTouchEvent()) {            return dispatchTouchEvent(event);        } else {            return dispatchGenericMotionEvent(event);        }    }       这个事件就会依次传递下去 我们就会得到正常的Touch事件 

本篇博客结束
如果觉得对你有帮助

粉我把
关注更多我的blog.

技术交流群:

博客圈 493554215

这是一个热爱分享技术,拥有热烈学习氛围的群 ,
博主身为群内的一员感到骄傲
推荐还在路上的伙伴们

1 0
原创粉丝点击