`
- 浏览:
703173 次
- 性别:
- 来自:
北京
-
onInterceptTouchEvent和onTouchEvent调用时序
onInterceptTouchEvent()是ViewGroup的一个方法,目的是在系统向该ViewGroup及其各个childView触发onTouchEvent()之前对相关事件进行一次拦截,Android这么设计的想法也很好理解,由于ViewGroup会包含若干childView,因此需要能够统一监控各种touch事件的机会,因此纯粹的不能包含子view的控件是没有这个方法的,如LinearLayout就有,TextView就没有。
onInterceptTouchEvent()使用也很简单,如果在ViewGroup里覆写了该方法,那么就可以对各种touch事件加以拦截。但是如何拦截,是否所有的touch事件都需要拦截则是比较复杂的,touch事件在onInterceptTouchEvent()和onTouchEvent以及各个childView间的传递机制完全取决于onInterceptTouchEvent()和onTouchEvent()的返回值。并且,针对down事件处理的返回值直接影响到后续move和up事件的接收和传递。
关于返回值的问题,基本规则很清楚,如果return true,那么表示该方法消费了此次事件,如果return false,那么表示该方法并未处理完全,该事件仍然需要以某种方式传递下去继续等待处理。
SDK给出的说明如下:
· You will receive the down event here.
· The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
· For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
· If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
由于onInterceptTouchEvent()的机制比较复杂,上面的说明写的也比较复杂,总结一下,基本的规则是:
1.down事件首先会传递到onInterceptTouchEvent()方法
2.如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return false,那么后续的move, up等事件将继续会先传递给该ViewGroup,之后才和down事件一样传递给最终的目标view的onTouchEvent()处理。
3.如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return true,那么后续的move, up等事件将不再传递给onInterceptTouchEvent(),而是和down事件一样传递给该ViewGroup的onTouchEvent()处理,注意,目标view将接收不到任何事件。
4.如果最终需要处理事件的view的onTouchEvent()返回了false,那么该事件将被传递至其上一层次的view的onTouchEvent()处理。
5.如果最终需要处理事件的view 的onTouchEvent()返回了true,那么后续事件将可以继续传递给该view的onTouchEvent()处理。
原文链接:http://blog.csdn.net/ddna/article/details/5473293
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
NULL 博文链接:https://2528.iteye.com/blog/1056731
onInterceptTouchEvent和onTouchEvent调用关系详解
关于onInterceptTouchEvent和onTouchEvent使用demo,很不错的一个demo,可以帮助Android的触摸事件。
onInterceptTouchEvent与OntouchEvent之间的事件传递机制的具体内涵关系...
onInterceptTouchEvent和onTouchEvent调用时序
NULL 博文链接:https://username2.iteye.com/blog/2193153
onInterceptTouchEvent和onTouchEvent调用时序详解 测试demo 详细介绍请移步:http://blog.csdn.net/yiranxinshou/article/details/9201833
NULL 博文链接:https://yxwww.iteye.com/blog/1409461
onInterceptTouchEvent 和 onTouchEvent 研究
通过代码实践总结dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent的调用流程、顺序、及返回值对事件处理的影响。
基于这个猜测,我自定义一个ViewPager,重写dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent,分别在这三个方法中打印log; 重写SlidingMenu的dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent,...
onInterceptTouchEvent与onTouchEvent事件传递的分析
onInterceptTouchEvent ontouchevent 的一个执行顺序和关于返回值的小demo 希望有点用
本篇文章小编为大家介绍,Android onTouchEvent 与 onInterceptTouchEvent的区别详解。需要的朋友参考下
侧滑面板(对ViewGroup的自定义) * 应用场景: 扩展主面板的功能 ... 触摸优化: 重写ViewGroup里onInterceptTouchEvent和onTouchEvent Github 大牛 Jake Wharton nineoldandroids.jar 属性动画 ActionBarSherlock
(之前上传的代码有bug,经过作者整理和修改,fix掉部分bug) ...关键技术是理解清楚“onInterceptTouchEvent和onTouchEvent”的关系,以及computeScroll和dispatchDraw等。 可以在百度中搜索关键词获取答案。
Title : dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent三者对MotionEvent的分发、拦截机制分析 Author: yaong Category:自己做的Demo Tips : 三个自定义View,依次嵌套,并各自覆写拦截、监听方法,通过...
android 事件传递在很多地方被描述的很复杂,难懂,其实只要自己通过代码跟一样程序的执行过程,看一下debug日志输出,就很容易理解了,在本代码里通过更改onInterceptTouchEvent和onTouchEvent的返回值,观查debug...
在自定义ViewGroup中,很多效果都包含用户手指去拖动其内部的某个View(eg:侧滑菜单等),针对具体的需要去写好onInterceptTouchEvent和onTouchEvent这两个方法是一件很不容易的事,需要自己去处理:多手指的处理、加...
安卓事件分发机制测试代码,事件传递从Activity-->ViewGroup-->View。dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent这三个函数的返回值不同,代表的事件传递的不同。