从源码说说dispatchTouchEvent与onTouchEvent的关系以及OnTouchListener的用法

来源:互联网 发布:淘宝分享有礼怎么领取 编辑:程序博客网 时间:2024/06/06 08:31

为什么要说这个问题呢,这段时间还是在SeekBar上遇到一些问题,好像ADT并没有给seekBar在xml中提供enabled属性,虽然我们可以在代码中设置,但是它并不能实现下面这个需求,所以我们需要找到别的方式去实现它,也为了能更好的了解一下dispatchTouchEvent与onTouchEvent之间的关系。

我们现在有这么一个问题,我们需要使SeekBar在某些时候只可点击不可拖动,如果简简单单使用enabled的话,那么只会不能移动,这是不满足的。所以我们需要有这么一个入口去逐步解决这个问题。

入口点:

需要知道seekbar是如何拖动与绘制的。

首先会想到去哪找这个问题?肯定是SeekBar的onTouchEvent方法对不对?我们在拖拽seekBar的thumb的时候,肯定会触发MotionEvent.ACTION_MOVE的事件,这样一来,我们就找到了我们问题当然入口处,只用合理控制onTouchEvent方法的调用就可以了,我们从源码开始看:

在SeekBar类中是没有onTouchEvent方法的,那么我们需要去它的父类中找,如果父类中没有,那么就需要去父类的父类中去找,直到找到为止,那么我们很快的就在SeekBar的父类AbsSeekBar中找到了onTouchEvent方法:

    public boolean onTouchEvent(MotionEvent event) {        if (!mIsUserSeekable || !isEnabled()) {            return false;        }        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                if (isInScrollingContainer()) {                    mTouchDownX = event.getX();                } else {                    setPressed(true);                    if (mThumb != null) {                        invalidate(mThumb.getBounds()); // This may be within the padding region                    }                    onStartTrackingTouch();                    trackTouchEvent(event);                    attemptClaimDrag();                }                break;            case MotionEvent.ACTION_MOVE:                if (mIsDragging) {                    trackTouchEvent(event);                } else {                    final float x = event.getX();                    if (Math.abs(x - mTouchDownX) > mScaledTouchSlop) {                        setPressed(true);                        if (mThumb != null) {                            invalidate(mThumb.getBounds()); // This may be within the padding region                        }                        onStartTrackingTouch();                        trackTouchEvent(event);                        attemptClaimDrag();                    }                }                break;            case MotionEvent.ACTION_UP:                if (mIsDragging) {                    trackTouchEvent(event);                    onStopTrackingTouch();                    setPressed(false);                } else {                    // Touch up when we never crossed the touch slop threshold should                    // be interpreted as a tap-seek to that location.                    onStartTrackingTouch();                    trackTouchEvent(event);                    onStopTrackingTouch();                }                // ProgressBar doesn't know to repaint the thumb drawable                // in its inactive state when the touch stops (because the                // value has not apparently changed)                invalidate();                break;            case MotionEvent.ACTION_CANCEL:                if (mIsDragging) {                    onStopTrackingTouch();                    setPressed(false);                }                invalidate(); // see above explanation                break;        }        return true;    }

来分析下这段代码:

mIsUserSeekable在AbsSeekBar中声明为了true,后面也没有任何地方对它进行修改。isEnabled由于没有对它进行专门设置,所以这里恒为true。所以if中的判断恒为false。接着往下MotionEvent.ACTION_DOWN中的if(isInScrollingContainer())表示是说如果是在可以滚动的容器当中,我们这里使用的是平常的布局,所以这里为false。

setPressed(true)更改按压状态,invalidate(mThumb.getBounds())绘制thumb,onStartTrackingTouch设置mIsDragging为true为ACTION_MOVE做准备,trackTouchEvent对内部进行测量计算绘制界面,attemptClaimDrag禁止父布局及祖先布局阻断touch事件。好,接下来就是MotionEvent.ACTION_MOVE了,已知mIsDragging是true,所以当执行move的时候一直是在调用trackTouchEvent方法了,所以,我们在拖拽的seekBar的thumb的时候就是在这里进行的处理。好,有些扯远了,回到我们的主要部分:

我们刚刚可以看到onTouchEvent中没有任何一处可以对我们的触摸事件进行拦截,那我们就需要找在哪里调用了onTouchEvent方法,我们从SeekBar到其父类AbsSeekBar再到ProgressBar,最后到View的dispatchTouchEvent中被调用了:

    /**     * 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) {        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;    }
我们先不管其它,只关心我们关心的:

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

我们先不管外面那个方法,只看 内部:这里判断了li.mOnTouchListener是否为空,控件是否可用,最后通过mOnTouchListener.onTouch将事件传递进去,这里的返回值就很关键了,如果返回false,则代码会继续向下执行,最后交给onTouchEvent,如果返回true,则直接返回,不会再执行onTouchEvent方法。所以为了解决有时可以点击SeekBar,但又不能拖拽问题,我们就可以从mOnTouchListener下手了。


0 0
原创粉丝点击