ViewGroup的onInterceptTouchEvent 顾名思义,作用是拦截TouchEvent。
假如有以下布局
在一组TouchEvent中可能出现:
没有View或ViewGroup消费ACTION_DOWN事件,则TouchEvent的传递路径会是:
ACTION_DOWN : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onInterceptTouchEvent —> ChildView.onTouchEvent —> ViewGroup2.onTouchEvent —> ViewGroup1.onTouchEvent
由于谁都没有消费ACTION_DOWN事件,后续的MOVE,UP事件将不会传进来。
ViewGroup2拦截且消费了ACTION_DOWN事件,则TouchEvent的传递路径会是:
ACTION_DOWN : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onInterceptTouchEvent —> ViewGroup2.onTouchEvent ;
ACTION_MOVE : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onTouchEvent ;
ACTION_MOVE : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onTouchEvent ;
也即后续的TouchEvent都传递给了ViewGroup2。
ViewGroup2拦截了ACTION_DOWN事件但不消费,而ViewGroup1消费了该TouchEvent,则TouchEvent的传递路径会是:
ACTION_DOWN : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onInterceptTouchEvent —> ViewGroup2.onTouchEvent —> ViewGroup1.onTouchEvent ;
ACTION_MOVE : ViewGroup1.onTouchEvent ;
ACTION_MOVE : ViewGroup1.onTouchEvent ;
也即后续的TouchEvent都传递给了ViewGroup1。
ChildView先消费了ACTION_DOWN事件,然后满足一定条件时ViewGroup2拦截了TouchEvent,此时无论ViewGroup2是否消费该TouchEvent,TouchEvent的传递路径是:
ACTION_DOWN : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onInterceptTouchEvent —> ChildView.onTouchEvent ;
ACTION_MOVE : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onInterceptTouchEvent —> ChildView.onTouchEvent ;
ACTION_MOVE : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onInterceptTouchEvent && ACTION_CANCEL : ChildView.onTouchEvent ; (满足条件触发
ViewGroup2拦截,会传递ACTION_CANCEL给ChildView)
ACTION_MOVE : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onTouchEvent ;
ACTION_MOVE : ViewGroup1.onInterceptTouchEvent —> ViewGroup2.onTouchEvent ;
也即后续的TouchEvent都传递给了ViewGroup2的onTouchEvent,且无论ViewGroup2是否消费该TouchEvent 。