public boolean onInterceptTouchEvent (MotionEvent ev)

  Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to

your children, and take ownership of the current gesture at any point.

  Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it

requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1)You will receive the down event here.

  2)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.   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.

  3)For as long as you return false from this function, each following event  will be delivered first here and then to the target's

onTouchEvent().

  4)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.

  1)在 onInterceptTouchEvent()接收到down事件

  2)ViewGroup 或者 ViewGroup里面的 View 都可以处理 down事件

  3)只要onInterceptTouchEvent()返回false, 则剩下的事件都会先发送到onInterceptTouchEvent(),再发送到目标对象的onTouchEvent()

  4)如果onInterceptTouchEvent()返回true, 则剩下的事件不会发送到ViewGroup 的 onInterceptTouchEvent()和目标对象的onTouchEvent()

只会发送给ViewGroup 的 onTouchEvent()

 Returns:

  • Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent().

The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

返回true会调用ViewGroup的onTouchEvent()。

一。实验代码

1.ViewGroup

public class GlingLayout extends FrameLayout {

    private static String tag = GlingLayout.class.getSimpleName();

    public GlingLayout(Context context){
super(context);
} public GlingLayout(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(tag, " --- down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(tag, " --- move");
break;
case MotionEvent.ACTION_UP:
Log.e(tag, " --- up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(tag, " --- cancel");
break;
default:
break;
}
return false;
} @Override
public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(tag, " --------------------- down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(tag, " ------------------ move");
break;
case MotionEvent.ACTION_UP:
Log.e(tag, " ----------------- up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(tag, " ----------------- cancel");
break;
default:
break;
}
return true;
}
}

2.View

package com.gl;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button; public class GlingButton extends Button { private static String tag = GlingButton.class.getSimpleName(); public GlingButton(Context context){
super(context);
} public GlingButton(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(tag, " down ---------------");
break;
case MotionEvent.ACTION_MOVE:
Log.e(tag, " move --------------- ");
break;
case MotionEvent.ACTION_UP:
Log.e(tag, " up ---------------");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(tag, " cancel ---------------");
break;
default:
break;
}
return true;
} }

二.结果

1.onInterceptTouchEvent返回false时

2.1.onInterceptTouchEvent返回true时

三。让ViewGroup不能截断TouchEvent

public void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).

This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.

Parameters

Android ViewGroup onInterceptTouchEvent的更多相关文章

  1. ViewGroup onInterceptTouchEvent,ViewGroup onTouchEvent,View onTouchEvent执行顺序说明

    今天抽出了一些时间实践了viewgroup和view的触摸事件顺序,之前也试过,总是忘记,今天记下笔记说明一下 首先 onInterceptTouchEvent只会出现在viewgroup中,view ...

  2. Android在onInterceptTouchEvent与onTouchEvent

    onInterceptTouchEvent: onInterceptTouchEvent是在ViewGroup里面定义的.Android中的layout布局类一般都是继承此类的.onIntercept ...

  3. Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理

    在Android中需要经常对用户手势进行判断,在判断手势时需要精细的分清楚每个触摸事件以及每个View对事件的接收情况,在View,ViewGroup,Activity中都可以接收事件,在对事件进行处 ...

  4. Android ViewGroup拦截触摸事件具体解释

    前言 在自己定义ViewGroup中.有时候须要实现触摸事件拦截.比方ListView下拉刷新就是典型的触摸事件拦截的样例. 触摸事件拦截就是在触摸事件被parent view拦截,而不会分发给其ch ...

  5. android ViewGroup事件分发机制

    1:事件分销过程 自定义一个LinearLayout,重写dispatchTouchEvent onInterceptTouchEvent onTouchEvent,定义一个按键重写dispathcT ...

  6. Android -- ViewGroup源码分析+自定义

    1,我们前三篇博客了解了一下自定义View的基本方法和流程 从源码的角度一步步打造自己的TextView 深入了解自定义属性 onMeasure()源码分析 之前,我们只是学习过自定义View,其实自 ...

  7. Android ViewGroup等容器控件的使用

    在Android中,可以自定义类,继承ViewGroup等容器类,以实现自己需要的布局显示.如果你在ViewGroup中增加了控件,却无法显示出 来,那么下面这个例子,就可以用来参考了.(主要是要实现 ...

  8. Android ViewGroup使用小结

    ViewGroup定义 在api中是这么描写叙述ViewGroup的:A ViewGroup is a special view that can contain other views. 依据意思我 ...

  9. Android ViewGroup点击效果(背景色)

    在开发Android应用的界面时,我们必然会用到本文ViewGroup,尤其是FrameLayout,LinearLayout,RelativeLayout等ViewGroup的子类: 在一些情况下, ...

随机推荐

  1. iOS直播-播放基于RTMP协议的视频

    iOS直播-播放基于RTMP协议的视频 流媒体协议介绍 1. 伪流媒体: 渐进式下载 : 边下边存, 文件会保存 使用http协议,也能够实现视频播放, 也能快进快退等, 体验上跟流媒体很像. 优酷, ...

  2. 进程隐藏与进程保护(SSDT Hook 实现)(三)

    文章目录: 1. 引子: 2. 获取当前系统下所有进程: 3. 服务管理(安装,启动,停止,卸载): 4. 应用程序和内核程序通信: 5. 小结: 1. 引子: 关于这个 SSDT Hook 实现进程 ...

  3. 如何设置UITextView不可被编辑

    在项目中遇到一些需求需要把文字用UITextView来展示,但是该文字不能被编辑,只要把以下该代理方法实现就可以了 -(BOOL)textViewShouldBeginEditing:(UITextV ...

  4. matlab hornerDemo

    % a quick demo of Horner's method and its effects clear all close all % first a comparison of ways t ...

  5. Webdings 图形字体

    如果想在网页上插入图形,最简单的方式就是使用图形字体.Webdings 是一种微软开发的图形字体,在IE浏览器上可以使用它. 什么是Webdings Webdings 是一个TrueType的ding ...

  6. Linux多条指令之间;和&&

    Linux 中经常使用到一个命令,如 make && make install,这里也可以使用 make ; make install,那么在 Linux 中执行命令 ; 和 & ...

  7. flask os.environ 的作用

    使用环境变量取值, 是为了增强系统的适应性, 在某些场景下, 设置环境变量比较方便. 假如, 你有一套代码, 部署在不同的系统中, 恰好这些系统有权限且很容易地设置环境变量, 那么, 这时候通过环境变 ...

  8. vs2003 不断提示 已过期 问题

    工作时曾遇到使用 vs2003 生成后,点击"调试“或者"执行“后不断提示 类似“已过期,是否要重新生成”这样的问题. 当时的情况是 :我要实现的功能和"时间" ...

  9. servlet之模板方法和多线程

    接触了一小段时间的servlet,以下就总结一下关于servlet的两个方面的知识,一个是模板方法的应用.另外一个是servlet多线程产生的原因. 1. 模板方法设计模式 定义一个操作中的算法的骨架 ...

  10. iOS-tableView会卡顿

    其实影响tableView卡顿的因素有很多,我也就其中一些常见的问题来说一下. 在tableView里的tableViewCell中使用许多图片的时候,而且我们大量使用的是 xxx.clipsToBo ...