onInterceptTouchEvent()用于处理事件并改变事件的传递方向。处理事件这个不用说了,你在函数内部编写代码处理就可以了。而决定传递方向的是返回值,返回为false时事件会传递给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控件,这就是所谓的Intercept(截断)。

【科学上网软件点击下载(能上youtube、facebook,享受google服务)】

onTouchEvent()用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。

本文源地址:http://www.cnblogs.com/rocky_yi/archive/2011/01/21/1941522.html#,转载请注明出处!

<?xmlversion="1.0"encoding="utf-8"?>
<com.touchstudy.LayoutView1xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.touchstudy.LayoutView2
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<com.touchstudy.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="AB"
android:textSize="40sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textColor="#0000FF"/>
</com.touchstudy.LayoutView2>
</com.touchstudy.LayoutView1>

在没有重写onInterceptTouchEvent()和onTouchEvent()的情况下(他们的返回值都是false),对上面这个布局,MotionEvent事件的传递顺序如下:

当某个控件的onInterceptTouchEvent()返回值为true时,就会发生截断,事件被传到当前控件的onTouchEvent()。如我们将LayoutView2的onInterceptTouchEvent()返回值为true,则传递流程变成:

如果我们同时将LayoutView2的onInterceptTouchEvent()和onTouchEvent()设置成true,那么LayoutView2将消费被传递的事件,同时后续事件(如跟着ACTION_DOWN的ACTION_MOVE或者ACTION_UP)会直接传给LayoutView2的onTouchEvent(),不传给其他任何控件的任何函数。同时传递给子空间一个ACTION_CANCEL事件。传递流程变成(图中没有画出ACTION_CANCEL事件):

附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.

MotionEvent事件在onInterceptTouchEvent()、onTouchEvent()中的传递顺序相关推荐

  1. Android中dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent的理解

    [转]http://blog.csdn.net/guitk/article/details/7057155 onInterceptTouchEvent用于改变事件的传递方向.决定传递方向的是返回值,返 ...

  2. onInterceptTouchEvent / onTouchEvent响应事件的详析

    onInterceptTouchEvent(),onTouchEvent() 默认返回false 注:事件分发机制是由 dispatchTouchEvent() 开始分发的 onInterceptTo ...

  3. QT事件事件之一:Qt中的事件处理与传递

    QT事件事件之一:Qt中的事件处理与传递 前言 一.简介 二.QT中的事件 三.事件的实现的方法 前言 在QT中,事件是我们很常用的东西,以下是我用事件时总结和做法 一.简介 在QT中,事件作为一个对 ...

  4. MotionEvent 事件解读

    Android在MotionEvent里定义了一系列的手势事件 其中包括: MotionEvent.ACTION_DOWN:当屏幕检测到第一个触点按下之后就会触发到这个事件. MotionEvent. ...

  5. 【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发机制...

    前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五 ...

  6. 【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发逻辑及经常遇到的一些“诡异”现象

    前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五 ...

  7. Android MotionEvent事件分发介绍与流程总结(伪代码形式)

        如果要一句话简单总结的话,就是:     找到一个按照规则"消耗"掉MotionEvent.ACTION_DOWN事件的View,默认情况下,后继会把整个事件流都交给它来处 ...

  8. Android 触摸事件机制(三) View中触摸事件详解

    本文将对View中触摸事件相关的内容进行介绍.重点介绍的是dispatchTouchEvent(), onTouchEvent()这两个API以及OnTouchListener接口. 注意:本文是基于 ...

  9. 智慧北京02_初步ui框架_ 主界面_viewPager事件_xUtils_slidingMenu_网络缓存_数据传递...

    智慧北京02_初步ui框架_ 主界面_viewPager事件_xUtils_slidingMenu_网络缓存_数据传递 1.使用Fragment搭建ui框架 参考分析图 1.1,Fragment生命周 ...

最新文章

  1. ssh开发实战之整合篇
  2. linux在执行时提示无此文件的一个处理办法
  3. 把十进制转为二进制到16进制
  4. 关于dialog的一点东西
  5. jsp数据传入html页面_曾经风光无限的 JSP,为什么现在很少有人使用了?
  6. 把java程序打包成.exe
  7. class.forname()中要写相对路径吗?_自考考试时间推迟,自考通过率会降低吗?
  8. 计算机仿真塞曼效应实验报告,实验报告模板
  9. 微信浏览器自动关闭页面
  10. gggggggggggggggggdsfdsf
  11. vb.net 简单登录界面(三层架构思想)
  12. SQL Developer连接时报错:ORA-12528
  13. 今日冬至《心的深处》孙溟㠭篆刻艺术
  14. python英文版怎么改成中文版_Python中文(非英文字符)解决方法
  15. 无人驾驶路径规划(三)局部路径规划-Frenet坐标系下的动态轨迹规划
  16. 你眼中的罗振宇和罗辑思维?
  17. 《三国演义》人物出场统计
  18. java程序伪代码_Java学习笔记——伪代码及程序控制
  19. 大数据挖掘建模案例分析:利用BP神经网络算法进行用户行为分析(一)
  20. 开心笑谈的背后是中国式无奈

热门文章

  1. JAVA常见算法题(二十五)
  2. sqlmap工具使用用法详解
  3. Ubuntu 12.04.1 mysql从5.5升级到5.6
  4. 找出符合下图的互联网产品实例
  5. 10-RabbitMQ-整合SpringBoot
  6. Docker应用基础
  7. 发展大数据还有啥问题:数据孤岛、技术差距、人才短缺
  8. Linux高级管理之ACL(访问控制列表)实战应用
  9. 怎样把ACCESS导入SQL数据库
  10. [Android] 环境优化配置Android Studio发展NDK