Android ViewGroup onInterceptTouchEvent
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的更多相关文章
- ViewGroup onInterceptTouchEvent,ViewGroup onTouchEvent,View onTouchEvent执行顺序说明
今天抽出了一些时间实践了viewgroup和view的触摸事件顺序,之前也试过,总是忘记,今天记下笔记说明一下 首先 onInterceptTouchEvent只会出现在viewgroup中,view ...
- Android在onInterceptTouchEvent与onTouchEvent
onInterceptTouchEvent: onInterceptTouchEvent是在ViewGroup里面定义的.Android中的layout布局类一般都是继承此类的.onIntercept ...
- Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理
在Android中需要经常对用户手势进行判断,在判断手势时需要精细的分清楚每个触摸事件以及每个View对事件的接收情况,在View,ViewGroup,Activity中都可以接收事件,在对事件进行处 ...
- Android ViewGroup拦截触摸事件具体解释
前言 在自己定义ViewGroup中.有时候须要实现触摸事件拦截.比方ListView下拉刷新就是典型的触摸事件拦截的样例. 触摸事件拦截就是在触摸事件被parent view拦截,而不会分发给其ch ...
- android ViewGroup事件分发机制
1:事件分销过程 自定义一个LinearLayout,重写dispatchTouchEvent onInterceptTouchEvent onTouchEvent,定义一个按键重写dispathcT ...
- Android -- ViewGroup源码分析+自定义
1,我们前三篇博客了解了一下自定义View的基本方法和流程 从源码的角度一步步打造自己的TextView 深入了解自定义属性 onMeasure()源码分析 之前,我们只是学习过自定义View,其实自 ...
- Android ViewGroup等容器控件的使用
在Android中,可以自定义类,继承ViewGroup等容器类,以实现自己需要的布局显示.如果你在ViewGroup中增加了控件,却无法显示出 来,那么下面这个例子,就可以用来参考了.(主要是要实现 ...
- Android ViewGroup使用小结
ViewGroup定义 在api中是这么描写叙述ViewGroup的:A ViewGroup is a special view that can contain other views. 依据意思我 ...
- Android ViewGroup点击效果(背景色)
在开发Android应用的界面时,我们必然会用到本文ViewGroup,尤其是FrameLayout,LinearLayout,RelativeLayout等ViewGroup的子类: 在一些情况下, ...
随机推荐
- JavaScript学习日志(1)
javascript用法: 1.HTML中的脚本必须位于<script>与</script>标签之间,可被放置在HTML页面的<body>和<head> ...
- HTML5自定义属性之data-index
#使用jquery获取data-index的值 jquery 的版本最好高一些 #html <div id = 'div'><span data-field='demo'>&l ...
- word中特殊符号的替换
首先在word里替换快捷键是ctrl+H,点击“更多”,会出现更多选项,在特殊格式那里可以选在各种符号,比如回车,空格什么的. 有的时候在word里看不到一些格式,需要点一下下图的对着的两个箭头图标: ...
- 如何将自己的网站分享到QQ空间,微信,微博等等。
逛一些网站的时候经常会看到右侧挂个分享栏,让用户把自己的站分享到qq空间,微信等等,所以自己也研究了下,把他加到了自己的网站上,喜欢的可以先看看效果:去转盘网,不多说了,直接上代码: window._ ...
- Servlet 容器对URI的处理
问题 请求到达server以后,server是怎样处理URI请求资源路径的,在与web.xml文件里的映射进行比对时的原则是什么. 方案 针对精确匹配.通配符匹配.后缀匹配三种模式改动web.xml文 ...
- 第二百一十七节,jQuery EasyUI,NumberSpinner(数字微调)组件
jQuery EasyUI,NumberSpinner(数字微调)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 NumberSpinner ...
- C#从Excel中读取数据为空
将HDR设置为YES,IMEX设置为1即可. OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.ACE.OL ...
- 64位系统下,一个32位的程序究竟可以申请到多少内存,4GB还是更多?(一)
前言: cpu的位是指一次性可处理的数据量是多少,1字节=8位,32位处理器可以一次性处理4个字节的数据量,依次类推.32位操作系统针对的32位的CPU设计.64位操作系统针对的64位的CPU设计.操 ...
- Revit 2017 编程须要用Visual Studio2015 +.NET Framework 4.52
一年一度的Revit产品公布时刻,我们抢先想各位介绍下Revit 2017的变化和新功能 Major changes and renovations to the Revit API API chan ...
- 将socket5代理转换为http代理
归根结底还是万恶的ZFW,MD弄得现在Google,gmail,dropbox都用不了了,看来这是要万民欢迎大中华局域网的到来了.最近在使用一同学的Shadowsocks来突破GFW的限制,可是电脑o ...