Android触摸事件--View

原博客:http://blog.csdn.net/yanbober/article/details/45887547

一:View的onTouch和onClick的执行效果

1:activity的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mylayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.mycompany.viewtouch.MainActivity">
    <Button
        android:id="@+id/my_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click test"/>
</LinearLayout>
2:activity的代码如下,主要看注释
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {
    private LinearLayout mLayout;
    private Button mButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mLayout = (LinearLayout) this.findViewById(R.id.mylayout);
        mButton = (Button) this.findViewById(R.id.my_btn);

        mLayout.setOnTouchListener(this);
        mButton.setOnTouchListener(this);

        mLayout.setOnClickListener(this);
        mButton.setOnClickListener(this);
    }

    /**
     * 1:单击Button:多次调用onTouch
     * OnTouchListener--onTouch-- action=0 --android.support.v7.widget.AppCompatButton > ACTION_DOWN
     * OnTouchListener--onTouch-- action=1 --android.support.v7.widget.AppCompatButton > ACTION_UP
     * OnClickListener--onClick--android.support.v7.widget.AppCompatButton > onClick在ACTION_UP之后执行
     *
     * 2:单击Button以外的区域:(分析同上)
     * OnTouchListener--onTouch-- action=0 --android.widget.LinearLayout
     * OnTouchListener--onTouch-- action=1 --android.widget.LinearLayout
     * OnClickListener--onClick--android.widget.LinearLayout
     *
     * 3:手指在Button上滑动:多次调用onTouch
     * OnTouchListener--onTouch-- action=0 --android.support.v7.widget.AppCompatButton > ACTION_DOWN
     * OnTouchListener--onTouch-- action=2 --android.support.v7.widget.AppCompatButton > ACTION_MOVE
     *                                      .
     *                                      .
     *                                      .
     * OnTouchListener--onTouch-- action=2 --android.support.v7.widget.AppCompatButton > ACTION_MOVE
     * OnTouchListener--onTouch-- action=1 --android.support.v7.widget.AppCompatButton > ACTION_UP
     * OnClickListener--onClick--android.support.v7.widget.AppCompatButton > onClick在ACTION_UP之后执行
     *
     * 4:修改onTouch的返回值为true:多次调用onTouch
     * OnTouchListener--onTouch-- action=0 --android.support.v7.widget.AppCompatButton > ACTION_DOWN
     * OnTouchListener--onTouch-- action=1 --android.support.v7.widget.AppCompatButton > ACTION_UP
     * 不再执行onClick
     *
     * 总结:
     * 1:Android控件的触摸事件触发顺序是先触发onTouch,其次onClick。
     * 2:如果控件的onTouch返回true将会阻止事件继续传递(onTouch中已经将触摸事件消费掉了),返回false事件会继续传递(onTouch并没有消费掉事件)。
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i("yin", "OnTouchListener--onTouch-- action="+event.getAction()+" --"+v);
        return false;
    }

    @Override
    public void onClick(View v) {
        Log.i("yin", "OnClickListener--onClick--"+v);
    }
}

二:下面通过源码分析为什么会出现第一步中演示的执行结果

Android的触摸事件都是从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;	//ListenerInfo是Veiw的静态内部类定义了各种各样的监听,mListenerInfo来自你写的setOnTouchListener、setOnClickListener等
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {//四个参数同时为true该if语句才成立,第三个参数判断View是否可用,默认可用(可设置),第四个参数onTouch我们会重写因此返回true或false由我们决定,因此如果该if语句成立则设置本来默认为false的result位true,后面的if语句则不再执行,该dispatchTouchEvent方法最终也会返回true,因此,这次事件(ACTON_DOWN,ACTION_MOVE,ACTION_UP等)分发完毕(被消费掉),后面的代码都不再执行了。注意:这里还没有执行onClick;如果view不可用,则即使你重写onTouch也不执行,但你可以重写onTouchEvent方法处理事件,因为下一个if语句会执行onTouchEvent方法。
                result = true;
            }
			//如果上面的if语句不成立,则result仍然为false,因此onTouchEvent就会执行,并且如果onTouchEvent返回false,则result仍然为false,dispatchTouchEvent也会返回false,如果onTouchEvent返回true则result为true,dispatchTouchEvent也会返回true。即如果onTouchEvent执行,则其返回值决定了dispatchTouchEvent的返回值。且到这里可以猜测onClick方法一定与onTouchEvent有关系
            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;
    }
onTouchEvent方法源码如下(注意看中文注释);
    public boolean onTouchEvent(MotionEvent event) {
        final float x = event.getX();
        final float y = event.getY();
        final int viewFlags = mViewFlags;
		//1:View不可用
        if ((viewFlags & ENABLED_MASK) == DISABLED) {
            if (event.getAction() == 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.//1:如果View不可用但是可点击则onTouchEvent直接返回true;2:如果View不可用且不可点击则onTouchEvent直接返回false
            return (((viewFlags & CLICKABLE) == CLICKABLE ||
                    (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
        }

        if (mTouchDelegate != null) {
            if (mTouchDelegate.onTouchEvent(event)) {
                return true;
            }
        }
		//2:View可用且可点击,则通过switch处理各种事件,但最终都返回true(第127行)。
        if (((viewFlags & CLICKABLE) == CLICKABLE ||
                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP: //onClick方法在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) {
                            // 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();//内部调用了onClick方法
                                }
                            }
                        }

                        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();
                    }
                    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();
                    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;
        }
		//3:View可用但不可点击,直接onTouchEvent直接返回false
        return false;
    }


通过以上源码的分析,对比我们第一步中演示的log日志,就很容易理解了。

三:根据原码总结下图:




触发每一种ActionEvent都会调用dispatchTouchEvent(MotionEvent event)方法,前提是前一个action执行完成后onTouchEvent返回true;
例如一个单机动作:ACTION_DOWN会调用一次dispatchTouchEvent进行事件分发,ACTION_UP也会调用一次,并不是说你单机一下就调用一次dispatchTouchEvent,
但是如果执行ACTION_DOWN后onTouchEvent返回了false,则不再分发ACTION_UP事件。
再例如一个滑动的动作:ACTION_DOWN、ACTION_MOVE、ACTION_UP都会分别调用一次dispatchTouchEvent进行事件分发。


如果执行了onTouchEvent则onTouchEvent的返回值与dispatchTouchEvent返回值一致。

四:实例验证以上的源码分析

1:自定义一个TestButton重写其dispatchTouchEvent方法和onTouchEvent方法
这里重写这两个方法只是为了单纯的查看log日志,而不修改其方法体逻辑,即直接super即可
public class TestButton extends Button {

    public TestButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 重写该方法,但不修改其逻辑,即直接super
     * @param event
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i("yin", "dispatchTouchEvent-- action=" + event.getAction());
        return super.dispatchTouchEvent(event);
    }

    /**
     * 重写该方法,但不修改其逻辑,即直接super
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("yin", "onTouchEvent-- action="+event.getAction());
        return super.onTouchEvent(event);
    }
}
2:activity及其布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:id="@+id/mylayout"
    tools:context="com.mycompany.simpleviewtouch.MainActivity">

    <com.mycompany.simpleviewtouch.TestButton
        android:id="@+id/my_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click test"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener{
    private LinearLayout mLayout;
    private TestButton mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLayout = (LinearLayout) this.findViewById(R.id.mylayout);
        mButton = (TestButton) this.findViewById(R.id.my_btn);

        mLayout.setOnTouchListener(this);
        mButton.setOnTouchListener(this);

        mLayout.setOnClickListener(this);
        mButton.setOnClickListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i("yin", "OnTouchListener--onTouch-- action="+event.getAction()+" --"+v);
        return false;
    }

    @Override
    public void onClick(View v) {
        Log.i("yin", "OnClickListener--onClick--"+v);
    }
}

五:针对第四步中的例子进行现象分析

5.1:点击TestButton日志如下
    /**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=0
     * 以上是分发的TestButton的ACTION_UP事件的逻辑
     * dispatchTouchEvent-- action=1
     * OnTouchListener--onTouch-- action=1 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=1
     * OnClickListener--onClick--com.mycompany.simpleviewtouch.TestButton
     * 以上是分发的TestButton的ACTION_DOWN的逻辑
     */

