事件分发学习总结2
2020-06-13 本文已影响0人
吃饱了就送
布局结构
所有事件返回值都为:return super.TouchEvent(event)
E/ActivityToucEvent: dispatchTouchEvent: 0
E/ParentLayout: dispatchTouchEvent: 0
E/ParentLayout: onInterceptTouchEvent: 0
E/NestedParentLayout: dispatchTouchEvent: 0
E/NestedParentLayout: onInterceptTouchEvent: 0
E/ChildView: dispatchTouchEvent: 0
E/ChildView: onTouchEvent: 0
E/NestedParentLayout: onTouchEvent: 0
E/ParentLayout: onTouchEvent: 0
E/ActivityToucEvent: onTouchEvent: 0
E/ActivityToucEvent: dispatchTouchEvent: 1
E/ActivityToucEvent: onTouchEvent: 1
底层View是具有事件的优先消费权的,onTouchEvent最先被调用
ChildView onTouchEvent返回true
E/ActivityToucEvent: dispatchTouchEvent: 0
E/ParentLayout: dispatchTouchEvent: 0
E/ParentLayout: onInterceptTouchEvent: 0
E/NestedParentLayout: dispatchTouchEvent: 0
E/NestedParentLayout: onInterceptTouchEvent: 0
E/ChildView: dispatchTouchEvent: 0
E/ChildView: onTouchEvent: 0
E/ActivityToucEvent: dispatchTouchEvent: 1
E/ParentLayout: dispatchTouchEvent: 1
E/ParentLayout: onInterceptTouchEvent: 1
E/NestedParentLayout: dispatchTouchEvent: 1
E/NestedParentLayout: onInterceptTouchEvent: 1
E/ChildView: dispatchTouchEvent: 1
E/ChildView: onTouchEvent: 1
跟上面对比发现:
- 如果View 没有对 ACTION_DOWN 进行消费,此次点击的后续事件不会传递过来;
- 如果 View 消费了 ACTION_DOWN ,此次点击的后续事件会直接给这个 View,这里的后续事件指的是 ACTION_MOVE 和 ACTION_UP 事件;此时,其父 ViewGroup 的 onIntercept 函数仍会被调用,仍能进行拦截,但它自己的 onIntercept 不会被调用了;
NestedParentLayout onInterceptTouchEvent返回true
E/ActivityToucEvent: dispatchTouchEvent: 0
E/ParentLayout: dispatchTouchEvent: 0
E/ParentLayout: onInterceptTouchEvent: 0
E/NestedParentLayout: dispatchTouchEvent: 0
E/NestedParentLayout: onInterceptTouchEvent: 0
E/NestedParentLayout: onTouchEvent: 0
E/ParentLayout: onTouchEvent: 0
E/ActivityToucEvent: onTouchEvent: 0
E/ActivityToucEvent: dispatchTouchEvent: 1
E/ActivityToucEvent: onTouchEvent: 1
若拦截事件但在onTouchEvent中未消费(返回true),事件还是会通过onTouchEvent 向上传递
NestedParentLayout dispatchTouchEvent返回false
E/ActivityToucEvent: dispatchTouchEvent: 0
E/ParentLayout: dispatchTouchEvent: 0
E/ParentLayout: onInterceptTouchEvent: 0
E/NestedParentLayout: dispatchTouchEvent: 0
E/ParentLayout: onTouchEvent: 0
E/ActivityToucEvent: onTouchEvent: 0
E/ActivityToucEvent: dispatchTouchEvent: 1
E/ActivityToucEvent: onTouchEvent: 1
若dispatchTouchEvent返回false,则本层不再向下传递,将事件传递给上层onTouchEvent