上一节简单的介绍了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.youkuaiyun.com/lvxiangan/article/details/9309927
http://blog.youkuaiyun.com/lmj623565791/article/details/38960443