5.2:修改第四步例子中TestButton的onTouchEvent如下:
即直接return true,而不super
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("yin", "onTouchEvent-- action="+event.getAction());
        return true;
    }
再次单击TestButton,log如下:
    /**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=0
     * 以上分发的是ACTION_DOWN的事件
     * dispatchTouchEvent-- action=1
     * OnTouchListener--onTouch-- action=1 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=1
     * 以上分发的是ACTION_UP的事件
     */
与5.1的log对比发现,执行逻辑基本一致,只是最后ACTION_UP并没有调用onClick方法,这是因为我们直接让onTouchEvent return true,而并没有super,因此并没有执行父类View的ACTION_DOEN,ACTION_MOVE,ACTION_UP等的逻辑。
因此,如果我们将TestButton改为如下:
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("yin", "onTouchEvent-- action="+event.getAction());
        super.onTouchEvent(event);
        return true;
    }
此时,再次点击TestButton,则日志如下:
/**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=0
     * 以上是分发的TestButton的ACTION_UP事件的逻辑
     * dispatchTouchEvent-- action=1
     * OnTouchListener--onTouch-- action=1 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=1
     * OnClickListener--onClick--com.mycompany.simpleviewtouch.TestButton
     * 以上是分发的TestButton的ACTION_DOWN的逻辑
     */
