跟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. hdu 1186(搜索+HASH)

    方程的解数 Time Limit: 15000MS   Memory Limit: 128000K Total Submissions: 7045   Accepted: 2417 Case Time ...

  2. LeetCode OJ——Plus One

    http://oj.leetcode.com/problems/plus-one/ 进位加法 #include <iostream> #include <vector> usi ...

  3. elasticsearch入库错误:gc overhead导致数据节点脱离集群

    https://my.oschina.net/u/3625378/blog/1793796

  4. ssm实现查看流程图

    jsp <!--显示数据列表--> <tbody id="TableData" class="dataContainer" datakey=& ...

  5. .net 哈希

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  6. 随笔:Golang 时间Time

    先了解下time类型: type Time struct { // sec gives the number of seconds elapsed since // January 1, year 1 ...

  7. TClientDataSet的 fastscript封装

    TClientDataSet的 fastscript封装 // 陈新光 2017-2-10// TClientDataSet's fastscript unit fs_ClientDataSet; i ...

  8. urllib(最基本的)库的应用

    Urllib库 python内置的http请求库 1.urllib.request 请求模块 2.urllib.error 异常处理模块(try,catch) 3.urllib.parse url解析 ...

  9. django_session

    基于cookie做用户验证时:敏感信息不适合放在cookie中 session依赖cookie session原理 cookie是保存在用户浏览器端的键值对 session是保存在服务器端的键值对 s ...

  10. python--函数嵌套 命名空间

    函数的嵌套调用 def func(): print(123) def func2(): func() print(345) func2() #结果:123 345 函数的嵌套定义 def func() ...