标签:
概要:
Activity |dispatchTouchEvent ========================》 ^onTouchEvent -- PhotoWindow.FragmentLayout
ViewGroup |dispatchTouchEvent = onInterceptTouchEvent 》 |onTouchEvent
View |dispatchTouchEvent ========================》 |onTouchEvent
其中,
角色:Activity、ViewGroup、View,其中ViewGroup继承View
方法:dispatch分发、onTouchEvent处理、onIntercept拦截/消费,最后一个为ViewGroup独有
事件:TouchEvent,包括一次DOWN、多次MOVE、一次UP,组成onClick,onLongClick,onScroll等事件
如图,
触摸事件总是先被派发,然后被处理,中间如果存在ViewGroup时,可以被拦截进而处理;
在向下传递不被处理的情况下,事件冒泡上传直到结束;
对于触摸监听器,直接影响View的事件派发
了解触摸事件的分发机制是自定义控件的基础。
测试:
1、均默认,且未指定监听器
06-23 15:58:39.551: I/MyActivity(11132): -->dispatchTouchEvent(DOWN)
06-23 15:58:39.551: I/MyLinearLayout(11132): -->dispatchTouchEvent(DOWN)
06-23 15:58:39.551: I/MyLinearLayout(11132): -->onInterceptTouchEvent(DOWN)
06-23 15:58:39.551: I/MyLinearLayout(11132): <--false:onInterceptTouchEvent(DOWN)
06-23 15:58:39.551: I/MyTextView(11132): -->dispatchTouchEvent(DOWN)
06-23 15:58:39.551: I/MyTextView(11132): -->onTouchEvent(DOWN)
06-23 15:58:39.551: I/MyTextView(11132): <--false:onTouchEvent(DOWN)
06-23 15:58:39.551: I/MyTextView(11132): <--false:dispatchTouchEvent(DOWN)
06-23 15:58:39.551: I/MyLinearLayout(11132): -->onTouchEvent(DOWN)
06-23 15:58:39.551: I/MyLinearLayout(11132): <--false:onTouchEvent(DOWN)
06-23 15:58:39.551: I/MyLinearLayout(11132): <--false:dispatchTouchEvent(DOWN)
06-23 15:58:39.551: I/MyActivity(11132): -->onTouchEvent(DOWN)
06-23 15:58:39.551: I/MyActivity(11132): <--false:onTouchEvent(DOWN)
06-23 15:58:39.551: I/MyActivity(11132): <--false:dispatchTouchEvent(DOWN)
06-23 15:58:39.641: I/MyActivity(11132): -->dispatchTouchEvent(MOVE)
06-23 15:58:39.641: I/MyActivity(11132): -->onTouchEvent(MOVE)
06-23 15:58:39.641: I/MyActivity(11132): <--false:onTouchEvent(MOVE)
06-23 15:58:39.641: I/MyActivity(11132): <--false:dispatchTouchEvent(MOVE)
06-23 15:58:39.931: I/MyActivity(11132): -->dispatchTouchEvent(UP)
06-23 15:58:39.931: I/MyActivity(11132): -->onTouchEvent(UP)
06-23 15:58:39.931: I/MyActivity(11132): <--false:onTouchEvent(UP)
06-23 15:58:39.931: I/MyActivity(11132): <--false:dispatchTouchEvent(UP)
-- 事件最终冒泡至MyActivity
2、MyActivity未派发DOWN事件
06-23 16:02:20.321: I/MyActivity(11673): -->dispatchTouchEvent(DOWN)
06-23 16:02:20.321: I/MyActivity(11673): <--true:dispatchTouchEvent(DOWN)
06-23 16:02:20.361: I/MyActivity(11673): -->dispatchTouchEvent(MOVE)
06-23 16:02:20.361: I/MyActivity(11673): -->onTouchEvent(MOVE)
06-23 16:02:20.361: I/MyActivity(11673): <--false:onTouchEvent(MOVE)
06-23 16:02:20.361: I/MyActivity(11673): <--false:dispatchTouchEvent(MOVE)
06-23 16:02:20.371: I/MyActivity(11673): -->dispatchTouchEvent(UP)
06-23 16:02:20.371: I/MyActivity(11673): -->onTouchEvent(UP)
06-23 16:02:20.371: I/MyActivity(11673): <--false:onTouchEvent(UP)
06-23 16:02:20.371: I/MyActivity(11673): <--false:dispatchTouchEvent(UP)
-- 无人得到DOWN事件,后续事件由MyActivity得到
3、MyLinearLayout未派发DOWN事件
06-23 16:05:35.411: I/MyActivity(12122): -->dispatchTouchEvent(DOWN)
06-23 16:05:35.411: I/MyLinearLayout(12122): -->dispatchTouchEvent(DOWN)
06-23 16:05:35.411: I/MyLinearLayout(12122): <--true:dispatchTouchEvent(DOWN)
06-23 16:05:35.411: I/MyActivity(12122): <--true:dispatchTouchEvent(DOWN)
06-23 16:05:35.481: I/MyActivity(12122): -->dispatchTouchEvent(MOVE)
06-23 16:05:35.481: I/MyLinearLayout(12122): -->dispatchTouchEvent(MOVE)
06-23 16:05:35.481: I/MyLinearLayout(12122): -->onTouchEvent(MOVE)
06-23 16:05:35.481: I/MyLinearLayout(12122): <--false:onTouchEvent(MOVE)
06-23 16:05:35.481: I/MyLinearLayout(12122): <--false:dispatchTouchEvent(MOVE)
06-23 16:05:35.481: I/MyActivity(12122): -->onTouchEvent(MOVE)
06-23 16:05:35.481: I/MyActivity(12122): <--false:onTouchEvent(MOVE)
06-23 16:05:35.481: I/MyActivity(12122): <--false:dispatchTouchEvent(MOVE)
06-23 16:05:35.501: I/MyActivity(12122): -->dispatchTouchEvent(UP)
06-23 16:05:35.501: I/MyLinearLayout(12122): -->dispatchTouchEvent(UP)
06-23 16:05:35.501: I/MyLinearLayout(12122): -->onTouchEvent(UP)
06-23 16:05:35.501: I/MyLinearLayout(12122): <--false:onTouchEvent(UP)
06-23 16:05:35.501: I/MyLinearLayout(12122): <--false:dispatchTouchEvent(UP)
06-23 16:05:35.501: I/MyActivity(12122): -->onTouchEvent(UP)
06-23 16:05:35.501: I/MyActivity(12122): <--false:onTouchEvent(UP)
06-23 16:05:35.501: I/MyActivity(12122): <--false:dispatchTouchEvent(UP)
-- 无人得到DOWN事件,后续事件由MyLinearLayout、MyActivity得到
4、MyLinearLayout未派发任何事件
06-23 16:13:04.011: I/MyActivity(12958): -->dispatchTouchEvent(DOWN)
06-23 16:13:04.011: I/MyLinearLayout(12958): -->dispatchTouchEvent(DOWN)
06-23 16:13:04.011: I/MyLinearLayout(12958): <--true:dispatchTouchEvent(DOWN)
06-23 16:13:04.011: I/MyActivity(12958): <--true:dispatchTouchEvent(DOWN)
06-23 16:13:04.071: I/MyActivity(12958): -->dispatchTouchEvent(MOVE)
06-23 16:13:04.071: I/MyLinearLayout(12958): -->dispatchTouchEvent(MOVE)
06-23 16:13:04.071: I/MyLinearLayout(12958): <--true:dispatchTouchEvent(MOVE)
06-23 16:13:04.071: I/MyActivity(12958): <--true:dispatchTouchEvent(MOVE)
06-23 16:13:04.151: I/MyActivity(12958): -->dispatchTouchEvent(UP)
06-23 16:13:04.151: I/MyLinearLayout(12958): -->dispatchTouchEvent(UP)
06-23 16:13:04.151: I/MyLinearLayout(12958): <--true:dispatchTouchEvent(UP)
06-23 16:13:04.151: I/MyActivity(12958): <--true:dispatchTouchEvent(UP)
-- 无人得到任何事件
-- 综合1、2、3、4的测试结果,dispatchTouchEvent用于进行事件的派发,返回true时标识该类型事件已完成派发,返回false时意味着冒泡到上级继续处理
4、MyLinearLayout拦截DOWN事件
06-23 16:21:15.211: I/MyActivity(13579): -->dispatchTouchEvent(DOWN)
06-23 16:21:15.211: I/MyLinearLayout(13579): -->dispatchTouchEvent(DOWN)
06-23 16:21:15.211: I/MyLinearLayout(13579): -->onInterceptTouchEvent(DOWN)
06-23 16:21:15.211: I/MyLinearLayout(13579): <--true:onInterceptTouchEvent(DOWN)
06-23 16:21:15.211: I/MyLinearLayout(13579): -->onTouchEvent(DOWN)
06-23 16:21:15.211: I/MyLinearLayout(13579): <--false:onTouchEvent(DOWN)
06-23 16:21:15.211: I/MyLinearLayout(13579): <--false:dispatchTouchEvent(DOWN)
06-23 16:21:15.211: I/MyActivity(13579): -->onTouchEvent(DOWN)
06-23 16:21:15.211: I/MyActivity(13579): <--false:onTouchEvent(DOWN)
06-23 16:21:15.211: I/MyActivity(13579): <--false:dispatchTouchEvent(DOWN)
06-23 16:21:15.261: I/MyActivity(13579): -->dispatchTouchEvent(MOVE)
06-23 16:21:15.261: I/MyActivity(13579): -->onTouchEvent(MOVE)
06-23 16:21:15.261: I/MyActivity(13579): <--false:onTouchEvent(MOVE)
06-23 16:21:15.261: I/MyActivity(13579): <--false:dispatchTouchEvent(MOVE)
06-23 16:21:15.291: I/MyActivity(13579): -->dispatchTouchEvent(UP)
06-23 16:21:15.291: I/MyActivity(13579): -->onTouchEvent(UP)
06-23 16:21:15.291: I/MyActivity(13579): <--false:onTouchEvent(UP)
06-23 16:21:15.291: I/MyActivity(13579): <--false:dispatchTouchEvent(UP)
-- DOWN事件由MyLinearLayout、MyActivity得到,后续事件由MyActivity得到
-- 综合之前的测试结果,onInterceptTouchEvent仅在ViewGroup进行派发时才会执行调用
5、MyLinearLayout拦截并消费DOWN事件
06-23 16:25:07.241: I/MyActivity(14077): -->dispatchTouchEvent(DOWN)
06-23 16:25:07.241: I/MyLinearLayout(14077): -->dispatchTouchEvent(DOWN)
06-23 16:25:07.251: I/MyLinearLayout(14077): -->onInterceptTouchEvent(DOWN)
06-23 16:25:07.251: I/MyLinearLayout(14077): <--true:onInterceptTouchEvent(DOWN)
06-23 16:25:07.251: I/MyLinearLayout(14077): -->onTouchEvent(DOWN)
06-23 16:25:07.251: I/MyLinearLayout(14077): <--true:onTouchEvent(DOWN)
06-23 16:25:07.251: I/MyLinearLayout(14077): <--true:dispatchTouchEvent(DOWN)
06-23 16:25:07.251: I/MyActivity(14077): <--true:dispatchTouchEvent(DOWN)
06-23 16:25:07.261: I/MyActivity(14077): -->dispatchTouchEvent(MOVE)
06-23 16:25:07.261: I/MyLinearLayout(14077): -->dispatchTouchEvent(MOVE)
06-23 16:25:07.261: I/MyLinearLayout(14077): -->onTouchEvent(MOVE)
06-23 16:25:07.261: I/MyLinearLayout(14077): <--false:onTouchEvent(MOVE)
06-23 16:25:07.261: I/MyLinearLayout(14077): <--false:dispatchTouchEvent(MOVE)
06-23 16:25:07.261: I/MyActivity(14077): -->onTouchEvent(MOVE)
06-23 16:25:07.261: I/MyActivity(14077): <--false:onTouchEvent(MOVE)
06-23 16:25:07.261: I/MyActivity(14077): <--false:dispatchTouchEvent(MOVE)
06-23 16:25:07.271: I/MyActivity(14077): -->dispatchTouchEvent(UP)
06-23 16:25:07.271: I/MyLinearLayout(14077): -->dispatchTouchEvent(UP)
06-23 16:25:07.271: I/MyLinearLayout(14077): -->onTouchEvent(UP)
06-23 16:25:07.271: I/MyLinearLayout(14077): <--false:onTouchEvent(UP)
06-23 16:25:07.271: I/MyLinearLayout(14077): <--false:dispatchTouchEvent(UP)
06-23 16:25:07.271: I/MyActivity(14077): -->onTouchEvent(UP)
06-23 16:25:07.271: I/MyActivity(14077): <--false:onTouchEvent(UP)
06-23 16:25:07.271: I/MyActivity(14077): <--false:dispatchTouchEvent(UP)
-- down事件由MyActivity得到并处理,后续事件由MyLinearLayout、MyActivity得到
5、MyLinearLayout拦截DOWN事件、处理所有事件
06-23 16:29:19.331: I/MyActivity(14591): -->dispatchTouchEvent(DOWN)
06-23 16:29:19.331: I/MyLinearLayout(14591): -->dispatchTouchEvent(DOWN)
06-23 16:29:19.331: I/MyLinearLayout(14591): -->onInterceptTouchEvent(DOWN)
06-23 16:29:19.341: I/MyLinearLayout(14591): <--true:onInterceptTouchEvent(DOWN)
06-23 16:29:19.341: I/MyLinearLayout(14591): -->onTouchEvent(DOWN)
06-23 16:29:19.341: I/MyLinearLayout(14591): <--true:onTouchEvent(DOWN)
06-23 16:29:19.341: I/MyLinearLayout(14591): <--true:dispatchTouchEvent(DOWN)
06-23 16:29:19.341: I/MyActivity(14591): <--true:dispatchTouchEvent(DOWN)
06-23 16:29:19.421: I/MyActivity(14591): -->dispatchTouchEvent(MOVE)
06-23 16:29:19.421: I/MyLinearLayout(14591): -->dispatchTouchEvent(MOVE)
06-23 16:29:19.421: I/MyLinearLayout(14591): -->onTouchEvent(MOVE)
06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:onTouchEvent(MOVE)
06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:dispatchTouchEvent(MOVE)
06-23 16:29:19.421: I/MyActivity(14591): <--true:dispatchTouchEvent(MOVE)
06-23 16:29:19.421: I/MyActivity(14591): -->dispatchTouchEvent(UP)
06-23 16:29:19.421: I/MyLinearLayout(14591): -->dispatchTouchEvent(UP)
06-23 16:29:19.421: I/MyLinearLayout(14591): -->onTouchEvent(UP)
06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:onTouchEvent(UP)
06-23 16:29:19.421: I/MyLinearLayout(14591): <--true:dispatchTouchEvent(UP)
06-23 16:29:19.421: I/MyActivity(14591): <--true:dispatchTouchEvent(UP)
-- 所有事件由MyLinearLayout得到并处理
-- 综合之前的测试结果,事件被消费后任何人不再得到该事件
6、MyTextView消费DOWN事件
06-23 16:44:42.351: I/MyActivity(15674): -->dispatchTouchEvent(DOWN)
06-23 16:44:42.351: I/MyLinearLayout(15674): -->dispatchTouchEvent(DOWN)
06-23 16:44:42.351: I/MyLinearLayout(15674): -->onInterceptTouchEvent(DOWN)
06-23 16:44:42.361: I/MyLinearLayout(15674): <--false:onInterceptTouchEvent(DOWN)
06-23 16:44:42.361: I/MyTextView(15674): -->dispatchTouchEvent(DOWN)
06-23 16:44:42.361: I/MyTextView(15674): -->onTouchEvent(DOWN)
06-23 16:44:42.361: I/MyTextView(15674): <--true:onTouchEvent(DOWN)
06-23 16:44:42.361: I/MyTextView(15674): <--true:dispatchTouchEvent(DOWN)
06-23 16:44:42.361: I/MyLinearLayout(15674): <--true:dispatchTouchEvent(DOWN)
06-23 16:44:42.361: I/MyActivity(15674): <--true:dispatchTouchEvent(DOWN)
06-23 16:44:42.401: I/MyActivity(15674): -->dispatchTouchEvent(MOVE)
06-23 16:44:42.401: I/MyLinearLayout(15674): -->dispatchTouchEvent(MOVE)
06-23 16:44:42.401: I/MyLinearLayout(15674): -->onInterceptTouchEvent(MOVE)
06-23 16:44:42.401: I/MyLinearLayout(15674): <--false:onInterceptTouchEvent(MOVE)
06-23 16:44:42.401: I/MyTextView(15674): -->dispatchTouchEvent(MOVE)
06-23 16:44:42.401: I/MyTextView(15674): -->onTouchEvent(MOVE)
06-23 16:44:42.401: I/MyTextView(15674): <--false:onTouchEvent(MOVE)
06-23 16:44:42.401: I/MyTextView(15674): <--false:dispatchTouchEvent(MOVE)
06-23 16:44:42.411: I/MyLinearLayout(15674): <--false:dispatchTouchEvent(MOVE)
06-23 16:44:42.411: I/MyActivity(15674): -->onTouchEvent(MOVE)
06-23 16:44:42.411: I/MyActivity(15674): <--false:onTouchEvent(MOVE)
06-23 16:44:42.411: I/MyActivity(15674): <--false:dispatchTouchEvent(MOVE)
06-23 16:44:42.411: I/MyActivity(15674): -->dispatchTouchEvent(UP)
06-23 16:44:42.411: I/MyLinearLayout(15674): -->dispatchTouchEvent(UP)
06-23 16:44:42.411: I/MyLinearLayout(15674): -->onInterceptTouchEvent(UP)
06-23 16:44:42.411: I/MyLinearLayout(15674): <--false:onInterceptTouchEvent(UP)
06-23 16:44:42.411: I/MyTextView(15674): -->dispatchTouchEvent(UP)
06-23 16:44:42.411: I/MyTextView(15674): -->onTouchEvent(UP)
06-23 16:44:42.411: I/MyTextView(15674): <--false:onTouchEvent(UP)
06-23 16:44:42.411: I/MyTextView(15674): <--false:dispatchTouchEvent(UP)
06-23 16:44:42.411: I/MyLinearLayout(15674): <--false:dispatchTouchEvent(UP)
06-23 16:44:42.411: I/MyActivity(15674): -->onTouchEvent(UP)
06-23 16:44:42.411: I/MyActivity(15674): <--false:onTouchEvent(UP)
06-23 16:44:42.411: I/MyActivity(15674): <--false:dispatchTouchEvent(UP)
-- down事件由MyTextView得到并消费,后续事件被MyTextView、MyActivity得到
7、MyTextView消费DOWN事件、MyLinearLayout拦截MOVE事件
06-23 16:50:38.431: I/MyActivity(16115): -->dispatchTouchEvent(DOWN)
06-23 16:50:38.431: I/MyLinearLayout(16115): -->dispatchTouchEvent(DOWN)
06-23 16:50:38.431: I/MyLinearLayout(16115): -->onInterceptTouchEvent(DOWN)
06-23 16:50:38.431: I/MyLinearLayout(16115): <--false:onInterceptTouchEvent(DOWN)
06-23 16:50:38.431: I/MyTextView(16115): -->dispatchTouchEvent(DOWN)
06-23 16:50:38.431: I/MyTextView(16115): -->onTouchEvent(DOWN)
06-23 16:50:38.431: I/MyTextView(16115): <--true:onTouchEvent(DOWN)
06-23 16:50:38.431: I/MyTextView(16115): <--true:dispatchTouchEvent(DOWN)
06-23 16:50:38.431: I/MyLinearLayout(16115): <--true:dispatchTouchEvent(DOWN)
06-23 16:50:38.431: I/MyActivity(16115): <--true:dispatchTouchEvent(DOWN)
06-23 16:50:38.461: I/MyActivity(16115): -->dispatchTouchEvent(MOVE)
06-23 16:50:38.461: I/MyLinearLayout(16115): -->dispatchTouchEvent(MOVE)
06-23 16:50:38.461: I/MyLinearLayout(16115): -->onInterceptTouchEvent(MOVE)
06-23 16:50:38.461: I/MyLinearLayout(16115): <--true:onInterceptTouchEvent(MOVE)
06-23 16:50:38.461: I/MyTextView(16115): -->dispatchTouchEvent(CANCEL)
06-23 16:50:38.461: I/MyTextView(16115): -->onTouchEvent(CANCEL)
06-23 16:50:38.461: I/MyTextView(16115): <--false:onTouchEvent(CANCEL)
06-23 16:50:38.461: I/MyTextView(16115): <--false:dispatchTouchEvent(CANCEL)
06-23 16:50:38.471: I/MyLinearLayout(16115): <--false:dispatchTouchEvent(MOVE)
06-23 16:50:38.471: I/MyActivity(16115): -->onTouchEvent(MOVE)
06-23 16:50:38.471: I/MyActivity(16115): <--false:onTouchEvent(MOVE)
06-23 16:50:38.511: I/MyActivity(16115): -->dispatchTouchEvent(MOVE)
06-23 16:50:38.511: I/MyLinearLayout(16115): -->dispatchTouchEvent(MOVE)
06-23 16:50:38.511: I/MyLinearLayout(16115): -->onTouchEvent(MOVE)
06-23 16:50:38.511: I/MyLinearLayout(16115): <--false:onTouchEvent(MOVE)
06-23 16:50:38.511: I/MyLinearLayout(16115): <--false:dispatchTouchEvent(MOVE)
06-23 16:50:38.511: I/MyActivity(16115): -->onTouchEvent(MOVE)
06-23 16:50:38.521: I/MyActivity(16115): <--false:onTouchEvent(MOVE)
06-23 16:50:38.521: I/MyActivity(16115): <--false:dispatchTouchEvent(MOVE)
06-23 16:50:38.521: I/MyActivity(16115): -->dispatchTouchEvent(UP)
06-23 16:50:38.521: I/MyLinearLayout(16115): -->dispatchTouchEvent(UP)
06-23 16:50:38.521: I/MyLinearLayout(16115): -->onTouchEvent(UP)
06-23 16:50:38.521: I/MyLinearLayout(16115): <--false:onTouchEvent(UP)
06-23 16:50:38.521: I/MyLinearLayout(16115): <--false:dispatchTouchEvent(UP)
06-23 16:50:38.521: I/MyActivity(16115): -->onTouchEvent(UP)
06-23 16:50:38.521: I/MyActivity(16115): <--false:onTouchEvent(UP)
06-23 16:50:38.521: I/MyActivity(16115): <--false:dispatchTouchEvent(UP)
-- down事件由MyTextView得到并消费,之后拦截MOVE事件时MyTextView得到一次CANCEL事件,后续事件由MyLinearLayout、MyActivity得到
-- 注意:MyLinearLayout拦截第一次MOVE之后,onInterceptTouchEvent方法未再被调用
8、MyTextView消费DOWN事件、MyLinearLayout拦截UP事件
-- down事件由MyTextView得到并消费,move事件被MyTextView、MyActivity得到,之后拦截UP时MyTextView得到一次CANCEL事件,MyActivity得到UP事件
9、MyTextView消费DOWN事件、MyLinearLayout拦截且消费MOVE事件
-- down事件由MyTextView得到并消费,之后拦截MOVE事件时MyTextView得到一次CANCEL事件,后续MOVE事件由MyLinearLayout得到,UP事件由MyLinearLayout、MyActivity得到
10、MyTextView指定Touch监听器,且消费DOWN事件
-- 同6一致
11、MyLinearLayout指定Touch监听器,拦截并消费DOWN事件
-- 同5一致
12、MyTextView指定click监听器
06-23 17:18:14.621: I/MyActivity(19859): -->dispatchTouchEvent(DOWN)
06-23 17:18:14.621: I/MyLinearLayout(19859): -->dispatchTouchEvent(DOWN)
06-23 17:18:14.621: I/MyLinearLayout(19859): -->onInterceptTouchEvent(DOWN)
06-23 17:18:14.621: I/MyLinearLayout(19859): <--false:onInterceptTouchEvent(DOWN)
06-23 17:18:14.621: I/MyTextView(19859): -->dispatchTouchEvent(DOWN)
06-23 17:18:14.621: I/MyTextView(19859): -->onTouchEvent(DOWN)
06-23 17:18:14.621: I/MyTextView(19859): <--true:onTouchEvent(DOWN)
06-23 17:18:14.621: I/MyTextView(19859): <--true:dispatchTouchEvent(DOWN)
06-23 17:18:14.621: I/MyLinearLayout(19859): <--true:dispatchTouchEvent(DOWN)
06-23 17:18:14.621: I/MyActivity(19859): <--true:dispatchTouchEvent(DOWN)
06-23 17:18:14.661: I/MyActivity(19859): -->dispatchTouchEvent(MOVE)
06-23 17:18:14.661: I/MyLinearLayout(19859): -->dispatchTouchEvent(MOVE)
06-23 17:18:14.671: I/MyLinearLayout(19859): -->onInterceptTouchEvent(MOVE)
06-23 17:18:14.671: I/MyLinearLayout(19859): <--false:onInterceptTouchEvent(MOVE)
06-23 17:18:14.671: I/MyTextView(19859): -->dispatchTouchEvent(MOVE)
06-23 17:18:14.671: I/MyTextView(19859): -->onTouchEvent(MOVE)
06-23 17:18:14.671: I/MyTextView(19859): <--true:onTouchEvent(MOVE)
06-23 17:18:14.671: I/MyTextView(19859): <--true:dispatchTouchEvent(MOVE)
06-23 17:18:14.671: I/MyLinearLayout(19859): <--true:dispatchTouchEvent(MOVE)
06-23 17:18:14.671: I/MyActivity(19859): <--true:dispatchTouchEvent(MOVE)
06-23 17:18:14.701: I/MyActivity(19859): -->dispatchTouchEvent(UP)
06-23 17:18:14.701: I/MyLinearLayout(19859): -->dispatchTouchEvent(UP)
06-23 17:18:14.701: I/MyLinearLayout(19859): -->onInterceptTouchEvent(UP)
06-23 17:18:14.701: I/MyLinearLayout(19859): <--false:onInterceptTouchEvent(UP)
06-23 17:18:14.701: I/MyTextView(19859): -->dispatchTouchEvent(UP)
06-23 17:18:14.701: I/MyTextView(19859): -->onTouchEvent(UP)
06-23 17:18:14.701: I/MyTextView(19859): <--true:onTouchEvent(UP)
06-23 17:18:14.701: I/MyTextView(19859): <--true:dispatchTouchEvent(UP)
06-23 17:18:14.701: I/MyLinearLayout(19859): <--true:dispatchTouchEvent(UP)
06-23 17:18:14.701: I/MyActivity(19859): <--true:dispatchTouchEvent(UP)
06-23 17:18:14.711: I/MyTextView(19859): --onClick()
-- 所有事件也由MyTextView得到并消费
13、MyLinearLayout、MyTextView同时指定click监听器
-- 同上
14、MyLinearLayout、MyTextView同时指定click监听器,MyLinearLayout指定属性android:descendantFocusability="blocksDescendants"
-- 同上
15、MyLinearLayout、MyTextView同时指定click监听器,MyLinearLayout拦截DOWN事件
06-23 17:29:54.591: I/MyActivity(22144): -->dispatchTouchEvent(DOWN)
06-23 17:29:54.591: I/MyLinearLayout(22144): -->dispatchTouchEvent(DOWN)
06-23 17:29:54.591: I/MyLinearLayout(22144): -->onInterceptTouchEvent(DOWN)
06-23 17:29:54.591: I/MyLinearLayout(22144): <--true:onInterceptTouchEvent(DOWN)
06-23 17:29:54.601: I/MyLinearLayout(22144): -->onTouchEvent(DOWN)
06-23 17:29:54.601: I/MyLinearLayout(22144): <--true:onTouchEvent(DOWN)
06-23 17:29:54.601: I/MyLinearLayout(22144): <--true:dispatchTouchEvent(DOWN)
06-23 17:29:54.601: I/MyActivity(22144): <--true:dispatchTouchEvent(DOWN)
06-23 17:29:54.631: I/MyActivity(22144): -->dispatchTouchEvent(MOVE)
06-23 17:29:54.631: I/MyLinearLayout(22144): -->dispatchTouchEvent(MOVE)
06-23 17:29:54.631: I/MyLinearLayout(22144): -->onTouchEvent(MOVE)
06-23 17:29:54.631: I/MyLinearLayout(22144): <--true:onTouchEvent(MOVE)
06-23 17:29:54.631: I/MyLinearLayout(22144): <--true:dispatchTouchEvent(MOVE)
06-23 17:29:54.631: I/MyActivity(22144): <--true:dispatchTouchEvent(MOVE)
06-23 17:29:54.681: I/MyActivity(22144): -->dispatchTouchEvent(UP)
06-23 17:29:54.681: I/MyLinearLayout(22144): -->dispatchTouchEvent(UP)
06-23 17:29:54.691: I/MyLinearLayout(22144): -->onTouchEvent(UP)
06-23 17:29:54.691: I/MyLinearLayout(22144): <--true:onTouchEvent(UP)
06-23 17:29:54.691: I/MyLinearLayout(22144): <--true:dispatchTouchEvent(UP)
06-23 17:29:54.691: I/MyActivity(22144): <--true:dispatchTouchEvent(UP)
06-23 17:29:54.691: I/MyLinearLayout(22144): --onClick()
-- 所有事件由MyLinearLayout得到并消费
总结:
1、dispatchTouchEvent总是首先执行,直接return之后返回到父类,其中当前控件和子控件不会得到任何事件
2、View和ViewGroup(通过拦截)得到消费down事件后,后续事件也会被该控件得到
3、onTouchEvent的返回值决定事件是否会被上级、上上级得到(均为false时,事件由底层View和Activity得到)
4、ViewGroup拦截事件之后,该类onInterceptTouch不会再被调用,而其中拦截MOVE或UP,子类会得到一个CANCEL事件
5、TouchListener同onTouchEvent的功能一致
6、ClickListener会消费所有的事件,前提是该控件得到或拦截到down事件
7、即便在同一个控件内部,事件优先被TouchListener得到,其次是onTouchEvent,最后是ClickListener得到
名词:
分发 dispatch 分发是指dispatchTouchEvent返回true
拦截 intercept 拦截是指onInterceptTouchEvent返回true
消费 touch 消费是指onTouchEvent、onTouch返回true
参考:
http://hunankeda110.iteye.com/blog/1944311
Android触摸事件分发机制
标签:
原文地址:http://my.oschina.net/lanfog/blog/469850