通过日志返现其执行结果与5.1完全一致,即onTouchEvent仍然执行了父类的逻辑,只是最终无论父类做了什么,我们都将onTouchEvent直接返回true。
5.3:修改第四步例子的TestButton的onTouchEvent直接返回false
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("yin", "onTouchEvent-- action="+event.getAction());
        return false;
    }
点击TestButton结果如下:
    /**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=0
     * 以上是TestButton相关的触摸日志
     * OnTouchListener--onTouch-- action=0 --android.widget.LinearLayout
     * OnTouchListener--onTouch-- action=1 --android.widget.LinearLayout
     * OnClickListener--onClick--android.widget.LinearLayout
     * 以上是TestButton的父控件LinearLayout的相关日志
     */
为什么会出现以上结果呢,为什么执行TestButton的父控件的触摸事件呢?
肯以肯定的是如果TestButton的onTouchEvent返回false,则TestButton的dispatchTouchEvent也返回false,如果dispatchTouchEvent返回false,则View不再分发下一个触摸事件。这里分发了ACTION_UP之后onTouchEvent返回了false,因此dispatchTouchEvent不再分发之后的ACTION_MOVE,ACTION_UP等事件。
那么为什么onTouchEvent返回了false后又牵扯到了View的父控件呢,且这里父控件还处理了一次ACTION_DOWN事件,这里先做个推测(不一定正确,待下一篇ViewGroup的触摸事件分析时再确认):这里子控件View调用dispatchTouchEvetn分发了ACTION_DOWN事件之后,其onTouchEvent返回false,则dispatchTuochEvent也返回false,说明View虽然分发了ACTION_DOWN,但是并没有将其消费掉,因此其父控件仍然能接收到这个ACTION_DOWN事件进行处理。因此ACTION_DOWN之后的ACTION_MOVE,ACTION_UP等事件就自然而然的由其父控件来处理,当前View就接收不到之后的ACTION_MOVE,ACTION_UP等事件了。
因此,可以理解如果将TestButton的onTouchEvent方法修改成如下,则执行结果与上面一样:
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i("yin", "onTouchEvent-- action="+event.getAction());
        super.onTouchEvent(event);
        return false;
    }
5.4:修改第四步中TestButton的dispatchTouchEvent方法,将其直接返回true,如下:
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i("yin", "dispatchTouchEvent-- action=" + event.getAction());
        return true;
    }
点击Button日志如下:
/**
     * dispatchTouchEvent-- action=0
     * dispatchTouchEvent-- action=2
     *              .
     *              .
     * dispatchTouchEvent-- action=2      
     * dispatchTouchEvent-- action=1
     */
分析可知,因为dispatchTouchEvent返回了true,所有各个触摸事件都能得到派发,但是由于没有super,因此各个事件在父类的逻辑并没有得到执行,而onTouch,onTouchEvent和onClick就存在于父类的各个触摸事件的逻辑中,因此都没有执行。
所以,如果将dispatchTouchEvent修改为如下,则因为super了,所以所有事件都可以得到正常派发
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i("yin", "dispatchTouchEvent-- action=" + event.getAction());
        super.dispatchTouchEvent(event);
        return true;
    }

5.5:修改第四步中TestButton的dispatchTuchEvent方法,将其直接返回false,如下:
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i("yin", "dispatchTouchEvent-- action=" + event.getAction());
        return false;
    }
