转自:http://www.cnblogs.com/halzhang/archive/2010/08/18/1802611.html
一、onTouch
onTouch是View中OnTouchListener接口中的方法,处理View及其子类被touch是的事件处理。当然,前提是touch时间能够传递到指定的view。Q1:为什么会传递不到呢?
1: /**
2: * Interface definition for a callback to be invoked when a touch event is
3: * dispatched to this view. The callback will be invoked before the touch
4: * event is given to the view.
5: */
6: public interface OnTouchListener {
7: /**
8: * Called when a touch event is dispatched to a view. This allows listeners to
9: * get a chance to respond before the target view.
10: *
11: * @param v The view the touch event has been dispatched to.
12: * @param event The MotionEvent object containing full information about
13: * the event.
14: * @return True if the listener has consumed the event, false otherwise.
15: */
16: boolean onTouch(View v, MotionEvent event);
17: }
二、onTouchEvent
onTouchEvent同样也是在view中定义的一个方法。处理传递到view 的手势事件。手势事件类型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL四种事件。
1: /**
2: * Implement this method to handle touch screen motion events.
3: *
4: * @param event The motion event.
5: * @return True if the event was handled, false otherwise.
6: */
7: public boolean onTouchEvent(MotionEvent event) {
8: ……
9: ……
10: }
一旦onTouchEvent方法被调用,并返回true则这个手势事件就结束了,并不会向下传递到子控件。Q2:onTouchEvent什么时候被调用呢?
三、onInterceptTouchEvent
onInterceptTouchEvent是在ViewGroup里面定义的。Android中的layout布局类一般都是继承此类的。 onInterceptTouchEvent是用于拦截手势事件的,每个手势事件都会先调用onInterceptTouchEvent。
1: public boolean onInterceptTouchEvent(MotionEvent ev) {
2: return false;
3: }
此方法返回false,则手势事件会向子控件传递;返回true,则调用onTouchEvent方法。
分享到:
相关推荐
本篇文章小编为大家介绍,Android onTouchEvent 与 onInterceptTouchEvent的区别详解。需要的朋友参考下
NULL 博文链接:https://2528.iteye.com/blog/1056731
onInterceptTouchEvent与OntouchEvent之间的事件传递机制的具体内涵关系...
onInterceptTouchEvent和onTouchEvent调用关系详解
NULL 博文链接:https://yxwww.iteye.com/blog/1409461
关于onInterceptTouchEvent和onTouchEvent使用demo,很不错的一个demo,可以帮助Android的触摸事件。
onInterceptTouchEvent ontouchevent 的一个执行顺序和关于返回值的小demo 希望有点用
NULL 博文链接:https://username2.iteye.com/blog/2193153
onInterceptTouchEvent和onTouchEvent调用时序详解 测试demo 详细介绍请移步:http://blog.csdn.net/yiranxinshou/article/details/9201833
NULL 博文链接:https://geyubin.iteye.com/blog/1633134
Title : dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent三者对MotionEvent的分发、拦截机制分析 Author: yaong Category:自己做的Demo Tips : 三个自定义View,依次嵌套,并各自覆写拦截、监听方法,通过...
onInterceptTouchEvent与onTouchEvent事件传递的分析
onInterceptTouchEvent 和 onTouchEvent 研究
View的onInterceptTouchEvent(MotionEvent ev)和dispatchTouchEvent(MotionEvent ev) 解析
onInterceptTouchEvent和onTouchEvent调用时序
文章大纲引言一、CoordinatorLayout核心角色1、CoordinatorLayout....onInterceptTouchEvent方法设置是否拦截触摸事件2.5、onTouchEvent方法处理触摸事件2.6、onMeasureChild方法测量使用Behavior的View尺寸2.
基于这个猜测,我自定义一个ViewPager,重写dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent,分别在这三个方法中打印log; 重写SlidingMenu的dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent,...
在View中其间会调用onTouchEvent(),在ViewGroup中其间会调用onInterceptTouchEvent()和onTouchEvent()。 **onInterceptTouchEvent():**这个函数是事件拦截函数,是ViewGroup才有的函数。
(之前上传的代码有bug,经过作者整理和修改,fix掉部分bug) ...关键技术是理解清楚“onInterceptTouchEvent和onTouchEvent”的关系,以及computeScroll和dispatchDraw等。 可以在百度中搜索关键词获取答案。