关于dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent的分发机制浅析_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 关于dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent的分发机制浅析

关于dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent的分发机制浅析

 2014/6/10 18:55:36  小草房  程序员俱乐部  我要评论(0)
  • 摘要:虽说这个问题不是很难...动动手就能看出答案...但是似乎不太容易理解...几次尝试把这个问题说明白....但是好像感觉说不明白....(顿时想起了那句话----说不明白就是自己还不明白!我怎么可能不明白..so)这里面牵扯到Activity的dispatchTouchEvent,onTouchEvent以及ViewGroup的dispatchTouchevent,onInterceptTouchEvent,onTouchEvent还有View的dispatchTouchEvent
  • 标签:touch

 

虽说这个问题不是很难...动动手就能看出答案...但是似乎不太容易理解...几次尝试把这个问题说明白....但是好像感觉说不明白....(顿时想起了那句话----说不明白就是自己还不明白! 我怎么可能不明白..so)

这里面牵扯到Activity的dispatchTouchEvent, onTouchEvent以及ViewGroup的dispatchTouchevent, onInterceptTouchEvent, onTouchEvent还有View的dispatchTouchEvent, onTouchEvent

(别嫌我..多一嘴View没有onInterceptTouchEvent).

前沿: 知识储备...默认只有View的onTouchEvent返回true其它都返回false;  true意味着本View需要处理(ViewGroup也是View), false则代表不处理继续分发.

0. 这个事件的执行顺序是activity的dispatchTouchEvent先接收到down事件..然后由父往子执行....如果期间没有视图的onTouchEvent返回true的, 那不好意思..只能由activity的onTouchEvent执行了(自个走完down, move, up)

06-10 17:08:29.535: V/Activity(3174): dispatchTouchEvent DOWN
06-10 17:08:29.535: V/ViewGroup1(3174): onInterceptTouchEvent DOWN
06-10 17:08:29.535: V/ViewGroup2(3174): onInterceptTouchEvent DOWN
06-10 17:08:29.535: V/Activity(3174): onTouch DOWN
06-10 17:08:29.535: V/Child(3174): onTouchEvent DOWN
06-10 17:08:29.535: V/ViewGroup2(3174): onTouchEvent DOWN
06-10 17:08:29.535: V/ViewGroup1(3174): onTouchEvent DOWN
06-10 17:08:29.535: V/Activity(3174): onTouchEvent DOWN
06-10 17:08:29.565: V/Activity(3174): dispatchTouchEvent MOVE
06-10 17:08:29.565: V/Activity(3174): onTouchEvent MOVE
06-10 17:08:29.575: V/Activity(3174): dispatchTouchEvent MOVE
06-10 17:08:29.575: V/Activity(3174): onTouchEvent MOVE
06-10 17:08:29.575: V/Activity(3174): dispatchTouchEvent UP
06-10 17:08:29.575: V/Activity(3174): onTouchEvent UP

这么多...咱就先抛开所有的dispatchTouchEvent先来分析onInterceptTouchEvent和onTouchEvent

1. 如果有哪个View的onInterceptTouchEvent返回true, 并且该View的onTouchEvent也返回true了....

本View的onInterceptTouchEvent走完down之后就跟它没关系了.

下面是ViewGroup2的情况.

06-10 16:36:24.881: V/Activity(30631): dispatchTouchEvent DOWN
06-10 16:36:24.881: V/ViewGroup1(30631): onInterceptTouchEvent DOWN
06-10 16:36:24.881: V/ViewGroup2(30631): onInterceptTouchEvent DOWN
06-10 16:36:24.881: V/ViewGroup2(30631): onTouchEvent DOWN
06-10 16:36:24.911: V/Activity(30631): dispatchTouchEvent MOVE
06-10 16:36:24.911: V/ViewGroup1(30631): onInterceptTouchEvent MOVE
06-10 16:36:24.911: V/ViewGroup2(30631): onTouchEvent MOVE
06-10 16:36:24.921: V/Activity(30631): dispatchTouchEvent MOVE
06-10 16:36:24.921: V/ViewGroup1(30631): onInterceptTouchEvent MOVE
06-10 16:36:24.921: V/ViewGroup2(30631): onTouchEvent MOVE
06-10 16:36:24.951: V/Activity(30631): dispatchTouchEvent UP
06-10 16:36:24.951: V/ViewGroup1(30631): onInterceptTouchEvent UP
06-10 16:36:24.951: V/ViewGroup2(30631): onTouchEvent UP

