跟touch事件相关的3个方法:
public boolean dispatchTouchEvent(MotionEvent ev); //用来分派event
public boolean onInterceptTouchEvent(MotionEvent ev); //用来拦截event
public boolean onTouchEvent(MotionEvent ev); //用来处理event
三个方法的用法:
dispatchTouchEvent() 用来分派事件。
其中调用了onInterceptTouchEvent()和onTouchEvent(),一般不重写该方法
onInterceptTouchEvent() 用来拦截事件。
ViewGroup类中的源码实现就是{return false;}表示不拦截该事件,
事件将向下传递(传递给其子View);
若手动重写该方法,使其返回true则表示拦截,事件将终止向下传递
事件由当前ViewGroup类来处理,就是调用该类的onTouchEvent()方法
onTouchEvent() 用来处理事件。
返回true则表示该View能处理该事件,事件将终止向上传递(传递给其父View);
返回false表示不能处理,则把事件传递给其父View的onTouchEvent()方法来处理
【注】:ViewGroup的某些子类(GridView、ScrollView…)重写了onInterceptTouchEvent()方法,当发生ACTION_MOVE事件时,返回true进行拦截。

拥有这三个方法的类:
Activity        :dispatchTouchEvent();                 onTouchEvent();
ViewGroup       :dispatchTouchEvent();   onInterceptTouchEvent();     onTouchEvent();
View         :dispatchTouchEvent();                 onTouchEvent();

现在重写  activity ViewGroup  View 的相应方法,构造布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <com.event.MyViewGroup
android:id="@+id/myViewGroup"
android:background="@color/colorPrimary"
android:layout_width="400dp"
android:layout_height="400dp"> <com.event.MyView
android:id="@+id/myView"
android:layout_width="200dp"
android:layout_height="200dp" />
</com.pig.event.MyViewGroup> </android.support.constraint.ConstraintLayout>

1、点击空白处(activity),输出:

activity   dispatchTouchEvent:
activity onTouchEvent:

直接点击  activity  触发  activity相应方法。与 内部控件无关

2、点击蓝色部分(ViewGroup),输出:(当我们手动设置OnTouchListener  并且返回true时,会拦截事件, onTouchEvent不会触发   )

 activity   dispatchTouchEvent:
group dispatchTouchEvent:
group onInterceptTouchEvent:
group onTouch: (OnTouchListener 部分)
group onTouchEvent:
activity onTouchEvent:

(1)由于activity没有拦截事件的方法,事件肯定会传进ViewGroup。

(2)ViewGroup 中的onTouchEvent 默认返回fasle  表示事件未处理完,则会触发activity的onTouchEvent

3、点击按钮(View),输出:(当我们手动设置OnTouchListener  并且返回true时,会拦截事件, onTouchEvent不会触发   )

 activity   dispatchTouchEvent:
group dispatchTouchEvent:
group onInterceptTouchEvent:
view dispatchTouchEvent:
view onTouch: (OnTouchListener 部分)
view onTouchEvent:

(1)activity分发事件进ViewGroup ,ViewGroup分发事件时,选择是否拦截事件,若不拦截则分发事件到View,若拦截事件,ViewGroup自己将事件处理,View不会接收到事件。

(2)view 的onTouchEvent默认返回true ,表示事件以处理完,不会触发ViewGroup  和 activity 的onTouchEvent。

Android Touch事件分发的更多相关文章

  1. Android Touch事件分发过程

    虽然网络上已经有非常多关于这个话题的优秀文章了,但还是写了这篇文章,主要还是为了加强自己的记忆吧,自己过一遍总比看别人的分析要深刻得多.那就走起吧. 简单演示样例 先看一个演示样例 : 布局文件 : ...

  2. 【转】Android:Touch事件分发机制

    Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewGroup处理. View在 ...

  3. Android Touch事件分发机制

    参考:http://blog.csdn.net/xiaanming/article/details/21696315 参考:http://blog.csdn.net/wangjinyu501/arti ...

  4. Android Touch事件分发机制学习

    Android  事件分发机制 ViewGroup dispatchTouchEvent 返回true dispatchTouchEvent: Activity ACTION_DOWN Myrelat ...

  5. android touch事件分发流程

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 三个方法:分发触摸事件dispatchTouchEvent.在触摸事件的时候onTouc ...

  6. Android开发之Touch事件分发机制

    原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...

  7. Android事件分发机制(二)30分钟弄明白Touch事件分发机制

    Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewGroup处理. View在 ...

  8. Android:30分钟弄明白Touch事件分发机制

    Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewGroup处理. View在 ...

  9. Android Touch事件传递机制 二:单纯的(伪生命周期)

    转载于:http://blog.csdn.net/yuanzeyao/article/details/38025165 在前一篇文章中,我主要讲解了Android源码中的Touch事件的传递过程,现在 ...

随机推荐

  1. LeetCode OJ——Longest Valid Parentheses

    http://oj.leetcode.com/problems/longest-valid-parentheses/ 最大括号匹配长度,括号是可以嵌套的 #include <string> ...

  2. 51 Nod 1238 最小公倍数之和 V3 杜教筛

    题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1238 题意:求$\sum_{i=1}^{n}\sum_{j=1}^{n}l ...

  3. (38)C#IIS

    IIS7.5标识介绍(转) http://www.cnblogs.com/zgqys1980/p/3862815.html 应用程序池的标识是运行应用程序池的工作进程所使用的服务帐户名称.默认情况下, ...

  4. CodeForces - 393E Yet Another Number Sequence

    Discription Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  5. objective-c 类目(Category)和延展(Extension)

    类目的基本概念: 如果有封装好的一个类,随着程序功能的增加,需要在类中增加一个方法,那我们就不必在那个类中做修改或者再定义一个子类,只需要在用到那个方法时添加一个该类的类目即可. 1.在类目定义的方法 ...

  6. android wifi state and wifi ap state

    /** * Wi-Fi is currently being disabled. The state will change to {@link #WIFI_STATE_DISABLED} if * ...

  7. C 标准库 - <float.h>

    C 标准库 - <float.h> 简介 C 标准库的 float.h 头文件包含了一组与浮点值相关的依赖于平台的常量.这些常量是由 ANSI C 提出的,这让程序更具有可移植性.在讲解这 ...

  8. 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载一(PhoneGap中的API)

    之前本博连载过<构建跨平台APP:jQuery Mobile移动应用实战>一书.深受移动开发入门人员的喜爱. 从如今開始,连载它的孪生姐妹书phoneGap移动应用实战一书,希望以前是小白 ...

  9. Phpstorm 放大字体的快捷键是什么?

      这个功能需要设置才能使用: 步骤:control+shift+A功能可以搜索对应功能 输入mouse 设置Change font size(Zoom) ...的按钮打开,然后就可以通过 ctrl+ ...

  10. springMVC学习之验证

    验证框中@NotEmpty.@NotBlank.@NotNull乍一看还是容易弄混的.主要使用情况记录一下: @NotEmpty 用在集合类上面 @NotBlank 用在String上面 @NotNu ...