Android_Touch事件的分发和消费机制

原创 2015年07月10日 21:26:21

我也是初学android的菜鸟,今天看书自己研究学习了android得Touch事件分发和消费机制,稍微做了一下总结,与大家一起分享,假如有理解错误的地方,欢迎大家提醒。

那么接下来让我们就开始进入正题。 在Android 中与 Touch 事件相关的方法包括以下三个方法:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)、onTouchEvent(MotionEvent ev);能够响应这些方法的控件包括:Activity、ViewGroup、View。下面是它们与Touch事件的对应表:

Touch 事件相关方法   方法功能  
  ViewGroup   
       View   
     Activity
  public boolean dispatchTouchEvent(MotionEvent ev) 事件分发 
 Yes  Yes  Yes
  public boolean onInterceptTouchEvent(MotionEvent ev)  
事件拦截 
 Yes  Yes  No
  public boolean onTouchEvent(MotionEvent ev) 事件响应 
 Yes  Yes  Yes

从上面这张表中我们可以看出 ViewGroup 和 View 对与 Touch 事件相关的三个方法的响应关系。Activity 根本就没有onInterceptTouchEvent(MotionEvent ev)方法,所以它不会对 onInterceptTouchEvent(MotionEvent ev) 也就是事件拦截进行响应。至于,Activity为什么没有拦截事件,这个也比较容易理解,因为Activity是作为最外层的一个容器,它进行拦截根本就没有意义。另外需要注意的是 View 对 dispatchTouchEvent(MotionEvent ev) (分发)和 onInterceptTouchEvent(MotionEvent ev) (拦截)的响应的前提是可以向该 View 中添加子 View,如果当前的 View 已经是一个最小的单元 View(比如 TextView,Button等),那么就无法向这个最小 View 中添加子 View,也就无法向子 View 进行事件的分发和拦截,所以它没有 dispatchTouchEvent(MotionEvent ev) 和 onInterceptTouchEvent(MotionEvent ev),只有 onTouchEvent(MotionEvent ev)。


下面是对这三个事件的分析:
(一)、事件分发:public boolean dispatchTouchEvent(MotionEvent ev)
Touch 事件发生时 Activity 的 dispatchTouchEvent(MotionEvent ev) 方法会以隧道方式(从根元素依次往下传递直到最内层子元素或在中间某一元素中由于某一条件停止传递)将事件传递给最外层 View 的 dispatchTouchEvent(MotionEvent ev) 方法,并由该 View 的 dispatchTouchEvent(MotionEvent ev) 方法对事件进行分发。


(二)、事件拦截:public boolean onInterceptTouchEvent(MotionEvent ev)
在外层 View 的 dispatchTouchEvent(MotionEvent ev) 方法返回系统默认的 super.dispatchTouchEvent(ev) 情况下,事件会自动的分发给当前 View 的 onInterceptTouchEvent 方法。onInterceptTouchEvent 的事件拦截逻辑如下:
  1. •如果 onInterceptTouchEvent 返回 true,则表示将事件进行拦截,并将拦截到的事件交由当前 View 的 onTouchEvent 进行处理; 
  2. •如果 onInterceptTouchEvent 返回 false,则表示将事件放行,当前 View 上的事件会被传递到子 View 上,再由子 View 的 dispatchTouchEvent 来开始这个事件的分发; 
  3. •如果 onInterceptTouchEvent 返回 super.onInterceptTouchEvent(ev),事件默认不会被拦截,并将拦截到的事件交由当前 View 的 onTouchEvent 进行处理。 

(三)、事件响应:public boolean onTouchEvent(MotionEvent ev)
在 dispatchTouchEvent 返回 super.dispatchTouchEvent(ev) 并且 onInterceptTouchEvent 返回 true 或返回 super.onInterceptTouchEvent(ev) 的情况下 onTouchEvent 会被调用。onTouchEvent 的事件响应逻辑如下:
  1. •如果事件传递到当前 View 的 onTouchEvent 方法,而该方法返回了 false,那么这个事件会从当前 View 向上传递,并且都是由上层 View 的 onTouchEvent 来接收,如果传递到上面的 onTouchEvent 也返回 false,这个事件就会“消失”,而且接收不到下一次事件。 
  2. •如果返回了 true 则会接收并消费该事件。 
  3. •如果返回 super.onTouchEvent(ev) 默认处理事件的逻辑和返回 false 时相同。


因为事件包括的动作很多,假如都考虑在内情况比较复杂,下面我们只从按下和抬起这两种状态来进行分析。下面是我的布局,因为xml和Activity中的代码都比较简单,在这就不贴出来了。让大家看一下我的log输出,


整个的是一个Activity,红色和黄色的是ViewGroup (都是继承LinearLayout)蓝色是一个view(继承了Button) 从图中可以看出它们是嵌套关系。

