关键词:下沉传递(Activity-ViewGroup-View);冒泡响应(View-ViewGroup-Activity);onTouch和onTouchEvent的区别
Android事件传递机制跟布局的层次密切相关,一个典型的布局包括根节点ViewGroup,子ViewGroup,以及最末端的子View,如下图:
![](https://img.haomeiwen.com/i2839011/efbb255341d061ad.png)
在这种结构中,上层是RootViewGroup,下层是子View,当我们点击子View的时候,点击事件从上层依次往下层传递,传递的过程调用dispatchTouchEvent和onInterceptTouchEvent函数。当事件传递到被点击的子View后,停止事件的传递,开始改变方向,依次向上层响应,响应的过程调用onTouch或onTouchEvent方法,如下图:
![](https://img.haomeiwen.com/i2839011/7eb5a044b21954ac.png)
由于事件的传递和响应有明确的方向,因此我称之为“下沉传递,冒泡响应”。下面结合我写的一个Demo给出验证过程。Demo布局很简单,红色背景的LinearLayout包含一个绿色背景的LinearLayout,包含一个白色背景的Textview,3个层级。当点击Textview时,事件传递方向红-绿-白,响应的方向相反,白-绿-红,如下图:
![](https://img.haomeiwen.com/i2839011/21842224e66a5b28.jpg)
Demo继承LinearLayout实现了一个MyLinearLayout,继承Textview实现了一个MyTextView,分别在dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent函数中添加日志输出:
public class MyLinearLayout extends LinearLayout {
...
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.e(MainActivity.TAG, getId() + " MyLinearLayout onInterceptTouchEvent");
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e(MainActivity.TAG, getId() + " MyLinearLayout dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e(MainActivity.TAG, getId() + " MyLinearLayout onTouchEvent");
return super.onTouchEvent(event);
}
}
public class MyTextView extends TextView {
...
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.e(MainActivity.TAG, getId() + " MyTextView dispatchTouchEvent");
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e(MainActivity.TAG, getId() + " MyTextView onTouchEvent");
return super.onTouchEvent(event);
}
}
布局文件:
<com.magic.wangdongliang.toucheventsystemtest.MyLinearLayout ...>
<com.magic.wangdongliang.toucheventsystemtest.MyLinearLayout ...>
<com.magic.wangdongliang.toucheventsystemtest.MyTextView .../>
</com.magic.wangdongliang.toucheventsystemtest.MyLinearLayout>
</com.magic.wangdongliang.toucheventsystemtest.MyLinearLayout>
点击最底层的MyTextView,输出如下:
![](https://img.haomeiwen.com/i2839011/d97e11e813ecbb01.png)
可以清楚的看到事件下沉传递和冒泡响应的过程。如果要在某一步消费并且拦截掉事件的传递或响应,只需要将函数返回设为True即可。
例如将MyLinearLayout的onInterceptTouchEvent函数返回设为True:
public class MyLinearLayout extends LinearLayout {
...
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.e(MainActivity.TAG, getId() + " MyLinearLayout onInterceptTouchEvent");
return true;
}
...
}
输入如下:
![](https://img.haomeiwen.com/i2839011/77d7ee5c28d5c999.png)
可以看出,此时事件被根布局的MyLinearLayout消费并返回,没有继续往下传递。这里虽然没有往更下层的布局传递,但是上层的布局还是会收到消息,并继续响应,如Activity的onTouchEvent函数。
如果同时把MyLinearLayout的onTouchEvent返回设为true,那既不会往下次传递事件,也不会往上层布局传递响应,如下示例:
public class MyLinearLayout extends LinearLayout {
...
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.e(MainActivity.TAG, getId() + " MyLinearLayout onInterceptTouchEvent");
return true;
}
...
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e(MainActivity.TAG, getId() + " MyLinearLayout onTouchEvent");
return true;
}
}
点击最底层的MyTextView,输出如下:
![](https://img.haomeiwen.com/i2839011/265d7d1345ac6c44.png)
那onTouch和onTouchEvent两个函数有什么区别呢?
1. 从响应顺序上,onTouchListener的onTouch方法优先级比onTouchEvent高,会先触发。
假如onTouch方法返回false,会接着触发onTouchEvent,反之onTouchEvent方法不会被调用。
2. 从使用方式上,在自定义View里使用onTouchEvent,系统提供的View使用onTouch接口。
参考:
http://codetheory.in/understanding-android-input-touch-events/
网友评论