Android的官方文档解析如下:
onInterceptTouchEvent()与onTouchEvent()的机制:
1. down事件首先会传递到onInterceptTouchEvent()方法
2. 如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return false,
那么后续的move, up等事件将继续会先传递给该ViewGroup,之后才和down事件一样传递给最
终的目标view的onTouchEvent()处理
3. 如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return true,
那么后续的move, up等事件将不再传递给onInterceptTouchEvent(),而是和down事件一样
传递给该ViewGroup的onTouchEvent()处理,注意,目标view将接收不到任何事件。
4. 如果最终需要处理事件的view的onTouchEvent()返回了false,那么该事件将被传递至其上一
层次的view的onTouchEvent()处理
5. 如果最终需要处理事件的view 的onTouchEvent()返回了true,那么后续事件将可以继续传递
给该view的onTouchEvent()处理
这是官方文档的说法。这里可以看出,先传递的是down时间,遇到 true返回后,后续的事件才会继续分发。
UI布局如下
<RelativeLayout 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"
tools:context=".MainActivity" >
<com.example.helloword.Layout1
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</com.example.helloword.Layout1>
</RelativeLayout>
Layout1 是一个自定义的 LinearLayout,重写了他的 onInterceptTouchEvent 跟 onTouchEvent 事件。
1、如果 Layout1 的 onInterceptTouchEvent 的 down 返回 ture,(看官方的第三点)
(1)、当 onTouchEvent 的 down 返回 false 时,触摸事件经过的路径(方法)情况如下
down事件 :onInterceptTouchEvent 、onTouchEvent
move事件 :无
up事件 :无
(2)、当 onTouchEvent 的 down 返回 true 时,触摸事件经过的路径(方法)情况如下
down事件 :onInterceptTouchEvent 、onTouchEvent
move事件 :onTouchEvent
up事件 :onTouchEvent
2、如果 Layout1 的 onInterceptTouchEvent 的 down 返回 false,
(1)、当 onTouchEvent 的 down 返回 false 时,触摸事件经过的路径(方法)情况如下
down事件 :onInterceptTouchEvent 、onTouchEvent
move事件 :无
up事件 :无
(2)、当 onTouchEvent 的 down 返回 true 时,触摸事件经过的路径(方法)情况如下
down事件 :onInterceptTouchEvent 、onTouchEvent
move事件 :onTouchEvent
up事件 :onTouchEvent
从这里可以看出,当该viewgroup处在最上层的时候,move、跟 up 事件五路如何都不会经过 onInterceptTouchEvent 方法。
接下来在Layout1上面放多一层Layout2,UI布局如下:
<RelativeLayout 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"
tools:context=".MainActivity" >
<com.example.helloword.Layout1
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.helloword.Layout2
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</com.example.helloword.Layout1>
</RelativeLayout>
1、如果 Layout1 的 onInterceptTouchEvent 的 down 返回 ture,(看官方的第三点,Layout2接受不到任何事件。)
(1)、当 Layout1 的 onTouchEvent 的 down 返回 false 时(Layout2 返回任意值),触摸事件经过的路径(方法)情况如下
down事件 :onInterceptTouchEvent 、onTouchEvent
move事件 :无
up事件 :无
(2)、当 Layout1 的 onTouchEvent 的 down 返回 true 时(Layout2 返回任意值),触摸事件经过的路径(方法)情况如下
down事件 :onInterceptTouchEvent 、onTouchEvent
move事件 :onTouchEvent
up事件 :onTouchEvent
从这里可以看出,如果下面的那层在 onInterceptTouchEvent 的 down 事件返回 true 的时候,在他上面的view都不会接收到touch事件,被拦截了,想到与返回true的那一层为最顶层。
2、如果 Layout1 的 onInterceptTouchEvent 的 down 返回 false,down 事件继续往下传,直到遇到 true 返回后,后续的事件才会继续分发。如果后续的事件在到达目标view前遇到了 true 返回,例如此时在 Layout1 的 onInterceptTouchEvent 中 move 返回 true, 那么 move 事件将不会传给 Layout2。
onTouchEvent事件是从最上面的那层往下面传递的,直到遇到true。