点击TestButton日志如下:
    /**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --android.widget.LinearLayout
     * OnTouchListener--onTouch-- action=2 --android.widget.LinearLayout
     *              .
     *              .
     * OnTouchListener--onTouch-- action=2 --android.widget.LinearLayout
     * OnTouchListener--onTouch-- action=1 --android.widget.LinearLayout
     * OnClickListener--onClick--android.widget.LinearLayout
     */
分析:TestButton首先调用dispatchTouchEvent分发ACTION_DOWN事件,但是dispatchTouchEvent直接返回了false,因此不再分发ACTION_MOVE,ACTION_UP等事件,至于为什么涉及到TestButton的父控件,参考5.3中的分析。
因此,如果将dispatchTouchEvent改为如下,结果也很容易理解:
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i("yin", "dispatchTouchEvent-- action=" + event.getAction());
        super.dispatchTouchEvent(event);
        return false;
    }
点击TestButton结果如下:
    /**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=0
     * 
     * OnTouchListener--onTouch-- action=0 --android.widget.LinearLayout
     * OnTouchListener--onTouch-- action=2 --android.widget.LinearLayout
     *              .
     *              .
     * OnTouchListener--onTouch-- action=2 --android.widget.LinearLayout
     * OnTouchListener--onTouch-- action=1 --android.widget.LinearLayout
     * OnClickListener--onClick--android.widget.LinearLayout
     */

5.6:修改第四步例子中dispatchTouchEvent方法和onTouchEvent方法如下:
@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i(null, "dispatchTouchEvent-- action=" + event.getAction());
        super.dispatchTouchEvent(event);
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(null, "onTouchEvent-- action=" + event.getAction());
        super.onTouchEvent(event);
        return false;
    }
点击TestButton日志如下:
    /**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=0
     *
     * dispatchTouchEvent-- action=1
     * OnTouchListener--onTouch-- action=1 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=1
     * OnClickListener--onClick--com.mycompany.simpleviewtouch.TestButton
     */
结果很好理解,这里,dispatchTouchEvent的返回值不再与onTouchEvent的返回值一致,因为我们让dispatchTouchEvent默认返回true,即默认各个触摸事件都分发。
因此,结合前面集中类型的分析,将第四步例子中的dispatchTouchEvent和onTouchEvent修改为如下:
@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i(null, "dispatchTouchEvent-- action=" + event.getAction());
        super.dispatchTouchEvent(event);
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(null, "onTouchEvent-- action=" + event.getAction());
        super.onTouchEvent(event);
        return true;
    }
点击TestButton日志如下:
    /**
     * dispatchTouchEvent-- action=0
     * OnTouchListener--onTouch-- action=0 --com.mycompany.simpleviewtouch.TestButton
     * onTouchEvent-- action=0
     *
     * OnTouchListener--onTouch-- action=0 --android.widget.LinearLayout
     * OnTouchListener--onTouch-- action=1 --android.widget.LinearLayout
     * OnClickListener--onClick--android.widget.LinearLayout
     */
分析:我们将dispatchTouchEvent默认返回false,则无论onTouchEvent返回什么,dispatchTouchEvent都不再分发之后的事件,至于为什么涉及到TestButton的父控件,参考参考5.3中的分析

六:最后再做一个总结

综合得出Android View的触摸屏事件传递机制有如下特征:


触摸控件(View)首先执行dispatchTouchEvent方法。
在dispatchTouchEvent方法中先执行onTouch方法,后执行onClick方法(onClick方法在onTouchEvent中执行,下面会分析)。
如果控件(View)的onTouch返回false或者mOnTouchListener为null(控件没有设置setOnTouchListener方法)或者控件不是enable的情况下会调运onTouchEvent,dispatchTouchEvent返回值与onTouchEvent返回一样。
如果控件不是enable的设置了onTouch方法也不会执行,只能通过重写控件的onTouchEvent方法处理(上面已经处理分析了),dispatchTouchEvent返回值与onTouchEvent返回一样。
如果控件(View)是enable且onTouch返回true情况下,dispatchTouchEvent直接返回true,不会调用onTouchEvent方法。
当dispatchTouchEvent在进行事件分发的时候,只有前一个action返回true,才会触发下一个action(也就是说dispatchTouchEvent返回true才会进行下一次action派发)。




















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值