1、Touch事件传递顺序
a、当onTouchEvent均返回false的时候,即事件没有被消费掉的时候
Activity ---> ViewGroup ---> View ---> ViewGroup ---> Activity
b、单点单次触摸事件产生一次ACTION_DOWN事件,一次或多次 ACTION_MOVE事件, 和一次的ACTION_UP事件;
对于传递至某个View的onTouch事件,若onTouchEvent 返回true,则会接收到一系列完整的Touch事件序列;若返回false,则只会接收到 ACTION_DOWN事件;
2、dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent
a、默认调用顺序
dispatchTouchEvent ---> (onInterceptTouchEvent) ---> onTouchEvent --->dispatchTouchEvent
1、dispatchTouchEvent,一般无需重写,不作处理;如重写记得调用父类的dispatchTouchEvent方法,否则事件无法传递
2、onInterceptTouchEvent ,一般在ViewGroup层重写用于是否拦截事件的传递;返回true时,代表拦截事件,事件不再向下传递,执行到本层的onTouchEvent返回;
默认返回false;一次触摸事件里,该方法只会被调用一次;
3、onTouchEvent,默认返回false,对于每一次的onTouch事件,只会接收到ACTION_DOWN事件;
返回true时,会接收到ACTION_DOWN(一次) --> ACTION_MOVE(一系列) --> ACTION_UP(一次)事件;
3、例子:
不作事件处理:
05-27 15:02:45.569: I/System.out(13058): Acticity ---> dispatchTouchEvent
05-27 15:02:45.569: I/System.out(13058): MotionEvent action --> 0
05-27 15:02:45.569: I/System.out(13058): @@@CustomLayout ---> dispatchTouchEvent
05-27 15:02:45.569: I/System.out(13058): @@@CustomLayout ---> onInterceptTouchEvent
05-27 15:02:45.569: I/System.out(13058): ###CustomView ---> dispatchTouchEvent
05-27 15:02:45.569: I/System.out(13058): ###CustomView ---> onInterceptTouchEvent
05-27 15:02:45.569: I/System.out(13058): ###CustomView ---> onInterceptTouchEvent - false
05-27 15:02:45.569: I/System.out(13058): ###CustomView ---> onTouchEvent
05-27 15:02:45.569: I/System.out(13058): ###CustomView ---> dispatchTouchEvent - false
05-27 15:02:45.569: I/System.out(13058): @@@CustomLayout ---> onTouchEvent
05-27 15:02:45.569: I/System.out(13058): @@@CustomLayout ---> dispatchTouchEvent - false
05-27 15:02:45.569: I/System.out(13058): Acticity ---> onTouchEvent
05-27 15:02:45.569: I/System.out(13058): Acticity ---> onTouchEvent - false
05-27 15:02:45.569: I/System.out(13058): Acticity ---> dispatchTouchEvent - false
05-27 15:02:45.579: I/System.out(13058): Acticity ---> dispatchTouchEvent
05-27 15:02:45.579: I/System.out(13058): MotionEvent action --> 2
05-27 15:02:45.579: I/System.out(13058): Acticity ---> onTouchEvent
05-27 15:02:45.579: I/System.out(13058): Acticity ---> onTouchEvent - false
05-27 15:02:45.579: I/System.out(13058): Acticity ---> dispatchTouchEvent - false
05-27 15:02:45.599: I/System.out(13058): Acticity ---> dispatchTouchEvent
05-27 15:02:45.599: I/System.out(13058): MotionEvent action --> 2
05-27 15:02:45.599: I/System.out(13058): Acticity ---> onTouchEvent
05-27 15:02:45.599: I/System.out(13058): Acticity ---> onTouchEvent - false
05-27 15:02:45.599: I/System.out(13058): Acticity ---> dispatchTouchEvent - false
05-27 15:02:45.599: I/System.out(13058): Acticity ---> dispatchTouchEvent
05-27 15:02:45.599: I/System.out(13058): MotionEvent action --> 1
05-27 15:02:45.599: I/System.out(13058): Acticity ---> onTouchEvent
05-27 15:02:45.599: I/System.out(13058): Acticity ---> onTouchEvent - false
05-27 15:02:45.599: I/System.out(13058): Acticity ---> dispatchTouchEvent - false
事件拦截:
05-27 14:58:26.166: I/System.out(12976): Acticity ---> dispatchTouchEvent
05-27 14:58:26.166: I/System.out(12976): MotionEvent action --> 0
05-27 14:58:26.166: I/System.out(12976): @@@CustomLayout ---> dispatchTouchEvent
05-27 14:58:26.166: I/System.out(12976): @@@CustomLayout ---> onInterceptTouchEvent
05-27 14:58:26.166: I/System.out(12976): @@@CustomLayout ---> onTouchEvent
05-27 14:58:26.166: I/System.out(12976): @@@CustomLayout ---> dispatchTouchEvent - true
05-27 14:58:26.166: I/System.out(12976): Acticity ---> dispatchTouchEvent - true
05-27 14:58:26.186: I/System.out(12976): Acticity ---> dispatchTouchEvent
05-27 14:58:26.186: I/System.out(12976): MotionEvent action --> 2
05-27 14:58:26.186: I/System.out(12976): @@@CustomLayout ---> dispatchTouchEvent
05-27 14:58:26.186: I/System.out(12976): @@@CustomLayout ---> onTouchEvent
05-27 14:58:26.186: I/System.out(12976): @@@CustomLayout ---> dispatchTouchEvent - true
05-27 14:58:26.186: I/System.out(12976): Acticity ---> dispatchTouchEvent - true
05-27 14:58:26.206: I/System.out(12976): Acticity ---> dispatchTouchEvent
05-27 14:58:26.216: I/System.out(12976): MotionEvent action --> 1
05-27 14:58:26.216: I/System.out(12976): @@@CustomLayout ---> dispatchTouchEvent
05-27 14:58:26.216: I/System.out(12976): @@@CustomLayout ---> onTouchEvent
05-27 14:58:26.216: I/System.out(12976): @@@CustomLayout ---> dispatchTouchEvent - true
05-27 14:58:26.216: I/System.out(12976): Acticity ---> dispatchTouchEvent - true