下面我们就开始做一些分析:

一、当所有的touchEvent都默认返回:

1、只点击父布局:

07-30 17:00:46.633: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN

07-30 17:00:46.633: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN

07-30 17:00:46.633: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN

07-30 17:00:46.644: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN

07-30 17:00:46.644: MainActivity: ==onTouchEvent --> ACTION_DOWN

07-30 17:00:46.827: MainActivity: ==dispatchTouchEvent --> ACTION_UP

07-30 17:00:46.827: MainActivity: ==onTouchEvent --> ACTION_UP   

2、点击父布局中的子布局:
07-30 17:04:00.599: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 17:04:00.604: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 17:04:00.604: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 17:04:00.604: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 17:04:00.614: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 17:04:00.654: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 17:04:00.654: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 17:04:00.654: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 17:04:00.865: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 17:04:00.865: MainActivity: ==onTouchEvent --> ACTION_UP
3、点击子布局中的Button:
07-30 19:12:25.479: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:12:25.479: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 19:12:25.614: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 19:12:25.684: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:12:25.694: MyButton: ==onTouchEvent --> ACTION_UP
07-30 19:12:25.724: MainActivity: ==onClick -->
二、当LinearLayoutFather中的onInterceptTouchEvent返回false:???为什么跟默认返回一致?
1、只点击父布局:
07-30 19:46:42.435: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:46:42.435: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:46:42.443: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:46:42.443: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:46:42.443: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:46:42.626: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:46:42.626: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 19:47:55.379: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:47:55.379: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:47:55.384: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 19:47:55.394: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:47:55.394: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:47:55.527: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:47:55.527: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 19:52:53.554: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:52:53.554: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:52:53.554: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:52:53.586: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:52:53.586: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:52:53.586: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 19:52:53.774: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:52:53.804: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 19:52:53.804: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:52:53.804: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 19:52:53.841: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 19:52:53.841: MyButton: ==onTouchEvent --> ACTION_UP
07-30 19:52:53.903: MainActivity: ==onClick --> 




三、当LinearLayoutFather中的onInterceptTouchEvent返回true:
1、只点击父布局:
07-30 19:57:52.385: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:57:52.385: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:57:52.385: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:57:52.385: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:57:52.394: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:57:52.485: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:57:52.485: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 19:59:26.684: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.837: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:59:26.837: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 19:59:26.684: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.684: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 19:59:26.837: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 19:59:26.837: MainActivity: ==onTouchEvent --> ACTION_UP






四、当LinearLayoutChilds中的onInterceptTouchEvent返回false:
1、只点击父布局:
07-30 20:08:25.894: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:08:25.894: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:08:26.094: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:08:26.094: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:09:22.347: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:09:22.347: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:09:22.354: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:09:22.354: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:09:22.365: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:09:22.441: MainActivity: ==dispatchTouchEvent --> ACTION_MOVE
07-30 20:09:22.447: MainActivity: ==onTouchEvent --> ACTION_MOVE
07-30 20:09:22.457: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:09:22.457: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 20:10:08.385: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:10:08.385: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:10:08.395: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:10:08.395: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:10:08.395: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:10:08.404: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:10:08.594: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:10:08.644: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:10:08.644: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:10:08.644: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:10:08.654: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:10:08.654: MyButton: ==onTouchEvent --> ACTION_UP
07-30 20:10:08.717: MainActivity: ==onClick --> 




五、当LinearLayoutChilds中的onInterceptTouchEvent返回true:
1、只点击父布局:
07-30 20:12:47.143: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:12:47.143: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:12:47.143: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:12:47.153: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:12:47.153: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:12:47.314: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:12:47.314: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:13:25.582: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:25.584: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:25.584: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:25.594: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:25.617: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:25.763: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:13:25.763: MainActivity: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 20:13:58.674: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:58.674: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:58.674: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:58.674: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:13:58.685: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:13:58.704: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:58.704: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:58.704: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:13:58.918: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:13:58.918: MainActivity: ==onTouchEvent --> ACTION_UP






六、当LinearLayoutFather中的onTouchEvent返回false:
1、只点击父布局:
07-30 20:23:45.933: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:23:45.933: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:23:45.933: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:23:45.933: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:23:45.944: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:23:46.068: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:23:46.068: MainActivity: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:26:08.985: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:08.985: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:09.004: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:09.114: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:09.114: MainActivity: ==onTouchEvent --> ACTION_UP




3、点击子布局中的Button:
07-30 20:26:58.767: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:58.774: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:58.774: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:58.785: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:26:58.785: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:26:58.785: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:26:58.933: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:58.933: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:58.933: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:26:58.933: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:26:58.944: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:26:58.944: MyButton: ==onTouchEvent --> ACTION_UP
07-30 20:26:58.955: MainActivity: ==onClick --> 




