了解Android View事件的传递过程,是编写复杂控件、解决ScrollView嵌套等问题的基础。
以touch事件为例,和其相关的回调函数有:
- dispatchTouchEvent,进行事件的分发
- onInterceptTouchEvent,ViewGroup对事件进行拦截
- onTouchEvent,响应具体的touch事件
下面通过一个简单的demo来观察这三个函数是如何调用的。
demo包括两部分,CustomViewGroup和CustomView,通过重写上面的三个方法,在其中输出日志来观察调用过程。
CustomViewGroup代码如下:
public class CustomViewGroup extends FrameLayout {
private static final String TAG = "CustomViewGroup";
public CustomViewGroup(Context context) {
super(context);
}
public CustomViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "onTouchEvent");
return super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d(TAG, "onInterceptTouchEvent");
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d(TAG, "dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
}
CustomView代码如下:
public class CustomView extends View {
private static final String TAG = "CustomView";
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "onTouchEvent");
return super.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d(TAG, "dispatchTouchEvent");
return super.dispatchTouchEvent(event);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xiaomi.shop.testdemo.MainActivity">
<com.xiaomi.shop.testdemo.view.event.CustomViewGroup
android:background="#ff0"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.xiaomi.shop.testdemo.view.event.CustomView
android:background="#f00"
android:layout_width="200dp"
android:layout_height="200dp" />
</com.xiaomi.shop.testdemo.view.event.CustomViewGroup>
</FrameLayout>
运行demo,界面如下:
红色区域为CustomView,黄色区域为CustomViewGroup
点击红色区域,看到日志输出如下:
05-08 11:44:55.304 12481-12481/com.xiaomi.shop.testdemo D/CustomViewGroup: dispatchTouchEvent
05-08 11:44:55.304 12481-12481/com.xiaomi.shop.testdemo D/CustomViewGroup: onInterceptTouchEvent
05-08 11:44:55.304 12481-12481/com.xiaomi.shop.testdemo D/CustomView: dispatchTouchEvent
05-08 11:44:55.304 12481-12481/com.xiaomi.shop.testdemo D/CustomView: onTouchEvent
05-08 11:44:55.305 12481-12481/com.xiaomi.shop.testdemo D/CustomViewGroup: onTouchEvent
1. 默认情况
dispatchTouchEvent,进行事件的分发
onInterceptTouchEvent,ViewGroup对事件进行拦截,默认返回false,即不拦截
onTouchEvent,响应具体的touch事件,默认返回false,即无响应
由上面的日志输出可以看出,默认情况下,函数的调用顺序为:
ViewGroup.dispatchTouchEvent
ViewGroup.onInterceptTouchEvent
View.dispatchTouchEvent
View.onTouchEvent
ViewGroup.onTouchEvent
需要注意的是,上面的日志只在手指点到红色区域后输出一次,手指滑动时,并没有新的日志输出。因为CustomView的onTouchEvent返回的false,所以CustomViewGroup认为CustomView不处理touch事件,所以后续的点击事件便不再向其分发。如何没有子View处理touch事件的话,那么就会自己处理,所以最后会调用到CustomViewGroup的onTouchEvent。
2. CustomView的onTouchEvent返回true
下面把CustomView的onTouchEvent的返回值改为true,表示处理touch事件,此时的日志输出为:
05-08 15:27:05.354 24788-24788/com.xiaomi.shop.testdemo D/CustomViewGroup: dispatchTouchEvent
05-08 15:27:05.354 24788-24788/com.xiaomi.shop.testdemo D/CustomViewGroup: onInterceptTouchEvent
05-08 15:27:05.354 24788-24788/com.xiaomi.shop.testdemo D/CustomView: dispatchTouchEvent
05-08 15:27:05.355 24788-24788/com.xiaomi.shop.testdemo D/CustomView: onTouchEvent
05-08 15:27:05.380 24788-24788/com.xiaomi.shop.testdemo D/CustomViewGroup: dispatchTouchEvent
05-08 15:27:05.380 24788-24788/com.xiaomi.shop.testdemo D/CustomViewGroup: onInterceptTouchEvent
05-08 15:27:05.380 24788-24788/com.xiaomi.shop.testdemo D/CustomView: dispatchTouchEvent
05-08 15:27:05.380 24788-24788/com.xiaomi.shop.testdemo D/CustomView: onTouchEvent
...
这次跟前面有两个不同,首先是点击和滑动都会输出日志,因为CustomView对touch事件感兴趣,所以后续的事件都会传递给它。另外这次没有调用CustomViewGroup的onTouchEvent函数,这是因为子View处理了touch事件,所以自己便不再处理。
3. 打开事件拦截
将CustomViewGroup的onInterceptTouchEvent函数的返回值改为true,启动事件拦截。在红色区域点击滑动,日志输出如下:
05-08 15:35:26.393 8556-8556/com.xiaomi.shop.testdemo D/CustomViewGroup: dispatchTouchEvent
05-08 15:35:26.394 8556-8556/com.xiaomi.shop.testdemo D/CustomViewGroup: onInterceptTouchEvent
05-08 15:35:26.394 8556-8556/com.xiaomi.shop.testdemo D/CustomViewGroup: onTouchEvent
CustomView的任何函数都没有调用。