Android事件传递顺序

2018-07-13  本文已影响16人  Max____

事件传递顺序

我们假设在一个activity中有一个ViewGroup,ViewGroup中有一个TextView。</br>

image.png

我们设置不同层级的不同事件拦截方法的返回值,每此触控TextView区域来观察事件的传递情况。监听方法有:

一共7个方法,如果下面展示的打印日志没有出现,说明没有调用

默认事件传递顺序


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ ViewGroup onInterceptTouchEvent: down 

     ↓ TextView dispatchTouchEvent down 

     ↓ TextView onTouchEvent down 

     ↓ ViewGroup onTouchEvent: down 

     ↓ Activity onTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ Activity onTouchEvent move 

     ↓ Activity dispatchTouchEvent up 

     ↓ Activity onTouchEvent up

Activity的dispatchTouchEvent中返回true


     ↓ Activity dispatchTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ Activity dispatchTouchEvent up

Activity的dispatchTouchEvent中返回false


     ↓ Activity dispatchTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ Activity dispatchTouchEvent up

ViewGroup dispatchTouchEvent中返回true


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ Activity dispatchTouchEvent move 

     ↓ ViewGroup dispatchTouchEvent: move 

     ↓ Activity dispatchTouchEvent up 

     ↓ ViewGroup dispatchTouchEvent: up

ViewGroup dispatchTouchEvent中返回false


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ Activity onTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ Activity onTouchEvent move 

     ↓ Activity dispatchTouchEvent up 

     ↓ Activity onTouchEvent up

ViewGroup onInterceptTouchEvent中返回true


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ ViewGroup onInterceptTouchEvent: down 

     ↓ ViewGroup onTouchEvent: down 

     ↓ Activity onTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ Activity onTouchEvent move 

     ↓ Activity dispatchTouchEvent up 

     ↓ Activity onTouchEvent up

ViewGroup onInterceptTouchEvent中返回true 并且 onTouchEvent返回true


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ ViewGroup onInterceptTouchEvent: down 

     ↓ ViewGroup onTouchEvent: down 

     ↓ Activity dispatchTouchEvent move 

     ↓ ViewGroup dispatchTouchEvent: move 

     ↓ ViewGroup onTouchEvent: move 

     ↓ Activity dispatchTouchEvent up 

     ↓ ViewGroup dispatchTouchEvent: up 

     ↓ ViewGroup onTouchEvent: up

ViewGroup onInterceptTouchEvent中返回false


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ ViewGroup onInterceptTouchEvent: down 

     ↓ TextView dispatchTouchEvent down 

     ↓ TextView onTouchEvent down 

     ↓ ViewGroup onTouchEvent: down 

     ↓ Activity onTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ Activity onTouchEvent move 

     ↓ Activity dispatchTouchEvent up 

     ↓ Activity onTouchEvent up

TextView dispatchTouchEvent返回true


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ ViewGroup onInterceptTouchEvent: down 

     ↓ TextView dispatchTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ ViewGroup dispatchTouchEvent: move 

     ↓ ViewGroup onInterceptTouchEvent: move 

     ↓ TextView dispatchTouchEvent move 

     ↓ Activity dispatchTouchEvent up 

     ↓ ViewGroup dispatchTouchEvent: up 

     ↓ ViewGroup onInterceptTouchEvent: up 

     ↓ TextView dispatchTouchEvent up

TextView dispatchTouchEvent返回false


     ↓ Activity dispatchTouchEvent down 

     ↓ ViewGroup dispatchTouchEvent: down 

     ↓ ViewGroup onInterceptTouchEvent: down 

     ↓ TextView dispatchTouchEvent down 

     ↓ ViewGroup onTouchEvent: down 

     ↓ Activity onTouchEvent down 

     ↓ Activity dispatchTouchEvent move 

     ↓ Activity onTouchEvent move 

     ↓ Activity dispatchTouchEvent up 

     ↓ Activity onTouchEvent up

总结

image.png
上一篇下一篇

猜你喜欢

热点阅读