Android事件模型之interceptTouchEvnet ,onTouchEvent关系正解
首先,看Android的官方文档正解
onInterceptTouchEvent()与onTouchEvent()的机制:
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()处理
这是官方文档的说法,要是自己没亲自去写个程序观察哈,基本上没法理解,所以上程序先,然后分析:
布局文件main.xml
- <span style="font-size: small;"><?xml version="1.0" encoding="utf-8"?>
- <com.hao.LayoutView1 xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <com.hao.LayoutView2
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:gravity="center">
- <com.hao.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.hao.LayoutView2>
- </com.hao.LayoutView1>
- </span>
第一层自定义布局LayoutView1.java
- <span style="font-size: small;">package com.hao;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.widget.LinearLayout;
- public class LayoutView1 extends LinearLayout {
- private final String TAG = "LayoutView1";
- public LayoutView1(Context context, AttributeSet attrs) {
- super(context, attrs);
- Log.e(TAG,TAG);
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- int action = ev.getAction();
- switch(action){
- case MotionEvent.ACTION_DOWN:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_DOWN");
- // return true; 在这就拦截了,后面的就不会得到事件
- break;
- case MotionEvent.ACTION_MOVE:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_MOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_UP");
- break;
- case MotionEvent.ACTION_CANCEL:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_CANCEL");
- break;
- }
- return false;
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- int action = ev.getAction();
- switch(action){
- case MotionEvent.ACTION_DOWN:
- Log.e(TAG,"onTouchEvent action:ACTION_DOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.e(TAG,"onTouchEvent action:ACTION_MOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.e(TAG,"onTouchEvent action:ACTION_UP");
- break;
- case MotionEvent.ACTION_CANCEL:
- Log.e(TAG,"onTouchEvent action:ACTION_CANCEL");
- break;
- }
- return true;
- // return false;
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- // TODO Auto-generated method stub
- super.onLayout(changed, l, t, r, b);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- // TODO Auto-generated method stub
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- }
- }</span>
第二层布局LayoutView2.java
- <span style="font-size: small;">package com.hao;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.widget.LinearLayout;
- public class LayoutView2 extends LinearLayout {
- private final String TAG = "LayoutView2";
- public LayoutView2(Context context, AttributeSet attrs) {
- super(context, attrs);
- Log.e(TAG,TAG);
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- int action = ev.getAction();
- switch(action){
- case MotionEvent.ACTION_DOWN:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_DOWN");
- // return true;
- break;
- case MotionEvent.ACTION_MOVE:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_MOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_UP");
- break;
- case MotionEvent.ACTION_CANCEL:
- Log.e(TAG,"onInterceptTouchEvent action:ACTION_CANCEL");
- break;
- }
- return false;
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- int action = ev.getAction();
- switch(action){
- case MotionEvent.ACTION_DOWN:
- Log.e(TAG,"onTouchEvent action:ACTION_DOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.e(TAG,"onTouchEvent action:ACTION_MOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.e(TAG,"onTouchEvent action:ACTION_UP");
- break;
- case MotionEvent.ACTION_CANCEL:
- Log.e(TAG,"onTouchEvent action:ACTION_CANCEL");
- break;
- }
- // return true;
- return false;
- }
- }
- </span>
自定义MyTextView.java
- <span style="font-size: small;">package com.hao;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.TextView;
- public class MyTextView extends TextView {
- private final String TAG = "MyTextView";
- public MyTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- Log.e(TAG,TAG);
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- int action = ev.getAction();
- switch(action){
- case MotionEvent.ACTION_DOWN:
- Log.e(TAG,"onTouchEvent action:ACTION_DOWN");
- break;
- case MotionEvent.ACTION_MOVE:
- Log.e(TAG,"onTouchEvent action:ACTION_MOVE");
- break;
- case MotionEvent.ACTION_UP:
- Log.e(TAG,"onTouchEvent action:ACTION_UP");
- break;
- case MotionEvent.ACTION_CANCEL:
- Log.e(TAG,"onTouchEvent action:ACTION_CANCEL");
- break;
- }
- return false;
- // return true;
- }
- public void onClick(View v) {
- Log.e(TAG, "onClick");
- }
- public boolean onLongClick(View v) {
- Log.e(TAG, "onLongClick");
- return false;
- }
- }
- </span>
其实代码很简单,就是自定义了View,在View里面都重写了interceptTouchEvnet (),和onTouchEvent(),然后测试其返回值,对监听的影响,关键是自己动手,逐个测试,并预测结果,等你能预测结果的时候,也就懂了,需要修改的地方就是interceptTouchEvnet 和onTouchEvent的返回值,他们决定了事件监听的流程,下面我画了一张图,如有不足之处欢迎指正,谢谢!
下面是我的正解:

下附源代码:
- TouchEventTest.rar (94.3 KB)
- 下载次数: 57
Android事件模型之interceptTouchEvnet ,onTouchEvent关系正解的更多相关文章
- Android 事件模型
本文内容 基于监听的事件模型 基于回调的事件模型 Android 支持两种事件模型,基于监听的事件模型和基于回调的事件模型. 基于监听的事件模型 基于监听的事件模型是一种委托式的,更"面向对 ...
- Android事件机制之二:onTouch详解
<Android事件机制之一:事件传递和消费>一文总结了Android中的事件传递和消费机制. 在其中对OntachEvent中的总结中,不是很具体.本文将主要对onTach进行总结. o ...
- 对于android触摸事件模型的一些理解
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- Android进阶——Android事件分发机制之dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent
Android事件分发机制可以说是我们Android工程师面试题中的必考题,弄懂它的原理是我们避不开的任务,所以长痛不如短痛,花点时间干掉他,废话不多说,开车啦 Android事件分发机制的发生在Vi ...
- 【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发逻辑及经常遇到的一些“诡异”现象
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五 ...
- 【朝花夕拾】Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/10998855.html]谢谢! 在自定义View中,经常需要处理Android事件分发的问题, ...
- Android事件分发机制三:事件分发工作流程
前言 很高兴遇见你~ 本文是事件分发系列的第三篇. 在前两篇文章中,Android事件分发机制一:事件是如何到达activity的? 分析了事件分发的真正起点:viewRootImpl,Activit ...
- Android事件分发机制四:学了事件分发有什么用?
" 学了事件分发,影响我CV大法吗?" " 影响我陪女朋友的时间" " ..... " 前言 Android事件分发机制已经来到第四篇了,在 ...
- 讲讲Android事件拦截机制
简介 什么是触摸事件?顾名思义,触摸事件就是捕获触摸屏幕后产生的事件.当点击一个按钮时,通常会产生两个或者三个事件--按钮按下,这是事件一,如果滑动几下,这是事件二,当手抬起,这是事件三.所以在And ...
随机推荐
- js this [转]
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 1.纯粹函数调用. function test() { this. ...
- projecteuler之58题Spiral primes
package com.android; public class SpiralPrimes { public static void main(String args[]) { long numPr ...
- LinuxC安装gcc
使用centos进行C编程的时候使用gcc hello.c提示 bash:gcc:command not found 此时需要给Linux安装gcc命令如下 1 yum -y install gcc ...
- asp.net core+ef core
asp.net core+ef core 官方的文档https://docs.asp.net/en/latest/tutorials/first-mvc-app/start-mvc.html 先来看一 ...
- 系统运维-hub, repeater, switch, router初览
repeater, hub, switch, router都是针对与osi的7层网络模型的不同层而产生的设备,逐一概览如下: repeater:中继器 是网络物理层的一种连接设备,工作在osi的物理层 ...
- Qt技巧:Win7下打包发布Qt程序(解释的比较清楚,把exe和dll伪装合并成一个文件)
转自:http://www.stardrad.com/blog/qt-5%E7%A8%8B%E5%BA%8F%E5%9C%A8windows%E4%B8%8A%E7%9A%84%E5%8F%91%E5 ...
- 数据切分——MySql表分区概述
定义: 表的分区指根据可以设置为任意大小的规则,跨文件系统分配单个表的多个部分.实际上,表的不同部分在不同的位置被存储为单独的表.用户所选择的.实现数据分割的规则被称为分区函数,这在M ...
- 顺序队列之C++实现
下面介绍下用C++实现的顺序队列,在VC6下调试通过. 1.文件组织形式 2.sq.h顺序队列类的说明 #ifndef _SQ_H_ #define _SQ_H_ typedef int dataTy ...
- Failed to load the JNI shared library
解决Eclipse无法打开"Failed to load the JNI shared library" 这是由于JDK配置错误所导致的现象. 一般说来,新购笔记本会预装64位的w ...
- levelDB跳表实现
跳表的原理就是利用随机性建立索引,加速搜索,并且简化代码实现难度.具体的跳表原理不再赘述,主要是看了levelDB有一些实现细节的东西,凸显自己写的实现不足之处. 去除冗余的key template& ...