Android事件分发<二>

上一节简单的介绍了Androd事件分发的流程,这一小节介绍一下之前的那三个方法的作用。
dispatchTouchEvent:
这个方法作用它的命名一样disspathTouchEvent,事件分发。
onInterceptTouchEvent:
Intercept,”拦截”的意思,即事件拦截,当我们操作该方法可以决定是否将事件继续往下传递,当它的返回值为true时,就可以将事件拦截,不再将事件传递给本view的onTouchEvent方法,该方法也不会再被调用。
onTouchEvent:
该view的事件处理方法。返回值为true表明,该view消费掉了该事件。不再往回传,去传递给上一级view的onTouchEvent。

下面我将LinearOutThree的onInterceptTouchEvent的返回值为true:

这里写图片描述

 MainActivity-----dispatchTouchEvent
 LinearoutOne-----dispatchTouchEvent
 LinearoutOne-----onInterceptTouchEvent
 LinearoutTwo-----dispatchTouchEvent
 LinearoutTwo-----onInterceptTouchEvent
 LinearoutThree-----dispatchTouchEvent
 LinearoutThree-----onInterceptTouchEvent
 LinearoutThree-----dispatchTouchEvent
 LinearoutTwo-----onTouchEvent
 LinearoutOne-----onTouchEvent
 MainActivity-----onTouchEvent
 MainActivity-----dispatchTouchEvent
 MainActivity-----onTouchEvent

事件继续往上回传,但是LinearOutThree的onTouchEvent并没有执行。

下面我将LinearOutThree的onTouchEvent的返回值为true:

这里写图片描述

MainActivity-----dispatchTouchEvent
LinearoutOne-----dispatchTouchEvent
LinearoutOne-----onInterceptTouchEvent
LinearoutTwo-----dispatchTouchEvent
LinearoutTwo-----onInterceptTouchEvent
LinearoutThree-----dispatchTouchEvent
LinearoutThree-----onInterceptTouchEvent
LinearoutThree-----dispatchTouchEvent
MainActivity-----dispatchTouchEvent

没有出现LinaroutOutOne和LinearOutTwo的 onTouchEvent方法,表明该事件在这儿被消费了,不再往上级回传。

我们再给LinearOutThree中的TextView设置setOnTouchListener返回值为false的结果:

 MainActivity-----dispatchTouchEvent
 LinearoutOne-----dispatchTouchEvent
 LinearoutOne-----onInterceptTouchEvent
 LinearoutTwo-----dispatchTouchEvent
 LinearoutTwo-----onInterceptTouchEvent
 LinearoutThree-----dispatchTouchEvent
 LinearoutThree-----onInterceptTouchEvent
 textView.setOnTouchListener-----onTouch
 LinearoutThree-----dispatchTouchEvent
 LinearoutTwo-----onTouchEvent
 LinearoutOne-----onTouchEvent
 MainActivity-----onTouchEvent
 MainActivity-----dispatchTouchEvent
 MainActivity-----onTouchEvent

我们再给LinearOutThree中的TextView设置setOnTouchListener返回值为true的结果:

MainActivity-----dispatchTouchEvent
LinearoutOne-----dispatchTouchEvent
LinearoutOne-----onInterceptTouchEvent
LinearoutTwo-----dispatchTouchEvent
LinearoutTwo-----onInterceptTouchEvent
LinearoutThree-----dispatchTouchEvent
LinearoutThree-----onInterceptTouchEvent
textView.setOnTouchListener-----onTouch

不再将事件分发给LinearoutOne,LinearoutTwo,LinearoutThree的onTouchevent。
在时间的开发当中我们使用最多的情况是自定义view出现手指滑动的一些事件的的处理,以及像一些滑动控件的相互套用,之后出现的滑事件冲突的处理都需要。前两天在ScrollView中嵌套viewflow,出现ViewFlow滑动不灵敏;ViewFlow全屏,手指屏幕上下滑动弹出子菜单,等等都遇到滑动事件的冲突。
下面是一些比较好的分析事件分发的博客:
http://blog.csdn.net/lvxiangan/article/details/9309927
http://blog.csdn.net/lmj623565791/article/details/38960443

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值