准备工作
对于Android事件处理分为两类:
-
对于Activity和View: 只有两种事件:
分发: dispatchTouchEvent函数
消费: onTouchEvent函数 -
对于ViewGroup: 全部三种事件:
分发: dispatchTouchEvent函数
拦截:onInterceptTouchEvent函数
消费: onTouchEvent函数
重写一个ViewGroup和一个View,代码如下
EventViewGroup.java
public class EventViewGroup extends LinearLayout {
public EventViewGroup(Context context) {
super(context);
}
public EventViewGroup(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("event","----------->dispatchTouchEvent:::ViewGroup----" + ev.getAction());
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.e("event","----------->onInterceptTouchEvent:::ViewGroup----" + ev.getAction());
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("event","----------->onTouchEvent:::ViewGroup----" + event.getAction());
return super.onTouchEvent(event);
}
}
EventView.java
public class EventView extends View {
public EventView(Context context) {
super(context);
}
public EventView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("event", "----------->dispatchTouchEvent:::View----" + ev.getAction());
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("event", "----------->onTouchEvent:::View----" + event.getAction());
return super.onTouchEvent(event);
}
}
MainActivity.java
package com.gerry.dispatchevent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import com.gerry.dispatchevent.view.EventView;
import com.gerry.dispatchevent.view.EventViewB;
import com.gerry.dispatchevent.viewgroup.EventViewGroup;
import com.gerry.dispatchevent.viewgroup.EventViewGroupB;
public class MainActivity extends AppCompatActivity {
private EventViewGroup evg;
private EventView ev;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
evgA= findViewById(R.id.evg);
evA= findViewById(R.id.ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("event", "----------->dispatchTouchEvent:::MainActivity----" + ev.getAction());
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("event", "----------->onTouchEvent:::MainActivity----" + event.getAction());
return super.onTouchEvent(event);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".MainActivity">
<com.gerry.dispatchevent.viewgroup.EventViewGroup
android:id="@+id/evg"
android:layout_width="match_parent"
android:layout_height="500px"
android:layout_marginTop="250px"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:orientation="vertical">
<com.gerry.dispatchevent.view.EventView
android:id="@+id/ev"
android:layout_width="200px"
android:layout_height="150px"
android:background="@android:color/holo_blue_light" />
</com.gerry.dispatchevent.viewgroup.EventViewGroup>
</FrameLayout>
开始分析
0为按下动作,1为抬起动作。
1. 对事件不进行任何修改
- 触摸Activity
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->onTouchEvent:::View----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
结论:
在默认情况下,Activity会把事件向下依次按照层级分发到触摸的最后一个ViewGroup或者View,然后会执行最后一个ViewGroup或者View的onTouchEvent()方法,然后又向上依次按照层级去触发onTouchEvent()事件,中途若没有被消费,就会传递直到activity,整个流程呈U形,隧道式向下分发,然后冒泡式向上处理。
2. 修改ViewGroup的dispatchTouchEvent方法,返回false。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
3. 修改View的dispatchTouchEvent方法,返回false。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
4. 修改ViewGroup的dispatchTouchEvent方法,返回true。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->dispatchTouchEvent:::ViewGroup----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->dispatchTouchEvent:::ViewGroup----1
5. 修改View的dispatchTouchEvent方法,返回true。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->dispatchTouchEvent:::ViewGroup----1
E/event: ----------->onInterceptTouchEvent:::ViewGroup----1
E/event: ----------->dispatchTouchEvent:::View----1
从2,3,4,5可以得出的结论:
- 当dispatchTouchEvent返回false时,事件不会继续往下分发,无论是ViewGroup还是View,都会从上一级的onTouchEvent事件向上传递,不会执行自己的onTouchEvent()方法
- 当dispatchTouchEvent返回true时,表示事件直接被消费,这个事件也就停止分发且不会逆向向上传递,直接结束了。
- 返回super:事件将会继续向下分发,直到事件被消费为止。
6. 修改ViewGroup的onInterceptTouchEvent方法,返回false。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->onTouchEvent:::View----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
7. 修改ViewGroup的onInterceptTouchEvent方法,返回true。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
从6,7可以得出的结论:
- 当onInterceptTouchEvnt返回false/super(默认)时,事件将会正常向下分发,分发至下级的dispatchTouchEvent方法 再次判断是否分发事件。
- 当onInterceptTouchEvent返回true时,表示ViewGroup容器拦截后续事件,会执行该ViewGroup的onTouchEvent()方法然后停止向下分发转而通过onTouchEvent()向上传递,直到最终被消费
8. 修改ViewGroup的onTouchEvent方法,返回false。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->onTouchEvent:::View----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
9. 修改ViewGroup的onTouchEvent方法,返回true。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->dispatchTouchEvent:::ViewGroup----1
E/event: ----------->onTouchEvent:::ViewGroup----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->onTouchEvent:::View----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->dispatchTouchEvent:::ViewGroup----1
E/event: ----------->onTouchEvent:::ViewGroup----1
10. 修改View的onTouchEvent方法,返回false。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->onTouchEvent:::View----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
11. 修改View的onTouchEvent方法,返回true。
- 触摸ViewGroup
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::ViewGroup----0
E/event: ----------->onTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->onTouchEvent:::MainActivity----1
- 触摸View
E/event: ----------->dispatchTouchEvent:::MainActivity----0
E/event: ----------->dispatchTouchEvent:::ViewGroup----0
E/event: ----------->onInterceptTouchEvent:::ViewGroup----0
E/event: ----------->dispatchTouchEvent:::View----0
E/event: ----------->onTouchEvent:::View----0
E/event: ----------->dispatchTouchEvent:::MainActivity----1
E/event: ----------->dispatchTouchEvent:::ViewGroup----1
E/event: ----------->onInterceptTouchEvent:::ViewGroup----1
E/event: ----------->dispatchTouchEvent:::View----1
E/event: ----------->onTouchEvent:::View----1
从8,9,10,11可以得出的结论:
- 当onTouchEvent返回false/super(默认)时,不消费掉此次事件,事件将会层层向上传递,直到被消费。
- 当onTouchEvent返回true时,立即消费掉事件,事件将不会向上传递,事件到此终止。
最终结论
dispatchTouchEvent 分发
- 当dispatchTouchEvent返回false时,事件不会继续往下分发,无论是ViewGroup还是View,都会从上一级的onTouchEvent事件向上传递,不会执行自己的onTouchEvent()方法
- 当dispatchTouchEvent返回true时,表示事件直接被消费,这个事件也就停止分发且不会逆向向上传递,直接结束了。
- 返回super:事件将会继续向下分发,直到事件被消费为止。
onInterceptTouchEvent 拦截
- 当onInterceptTouchEvnt返回false/super(默认)时,事件将会正常向下分发,分发至下级的dispatchTouchEvent方法 再次判断是否分发事件。
- 当onInterceptTouchEvent返回true时,表示ViewGroup容器拦截后续事件,会执行该ViewGroup的onTouchEvent()方法然后停止向下分发转而通过onTouchEvent()向上传递,直到最终被消费
onTouchEvent 消费
- 当onTouchEvent返回false/super(默认)时,不消费掉此次事件,事件将会层层向上传递,直到被消费。
- 当onTouchEvent返回true时,立即消费掉事件,事件将不会向上传递,事件到此终止。
参考链接:
https://www.jianshu.com/p/fc0590afb1bf
https://www.jianshu.com/p/66a80ff0133b
网友评论