2. 如果有哪个onInterceptTouchEvent返回true了, 但是onTouchEvent返回false了.遵循第0规律

06-10 16:43:50.496: V/Activity(32620): dispatchTouchEvent DOWN
06-10 16:43:50.496: V/ViewGroup1(32620): onInterceptTouchEvent DOWN
06-10 16:43:50.496: V/ViewGroup2(32620): onInterceptTouchEvent DOWN
06-10 16:43:50.496: V/ViewGroup2(32620): onTouchEvent DOWN
06-10 16:43:50.496: V/ViewGroup1(32620): onTouchEvent DOWN
06-10 16:43:50.496: V/Activity(32620): onTouchEvent DOWN
06-10 16:43:50.556: V/Activity(32620): dispatchTouchEvent MOVE
06-10 16:43:50.556: V/Activity(32620): onTouchEvent MOVE
06-10 16:43:50.576: V/Activity(32620): dispatchTouchEvent MOVE
06-10 16:43:50.576: V/Activity(32620): onTouchEvent MOVE
06-10 16:43:50.586: V/Activity(32620): dispatchTouchEvent UP
06-10 16:43:50.586: V/Activity(32620): onTouchEvent UP

3. 还有第三种吗...第三种就是默认情况了....只走Child的onTouchEvent, 那就遵循第1条规律

06-10 16:49:04.191: V/Activity(746): dispatchTouchEvent DOWN
06-10 16:49:04.191: V/ViewGroup1(746): onInterceptTouchEvent DOWN
06-10 16:49:04.191: V/ViewGroup2(746): onInterceptTouchEvent DOWN
06-10 16:49:04.191: V/Activity(746): onTouch DOWN
06-10 16:49:04.191: V/Child(746): onTouchEvent DOWN
06-10 16:49:04.261: V/Activity(746): dispatchTouchEvent MOVE
06-10 16:49:04.261: V/ViewGroup1(746): onInterceptTouchEvent MOVE
06-10 16:49:04.261: V/ViewGroup2(746): onInterceptTouchEvent MOVE
06-10 16:49:04.261: V/Activity(746): onTouch MOVE
06-10 16:49:04.271: V/Child(746): onTouchEvent MOVE
06-10 16:49:04.271: V/Activity(746): dispatchTouchEvent UP
06-10 16:49:04.271: V/ViewGroup1(746): onInterceptTouchEvent UP
06-10 16:49:04.271: V/ViewGroup2(746): onInterceptTouchEvent UP
06-10 16:49:04.271: V/Activity(746): onTouch UP
06-10 16:49:04.271: V/Child(746): onTouchEvent UP

最后加上这个dispatchTouchEvent,这个View要是走的话...先走dispatchTouchEvent, 如果返回true的话,那就跟本View的onInterceptTouchEvent和onTouchEvent没关系了.自己处理了, 不分发了.

下面我把ViewGroup2的dispatchTouchEvent返回true(ViewGroup1的dispatchTouchEvent注释了.)

06-10 16:52:52.284: V/Activity(1403): dispatchTouchEvent DOWN
06-10 16:52:52.284: V/ViewGroup1(1403): onInterceptTouchEvent DOWN
06-10 16:52:52.284: V/ViewGroup2(1403): dispatchTouchEvent DOWN
06-10 16:52:52.324: V/Activity(1403): dispatchTouchEvent MOVE
06-10 16:52:52.324: V/ViewGroup1(1403): onInterceptTouchEvent MOVE
06-10 16:52:52.334: V/ViewGroup2(1403): dispatchTouchEvent MOVE
06-10 16:52:52.345: V/Activity(1403): dispatchTouchEvent MOVE
06-10 16:52:52.345: V/ViewGroup1(1403): onInterceptTouchEvent MOVE
06-10 16:52:52.345: V/ViewGroup2(1403): dispatchTouchEvent MOVE
06-10 16:52:52.365: V/Activity(1403): dispatchTouchEvent MOVE
06-10 16:52:52.365: V/ViewGroup1(1403): onInterceptTouchEvent MOVE
06-10 16:52:52.365: V/ViewGroup2(1403): dispatchTouchEvent MOVE
06-10 16:52:52.375: V/Activity(1403): dispatchTouchEvent UP
06-10 16:52:52.375: V/ViewGroup1(1403): onInterceptTouchEvent UP
06-10 16:52:52.375: V/ViewGroup2(1403): dispatchTouchEvent UP

终于写完了...发现自己越来越懂了...要是没明白的话...建议自己写一下...

发表评论
用户名: 匿名