Android自定义View时按键消息流程是绕不过去的知识点,其中又涉及到三大方法
onTouchEvent、
dispatchTouchEvent、
onInterceptTouchEvent,他们之间的关系如何?优先级如何?出现矛盾时又会采取怎样的处理方式,我们这一节来做一下测试。
touch_event_test的布局文件如此:
该Demo中有一个Activity,布局里面用到了我们自定义的TouchEventViewGroup,该ViewGroup中包含了两个自定义TouchEventView。
设置View的onTouchEvent=true
设置View的onTouchEvent=false
小结
设置View的dispatchTouchEvent=false,View的onTouchEvent=false
设置View的dispatchTouchEvent=false,View的onTouchEvent=true
View的dispatchTouchEvent根据onTouchEvent改变,但是优先级又高于onTouchEvent
设置View的onTouchEvent返回true
小结
设置View的onTouchEvent返回false
小结
要测试就要先在View中dispatchTouchEvent=false,封堵View的流程
设置ViewGroup的onTouchEvent返回true
设置ViewGroup的onTouchEvent返回false
设置ViewGroup的onTouchEvent返回true,dispatchTouchEvent返回false
说明和View一样,ViewGroup的dispatchTouchEvent优先级高于onTouchEvent
ViewGroup的onInterceptTouchEvent返回true,onTouchEvent也返回true
ViewGroup的onInterceptTouchEvent返回true,但是onTouchEvent返回false
再来个小结
ViewGroup的onInterceptTouchEvent拦截之后消息不再发给View,这也是onInterceptTouchEvent最主要的作用,因为通过dispatchTouchEvent拦截,都会先进入View再来询问ViewGroup
但是ViewGroup的拦截后,如果处理就给ViewGroup处理,如果不处理,还是上交给Activity
前提是ViewGroup不要在onInterceptTouchEvent中拦截哦
设置View和ViewGroup的onTouchEvent都返回true
消息流程是View--ViewGroup--Activity,所以不想处理时层层递交(Activity是终点站),想处理时,随时可以拦截
View和ViewGroup的内部,onTouchEvent决定了他们的dispatchTouchEvent的值,但是dispatchTouchEvent优先级又高于onTouchEvent(只要拦截,就一定交给当前对象处理,只要不拦截,就一定不交给当前对象处理)
ViewGroup的onInterceptTouchEvent决定了是否不向View派发消息,但是即使拦截了,如果ViewGroup没有处理,还是要上交的
1.Demo源码
搭建的Demo示例也比较简单,分别创建了TouchEventFlowActivity、TouchEventViewGroup、TouchEventView三个对象来测试消息流程,代码分别如下,也可以访问github的测试Demo中直接下载文件。
1.1.TouchEventFlowActivity
@EActivity(R.layout.touch_event_test)
public class TouchEventFlowActivity extends AppCompatActivity {
public static final String TAG = "TouchEventTag";
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
boolean returnResult = super.dispatchTouchEvent(event);
Log.d(TouchEventFlowActivity.TAG, "Activity--dispatchTouchEvent:" + event.getAction() + ",returnResult:" + returnResult);
return returnResult;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean returnResult = super.onTouchEvent(event);
// returnResult = true;
Log.d(TouchEventFlowActivity.TAG, "Activity--onTouchEvent:" + event.getAction() + ",returnResult:" + returnResult);
return returnResult;
}
}
touch_event_test的布局文件如此:
<com.able.libs.touchEventFlowTest.TouchEventViewGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<com.able.libs.touchEventFlowTest.TouchEventView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view1"/>
<com.able.libs.touchEventFlowTest.TouchEventView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view2"/>
</com.able.libs.touchEventFlowTest.TouchEventViewGroup>
1.2.TouchEventViewGroup
public class TouchEventViewGroup extends LinearLayout {
public TouchEventViewGroup(Context context) {
super(context);
}
public TouchEventViewGroup(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TouchEventViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean returnResult = super.dispatchTouchEvent(ev);
// returnResult = false;
Log.d(TouchEventFlowActivity.TAG, "ViewGroup--dispatchTouchEvent:" + ev.getAction() + ",returnResult:" + returnResult);
return returnResult;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean returnResult = super.onInterceptTouchEvent(ev);
// returnResult = false;
Log.d(TouchEventFlowActivity.TAG, "ViewGroup--onInterceptTouchEvent:" + ev.getAction() + ",returnResult:" + returnResult);
return returnResult;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean returnResult = super.onTouchEvent(event);
returnResult = true;
Log.d(TouchEventFlowActivity.TAG, "ViewGroup--onTouchEvent:" + event.getAction() + ",returnResult:" + returnResult);
return returnResult;
}
}
1.3.TouchEventView
public class TouchEventView extends android.support.v7.widget.AppCompatButton {
public TouchEventView(Context context) {
super(context);
}
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchEventView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
boolean returnResult = super.dispatchTouchEvent(event);
// returnResult = false;
Log.d(TouchEventFlowActivity.TAG, "View--dispatchTouchEvent:" + event.getAction() + ",returnResult:" + returnResult);
return returnResult;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean returnResult = super.onTouchEvent(event);
returnResult = true;
Log.d(TouchEventFlowActivity.TAG, "View--onTouchEvent:" + event.getAction() + ",returnResult:" + returnResult);
return returnResult;
}
}
1.4.代码说明
该Demo中有一个Activity,布局里面用到了我们自定义的TouchEventViewGroup,该ViewGroup中包含了两个自定义TouchEventView。
代码中的returnResult变量可以控制每个方法是否强制返回true或false。
好了,测试开始。
2.View内部状态
2.1.View的onTouchEvent返回结果是否会影响View的dispatchTouchEvent?
设置View的onTouchEvent=true
01-31 17:33:00.055 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 17:33:00.058 28485-28485/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:true
01-31 17:33:00.059 28485-28485/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:true
01-31 17:33:00.059 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:true
01-31 17:33:00.059 28485-28485/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:true
01-31 17:33:00.078 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:2,returnResult:false
01-31 17:33:00.079 28485-28485/com.able.libs D/TouchEventTag: View--onTouchEvent:2,returnResult:true
01-31 17:33:00.079 28485-28485/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:2,returnResult:true
01-31 17:33:00.079 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 17:33:00.079 28485-28485/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 17:33:00.108 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:2,returnResult:false
01-31 17:33:00.109 28485-28485/com.able.libs D/TouchEventTag: View--onTouchEvent:2,returnResult:true
01-31 17:33:00.109 28485-28485/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:2,returnResult:true
01-31 17:33:00.110 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 17:33:00.110 28485-28485/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 17:33:00.111 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:1,returnResult:false
01-31 17:33:00.112 28485-28485/com.able.libs D/TouchEventTag: View--onTouchEvent:1,returnResult:true
01-31 17:33:00.112 28485-28485/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:1,returnResult:true
01-31 17:33:00.112 28485-28485/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:1,returnResult:true
01-31 17:33:00.113 28485-28485/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:true
设置View的onTouchEvent=false
01-31 17:34:11.059 28760-28760/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 17:34:11.061 28760-28760/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:false
01-31 17:34:11.061 28760-28760/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:false
01-31 17:34:11.061 28760-28760/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:false
01-31 17:34:11.061 28760-28760/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:false
01-31 17:34:11.062 28760-28760/com.able.libs D/TouchEventTag: Activity--onTouchEvent:0,returnResult:false
01-31 17:34:11.062 28760-28760/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:false
01-31 17:34:11.100 28760-28760/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 17:34:11.100 28760-28760/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 17:34:11.139 28760-28760/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 17:34:11.139 28760-28760/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 17:34:11.140 28760-28760/com.able.libs D/TouchEventTag: Activity--onTouchEvent:1,returnResult:false
01-31 17:34:11.140 28760-28760/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:false
小结
View的onTouchEvent结果会影响到dispatchTouchEvent的结果,如果View不处理事件,就上交
2.2.View的dispatchTouchEvent对onTouchEvent的影响
设置View的dispatchTouchEvent=false,View的onTouchEvent=false
01-31 17:53:56.123 29336-29336/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 17:53:56.127 29336-29336/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:false
01-31 17:53:56.127 29336-29336/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:false
01-31 17:53:56.128 29336-29336/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:false
01-31 17:53:56.128 29336-29336/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:false
01-31 17:53:56.129 29336-29336/com.able.libs D/TouchEventTag: Activity--onTouchEvent:0,returnResult:false
01-31 17:53:56.129 29336-29336/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:false
01-31 17:53:56.192 29336-29336/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 17:53:56.192 29336-29336/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 17:53:56.193 29336-29336/com.able.libs D/TouchEventTag: Activity--onTouchEvent:1,returnResult:false
01-31 17:53:56.194 29336-29336/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:false
设置View的dispatchTouchEvent=false,View的onTouchEvent=true
01-31 17:55:25.437 29670-29670/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 17:55:25.439 29670-29670/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:true
01-31 17:55:25.439 29670-29670/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:false
01-31 17:55:25.440 29670-29670/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:false
01-31 17:55:25.440 29670-29670/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:false
01-31 17:55:25.440 29670-29670/com.able.libs D/TouchEventTag: Activity--onTouchEvent:0,returnResult:false
01-31 17:55:25.440 29670-29670/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:false
01-31 17:55:25.461 29670-29670/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 17:55:25.461 29670-29670/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 17:55:25.479 29670-29670/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 17:55:25.479 29670-29670/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 17:55:25.496 29670-29670/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 17:55:25.496 29670-29670/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 17:55:25.501 29670-29670/com.able.libs D/TouchEventTag: Activity--onTouchEvent:1,returnResult:false
01-31 17:55:25.501 29670-29670/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:false
小结
无论View的onTouchEvent何种状态,只要View的dispatchTouchEvent=false,消息就不会发给View
2.3.View内部小结
View的dispatchTouchEvent根据onTouchEvent改变,但是优先级又高于onTouchEvent
3.ViewGroup与View之间优先级
设置View的onTouchEvent返回true
01-31 18:09:06.612 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 18:09:06.613 30329-30329/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:true
01-31 18:09:06.613 30329-30329/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:true
01-31 18:09:06.613 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:true
01-31 18:09:06.613 30329-30329/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:true
01-31 18:09:06.651 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:2,returnResult:false
01-31 18:09:06.651 30329-30329/com.able.libs D/TouchEventTag: View--onTouchEvent:2,returnResult:true
01-31 18:09:06.651 30329-30329/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:2,returnResult:true
01-31 18:09:06.651 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 18:09:06.651 30329-30329/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 18:09:06.667 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:2,returnResult:false
01-31 18:09:06.667 30329-30329/com.able.libs D/TouchEventTag: View--onTouchEvent:2,returnResult:true
01-31 18:09:06.668 30329-30329/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:2,returnResult:true
01-31 18:09:06.668 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 18:09:06.668 30329-30329/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 18:09:06.712 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:1,returnResult:false
01-31 18:09:06.712 30329-30329/com.able.libs D/TouchEventTag: View--onTouchEvent:1,returnResult:true
01-31 18:09:06.713 30329-30329/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:1,returnResult:true
01-31 18:09:06.713 30329-30329/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:1,returnResult:true
01-31 18:09:06.713 30329-30329/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:true
小结
只要View的onTouchEvent处理,消息就不再发送给ViewGroup
设置View的onTouchEvent返回false
01-31 18:12:28.601 30827-30827/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 18:12:28.606 30827-30827/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:false
01-31 18:12:28.606 30827-30827/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:false
01-31 18:12:28.607 30827-30827/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:false
01-31 18:12:28.607 30827-30827/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:false
01-31 18:12:28.608 30827-30827/com.able.libs D/TouchEventTag: Activity--onTouchEvent:0,returnResult:false
01-31 18:12:28.608 30827-30827/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:false
01-31 18:12:28.617 30827-30827/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 18:12:28.617 30827-30827/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 18:12:28.668 30827-30827/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 18:12:28.668 30827-30827/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 18:12:28.671 30827-30827/com.able.libs D/TouchEventTag: Activity--onTouchEvent:1,returnResult:false
01-31 18:12:28.671 30827-30827/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:false
小结
View不处理消息时,才会轮到ViewGroup
再来个小结
4.ViewGroup内部状态
4.1.ViewGroup的onTouchEvent对dispatchTouchEvent的影响
要测试就要先在View中dispatchTouchEvent=false,封堵View的流程
设置ViewGroup的onTouchEvent返回true
01-31 18:23:26.165 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 18:23:26.166 31388-31388/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:true
01-31 18:23:26.166 31388-31388/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:false
01-31 18:23:26.166 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:true
01-31 18:23:26.166 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:true
01-31 18:23:26.166 31388-31388/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:true
01-31 18:23:26.204 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:2,returnResult:true
01-31 18:23:26.204 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 18:23:26.204 31388-31388/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 18:23:26.254 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:2,returnResult:true
01-31 18:23:26.254 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 18:23:26.254 31388-31388/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 18:23:26.266 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:1,returnResult:true
01-31 18:23:26.266 31388-31388/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:1,returnResult:true
01-31 18:23:26.266 31388-31388/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:true
设置ViewGroup的onTouchEvent返回false
01-31 18:24:32.798 31553-31553/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 18:24:32.803 31553-31553/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:true
01-31 18:24:32.803 31553-31553/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:false
01-31 18:24:32.805 31553-31553/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:false
01-31 18:24:32.805 31553-31553/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:false
01-31 18:24:32.807 31553-31553/com.able.libs D/TouchEventTag: Activity--onTouchEvent:0,returnResult:false
01-31 18:24:32.807 31553-31553/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:false
01-31 18:24:32.853 31553-31553/com.able.libs D/TouchEventTag: Activity--onTouchEvent:1,returnResult:false
01-31 18:24:32.853 31553-31553/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:false
小结
4.2.ViewGroup的dispatchTouchEvent对onTouchEvent的影响
设置ViewGroup的onTouchEvent返回true,dispatchTouchEvent返回false
01-31 18:30:54.474 32098-32098/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 18:30:54.475 32098-32098/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:true
01-31 18:30:54.475 32098-32098/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:false
01-31 18:30:54.475 32098-32098/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:true
01-31 18:30:54.475 32098-32098/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:false
01-31 18:30:54.476 32098-32098/com.able.libs D/TouchEventTag: Activity--onTouchEvent:0,returnResult:false
01-31 18:30:54.476 32098-32098/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:false
01-31 18:30:54.497 32098-32098/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-31 18:30:54.497 32098-32098/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-31 18:30:54.557 32098-32098/com.able.libs D/TouchEventTag: Activity--onTouchEvent:1,returnResult:false
01-31 18:30:54.558 32098-32098/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:false
小结
说明和View一样,ViewGroup的dispatchTouchEvent优先级高于onTouchEvent
4.3.ViewGroup的onInterceptTouchEvent对ViewGroup的影响
ViewGroup的onInterceptTouchEvent返回true,onTouchEvent也返回true
01-26 16:36:02.152 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:true
01-26 16:36:02.152 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:true
01-26 16:36:02.152 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:true
01-26 16:36:02.152 8768-8768/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:true
01-26 16:36:02.190 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:2,returnResult:true
01-26 16:36:02.190 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-26 16:36:02.190 8768-8768/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-26 16:36:02.235 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:2,returnResult:true
01-26 16:36:02.235 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-26 16:36:02.235 8768-8768/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-26 16:36:02.235 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:1,returnResult:true
01-26 16:36:02.235 8768-8768/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:1,returnResult:true
01-26 16:36:02.235 8768-8768/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:true
小结
ViewGroup的onInterceptTouchEvent返回true,但是onTouchEvent返回false
01-26 16:32:57.300 8594-8594/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:true
01-26 16:32:57.301 8594-8594/com.able.libs D/TouchEventTag: ViewGroup--onTouchEvent:0,returnResult:false
01-26 16:32:57.301 8594-8594/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:false
01-26 16:32:57.302 8594-8594/com.able.libs D/TouchEventTag: Activity--onTouchEvent:0,returnResult:false
01-26 16:32:57.302 8594-8594/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:false
01-26 16:32:57.323 8594-8594/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-26 16:32:57.323 8594-8594/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-26 16:32:57.352 8594-8594/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-26 16:32:57.352 8594-8594/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-26 16:32:57.364 8594-8594/com.able.libs D/TouchEventTag: Activity--onTouchEvent:2,returnResult:false
01-26 16:32:57.364 8594-8594/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:false
01-26 16:32:57.365 8594-8594/com.able.libs D/TouchEventTag: Activity--onTouchEvent:1,returnResult:false
01-26 16:32:57.366 8594-8594/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:false
小结
再来个小结
ViewGroup的onInterceptTouchEvent拦截之后消息不再发给View,这也是onInterceptTouchEvent最主要的作用,因为通过dispatchTouchEvent拦截,都会先进入View再来询问ViewGroup
但是ViewGroup的拦截后,如果处理就给ViewGroup处理,如果不处理,还是上交给Activity
5.View、ViewGroup都想处理
前提是ViewGroup不要在onInterceptTouchEvent中拦截哦
设置View和ViewGroup的onTouchEvent都返回true
01-31 18:40:41.641 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:0,returnResult:false
01-31 18:40:41.643 32429-32429/com.able.libs D/TouchEventTag: View--onTouchEvent:0,returnResult:true
01-31 18:40:41.643 32429-32429/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:0,returnResult:true
01-31 18:40:41.643 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:0,returnResult:true
01-31 18:40:41.643 32429-32429/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:0,returnResult:true
01-31 18:40:41.663 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:2,returnResult:false
01-31 18:40:41.663 32429-32429/com.able.libs D/TouchEventTag: View--onTouchEvent:2,returnResult:true
01-31 18:40:41.663 32429-32429/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.663 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.663 32429-32429/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.687 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:2,returnResult:false
01-31 18:40:41.687 32429-32429/com.able.libs D/TouchEventTag: View--onTouchEvent:2,returnResult:true
01-31 18:40:41.687 32429-32429/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.688 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.688 32429-32429/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.691 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:2,returnResult:false
01-31 18:40:41.692 32429-32429/com.able.libs D/TouchEventTag: View--onTouchEvent:2,returnResult:true
01-31 18:40:41.692 32429-32429/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.692 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.692 32429-32429/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:2,returnResult:true
01-31 18:40:41.693 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--onInterceptTouchEvent:1,returnResult:false
01-31 18:40:41.694 32429-32429/com.able.libs D/TouchEventTag: View--onTouchEvent:1,returnResult:true
01-31 18:40:41.694 32429-32429/com.able.libs D/TouchEventTag: View--dispatchTouchEvent:1,returnResult:true
01-31 18:40:41.694 32429-32429/com.able.libs D/TouchEventTag: ViewGroup--dispatchTouchEvent:1,returnResult:true
01-31 18:40:41.694 32429-32429/com.able.libs D/TouchEventTag: Activity--dispatchTouchEvent:1,returnResult:true
小结
6.总结
消息流程是View--ViewGroup--Activity,所以不想处理时层层递交(Activity是终点站),想处理时,随时可以拦截
View和ViewGroup的内部,onTouchEvent决定了他们的dispatchTouchEvent的值,但是dispatchTouchEvent优先级又高于onTouchEvent(只要拦截,就一定交给当前对象处理,只要不拦截,就一定不交给当前对象处理)
ViewGroup的onInterceptTouchEvent决定了是否不向View派发消息,但是即使拦截了,如果ViewGroup没有处理,还是要上交的