七、当LinearLayoutFather中的onTouchEvent返回true:
1、只点击父布局:
07-30 20:17:54.564: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:17:54.564: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:17:54.574: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:17:54.574: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:17:54.756: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:17:54.756: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:17:54.756: LinearLayoutFather: ==onTouchEvent --> ACTION_UP


2、点击父布局中的子布局:
07-30 20:18:52.710: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:18:52.710: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:18:52.714: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:18:52.724: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:18:52.828: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:18:52.834: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:18:52.834: LinearLayoutFather: ==onTouchEvent --> ACTION_UP


3、点击子布局中的Button:
07-30 20:20:26.579: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:20:26.579: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:20:26.585: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:20:26.824: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:20:26.884: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:20:26.894: MyButton: ==onTouchEvent --> ACTION_UP
07-30 20:20:26.975: MainActivity: ==onClick --> 




八、当MyButton中的onTouchEvent返回false:
1、点击子布局中的Button:
07-30 20:35:24.218: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:35:24.224: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.234: LinearLayoutChilds: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.234: LinearLayoutFather: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.234: MainActivity: ==onTouchEvent --> ACTION_DOWN
07-30 20:35:24.514: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:35:24.514: MainActivity: ==onTouchEvent --> ACTION_UP




九、当MyButton中的onTouchEvent返回true:
1、点击子布局中的Button:
07-30 20:34:12.458: MainActivity: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:34:12.458: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:34:12.458: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:34:12.484: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_DOWN
07-30 20:34:12.484: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_DOWN
07-30 20:34:12.484: MyButton: ==onTouchEvent --> ACTION_DOWN
07-30 20:34:12.627: MainActivity: ==dispatchTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutFather: ==dispatchTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutFather: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutChilds: ==dispatchTouchEvent --> ACTION_UP
07-30 20:34:12.634: LinearLayoutChilds: ==onInterceptTouchEvent --> ACTION_UP
07-30 20:34:12.634: MyButton: ==onTouchEvent --> ACTION_UP


好了我们暂时就分析到这里,希望我的文章可以帮助你进一步理解Android中的Touch事件的分发和消费机制
版权声明:本文为博主原创文章,未经博主允许不得转载。

Android View事件分发、拦截、消费机制

日常开发中,我们经常会给各种控件设置点击、触摸事件。如果滑动有冲突,还得去解决滑动冲突,所以对View的事件分发(dispatchTouchEvent)、消费(onTouchEvent)、拦截(onI...
  • SilentWeek
  • SilentWeek
  • 2016年12月27日 17:52
  • 1590

事件分发与消费(初学)

ii
  • u011809714
  • u011809714
  • 2016年03月25日 19:01
  • 441

事件的分发和消费机制

事件的分发和消费机制     一、简介  :   Activity或View类的onTouchEvent()回调函数会接收到touch事件。   一个完整的手势是...
  • ElevenDGQ
  • ElevenDGQ
  • 2016年05月24日 16:03
  • 252

Android 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev...
  • bgc525725278
  • bgc525725278
  • 2016年01月13日 17:33
  • 248

android 事件的分发和消费机制

android 中可以相应事件的类有 Activity ViewGroup View这三类。 事件的分发顺序为由上到下 会依次调用 。中间可被打断 即 ViewGroup的 @Override p...
  • tintick
  • tintick
  • 2016年08月03日 20:17
  • 140

Android43_Touch事件的分发和消费机制

TouchEvent事件处理及事件消费 一、简介  : 事件分发主要分为两部分:view的事件分发和viewgroup的事件分发。在探讨事件分发机制之前,先...
  • huohu1007
  • huohu1007
  • 2016年01月20日 10:32
  • 119

Android--Touch 事件的分发和消费机制

Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev)、onInterceptTouchEvent(MotionEvent ev)、...
  • u012815217
  • u012815217
  • 2015年09月14日 21:17
  • 312

事件的分发和消费机制详解

原创团队: 【总监】十二春秋之,3483099@qq.com; 【Master】zelo,616701261@qq.com;【运营】狼行天下,897221533@qq.com; 【产品设计】流浪猫,3...
  • u011215632
  • u011215632
  • 2017年08月18日 11:16
  • 585

Touch 事件的分发和消费机制

本文改自转载:  Android 中与 Touch 事件相关的方法包括: dispatchTouchEvent(MotionEvent ev)、 onInterceptTouchEv...
  • u010870955
  • u010870955
  • 2016年09月10日 08:57
  • 346

Android事件的分发与消费机制

事件的分发和消费机制       一、简介  : Activity或View类的onTouchEvent()回调函数会接收到touch事件。     一个完整的手势是从ACTION_DOWN...
  • qq_40239822
  • qq_40239822
  • 2017年11月29日 16:37
  • 71
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android_Touch事件的分发和消费机制
举报原因:
原因补充:

(最多只允许输入30个字)