android 事件冒泡,Android事件分发与消费机制

一、Touch 事件分析:

事件分发:dispatchTouchEvent

return true:事件会分发给当前 View 并由 dispatchTouchEvent 方法进行消费,同时事件停止向下传递;

return false:返回给 上一级的onTouchEvent 进行消费;

returnsuper.dispatchTouchEvent(ev):事件会自动的分发给当前 View 的 onInterceptTouchEvent 方法。

事件拦截:onInterceptTouchEvent

(在View 的 dispatchTouchEvent(MotionEvent ev) 返回super.dispatchTouchEvent(ev) 时,事件会自动分发给当前 View 的 onInterceptTouchEvent 方法)

returntrue:将事件进行拦截,并将拦截到的事件交由当前 View 的 onTouchEvent 进行处理;

return false:将事件放行,当前 View 上的事件会被传递到子 View 上,再由子 View 的 dispatchTouchEvent 来开始这个事件的分发;

return super.onInterceptTouchEvent(ev):事件默认会被拦截,并将拦截到的事件交由当前 View 的 onTouchEvent 进行处理。

事件响应:onTouchEvent

(在 dispatchTouchEvent 返回 super.dispatchTouchEvent(ev) 并且 onInterceptTouchEvent 返回 true 或返回 super.onInterceptTouchEvent(ev) 时onTouchEvent 会被调用)

return  false:事件从当前 View 向上传递,并且都是由上层 View 的onTouchEvent 来接收,如果传递到上面的 onTouchEvent 也返回

false,这个事件就会“消失”,而且接收不到下一次事件。

return true:则会接收并消费该事件。

return super.onTouchEvent(ev): 默认处理事件的逻辑和返回 false 时相同。

9ef505641d7a

全部系统默认

1.ViewGroup的dispatchTouchEvent为false:

9ef505641d7a

ViewGroup的dispatchTouchEvent为false

结果分析:

事件先由Activity的dispatchTouchEvent 分发给 ViewGroup的dispatchTouchEvent,而ViewGroup的 dispatchTouchEvent 返回 false,表示对获取到的事件停止向下传递,同时也不对该事件进行消费。由于 ViewGroup获取的事件直接来自 Activity,则会将事件返回给 Activity的 onTouchEvent 进行消费,最后直接由 Activity来响应手指移动和抬起事件。

2.ViewGroup的dispatchTouchEvent为true:

9ef505641d7a

ViewGroup的dispatchTouchEvent为true

结果分析:

事件由Activity的dispatchTouchEvent 分发给 ViewGroup的dispatchTouchEvent,而ViewGroup的 dispatchTouchEvent 返回 true,表示分发事件到ViewGroup并由ViewGroup的dispatchTouchEvent 进行消费。Activity 不断的分发事件到ViewGroup控件的dispatchTouchEvent,而ViewGroup控件的dispatchTouchEvent也不断的将获取到的事件进行消费。

3.ViewGroup的onInterceptTouchEvent为true:

9ef505641d7a

ViewGroup的onInterceptTouchEvent为true

结果分析:

事件由Activity的dispatchTouchEvent方法分发给ViewGroup控件的dispatchTouchEvent,而ViewGroup的 dispatchTouchEvent 返回super.dispatchTouchEvent(ev),表示对事件进行分发并向下传递给 ViewGroup控件的onInterceptTouchEvent 方法,该方法返回 true 表示对所获取到的事件进行拦截并将事件传递给ViewGroup的onTouchEvent 进行处理,ViewGroup的 onTouchEvent返回 super.onTouchEvent(ev) 表示对事件没有做任何处理直接将事件返回给上级,由于 ViewGroup获取的事件直接来自Activity,所以 ViewGroup的 onTouchEvent会将事件以冒泡方式直接返回给 Activity的 onTouchEvent 进行消费,后续的事件则会跳过ViewGroup直接由 Activity的 onTouchEvent 消费来自Activity自身分发的事件。

4.ViewGroup的onInterceptTouchEvent为false:

9ef505641d7a

ViewGroup的onInterceptTouchEvent为false

结果分析:

事件先由Activity 的 dispatchTouchEvent 方法分发给 ViewGroup的 dispatchTouchEvent,而ViewGroup的 dispatchTouchEvent 返回super.dispatchTouchEvent(ev),表示对事件进行分发并向下传递给ViewGroup的 onInterceptTouchEvent 方法,该方法返回 false 表示事件会被放行并传递到View 的dispatchTouchEvent方法,同样View的 dispatchTouchEvent 返回 super.dispatchTouchEvent(ev),认会将事件传递给 View的 onTouchEvent 进行处理,View的 onTouchEvent 返回 super.onTouchEvent(ev)表示对事件没有做任何处理直接将事件返回给上级,由于 View获取的事件直接来自ViewGroup,所以View的 onTouchEvent会将事件以冒泡方式直接返回给 ViewGroup

的 onTouchEvent 进行消费,而 ViewGroup的 onTouchEvent 也返回了super.onTouchEvent(ev),同样 ViewGroup的 onTouchEvent 也会将事件返回给上级,而TouchEventFather 获取的事件直接来自Activity,所以 ViewGroup控件的onTouchEvent 会将事件以冒泡方式直接返回给 Activity 的 onTouchEvent进行消费,后续的事件则会跳过 ViewGroup和 View,直接由Activity 的 onTouchEvent 消费来自Activity 自身分发的事件。

5.ViewGroup的onInterceptTouchEvent为false,View的dispatchTouchEvent为true:

9ef505641d7a

ViewGroup的onInterceptTouchEvent为false,View的dispatchTouchEvent为true

结果分析:

事件由Activity的dispatchTouchEvent 方法分发给ViewGroup的dispatchTouchEvent,ViewGroup的 dispatchTouchEvent 返回super.dispatchTouchEvent(ev),事件会分发到ViewGroup的 onInterceptTouchEvent,onInterceptTouchEvent 返回 false,表示放行当前事件;事件会被传递到View 的 dispatchTouchEvent,dispatchTouchEvent 返回 true 表示事件被分发到 View并由View的dispatchTouchEvent 方法消费。后续的事件也会不断的重复上面的逻辑最终被 View的dispatchTouchEvent 消费。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值