事件分发学习总结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

跟上面对比发现:

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

上一篇 下一篇

猜你喜欢

热点阅读