探究requestDisallowInterceptTouchEvent失效的原因
昨天在用requestDisallowInterceptTouchEvent的时候,发如今设置了requestDisallowInterceptTouchEvent(true)之后,父View的onInterceptTouchEvent方法照样运行。
怎么会这样呢?仅仅能通过查看源代码来一探到底了。
首先看下requestDisallowInterceptTouchEvent方法的源代码,在android.view.ViewGroup类中:
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
// We're already in this state, assume our ancestors are too
return;
}
if (disallowIntercept) {
mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
} else {
mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
}
// Pass it up to our parent
if (mParent != null) {
mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
}
}
这里看到,requestDisallowInterceptTouchEvent方法做了两个操作:
1. 设置ViewGroup自己的FLAG_DISALLOW_INTERCEPT标志位为true。
2. 递归设置ViewGroup其父View的FLAG_DISALLOW_INTERCEPT标志位为true。
主要和FLAG_DISALLOW_INTERCEPT标志位有关。这里全局看下android.view.ViewGroup的源码,发现仅仅有三个地方用到了FLAG_DISALLOW_INTERCEPT:
read:dispatchTouchEvent方法。
write:requestDisallowInterceptTouchEvent方法,resetTouchState方法。
先来看下涉及到write操作的地方,requestDisallowInterceptTouchEvent方法前面看过了,这里看下resetTouchState方法的源码:
/**
* Resets all touch state in preparation for a new cycle.
*/
private void resetTouchState() {
clearTouchTargets();
resetCancelNextUpFlag(this);
mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
mNestedScrollAxes = SCROLL_AXIS_NONE;
}
这里看到。resetTouchState方法会把ViewGroup的FLAG_DISALLOW_INTERCEPT置为false。
再来看下涉及到读操作的dispatchTouchEvent方法的源码:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) { ...... // Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
intercepted = true;
} ......
}
这里就是整个ViewGroup代码中唯一用到FLAG_DISALLOW_INTERCEPT的地方。它的主要罗辑就是推断ViewGroup的FLAG_DISALLOW_INTERCEPT标志位是否为true。假设为true,调用ViewGroup的onInterceptTouchEvent的方法。假设不是则不会走。
这也和requestDisallowInterceptTouchEvent的官方解释也是一致的。
而我遇到的问题是:明明调用了requestDisallowInterceptTouchEvent(true)方法。而还是走了ViewGroup的onInterceptTouchEvent方法,那仅仅有一个可能:FLAG_DISALLOW_INTERCEPT被改回了false。对FLAG_DISALLOW_INTERCEPT进行写操作的除了requestDisallowInterceptTouchEvent方法,仅仅有resetTouchState方法,一定是某个地方调用了resetTouchState方法,把FLAG_DISALLOW_INTERCEPT标志位置为false。才造成了我遇到的问题。
看一下resetTouchState的调用关系,发现仅仅有一个方法调用了:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) { ...... final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK; // Handle an initial down.
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Throw away all previous state when starting a new touch gesture.
// The framework may have dropped the up or cancel event for the previous gesture
// due to an app switch, ANR, or some other state change.
cancelAndClearTouchTargets(ev);
resetTouchState();
} ...... // Update list of touch targets for pointer up or cancel, if needed.
if (canceled
|| actionMasked == MotionEvent.ACTION_UP
|| actionMasked == MotionEvent.) {
resetTouchState();
} else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
final int actionIndex = ev.getActionIndex();
final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
removePointersFromTouchTargets(idBitsToRemove);
} ......
}
看完代码就发现,原来在ViewGroup的ACTION_DOWN时,FLAG_DISALLOW_INTERCEPT标志位被置为false,这就是我使用requestDisallowInterceptTouchEvent方法失效的原因。由于我是在findView之后,直接调用的requestDisallowInterceptTouchEvent(true),而在事件处理的開始(ACTION_DOWN)。这里又被设为了false。
怎么解决问题呢?仅仅有重写自己用到的View的onTouchEvent方法,在其ACTION_DOWN的时候,调用父View的requestDisallowInterceptTouchEvent(true)方法设置,在ACTION_UP或者ACTION_CANCEL的时候。调用调用父View的requestDisallowInterceptTouchEvent(false)方法重置。
探究requestDisallowInterceptTouchEvent失效的原因的更多相关文章
- angular中ng-include失效的原因
使得angular的ng-include指令失效的原因有两个: 例如:在demo.html中的代码<div ng-include = "'demo1.html'">&l ...
- 关于动态生成dom绑定事件失效的原因
之前做项目都是直接用jquery的bind绑定事件,不过当时都不是动态生成dom元素,而是已经页面中原本存在的dom元素进行事件绑定,最近在测试给动态生成的dom绑定事件的时候发现事件失效,于是就测试 ...
- 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法
参考原贴地址:https://blog.csdn.net/clementad/article/details/47339519 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Trans ...
- Android开发(十六)——Android listview onItemClick事件失效的原因
参考: Android listview onItemClick事件失效的原因.http://blog.csdn.net/wangchun8926/article/details/8793178
- Spring事务失效的原因
http://blog.csdn.net/paincupid/article/details/51822599 Spring事务失效的原因 5种大的原因 如使用mysql且引擎是MyISAM,则事务会 ...
- 【转】在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法
参考 原文链接 @Transactional does not work on method level 描述 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational) ...
- @Transational)的方法,注解失效的原因和解决方法
在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解是不会生效的. 比如,下面代码例子中,有两方法,一个有@Transational注解,一个没有.如果 ...
- 全网最深分析SpringBoot MVC自动配置失效的原因
前言 本来没有计划这一篇文章的,只是在看完SpringBoot核心原理后,突然想到之前开发中遇到的MVC自动失效的问题,虽然网上有很多文章以及官方文档都说明了原因,但还是想亲自看一看,本以为很简单的事 ...
- 百度地图API 与 jquery 同时使用时报 TypeError $(...) is null错误 失效的原因及解决办法
在引用百度地图API后,发现jquery 根据id 找不到 form.但是对于别的控件没有问题. 在排除了 html加载的问题后. 上网查找 发现以下解决办法: 原因应该是有冲突的插件. 解决办法将 ...
随机推荐
- PowerShell调用jira rest api实现jira统计自动化
通过调用JIRA Rest web api实现统计自动化,首先进行登录模拟: $content = @{username='用户名';password='密码'} $JSON=$content|con ...
- java 抽象类和接口的区别
抽象类 特点: 1.抽象类中可以构造方法 2.抽象类中可以存在普通属性,方法,静态属性和方法. 3.抽象类中可以存在抽象方法. 4.如果一个类中有一个抽象方法,那么当前类一定是抽象类:抽象类中不一定有 ...
- JS中的call()和apply()方法和bind()
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- 2016年11月2日 星期三 --出埃及记 Exodus 19:18
2016年11月2日 星期三 --出埃及记 Exodus 19:18 Mount Sinai was covered with smoke, because the LORD descended on ...
- spark1.4
spark1.4 Windows local调试环境搭建总结 1.scala版本 scala-2.10.4 官方推荐 scala-2.11.7[不推荐,非sbt项目.需要后加载] 2.spark版本 ...
- sql 流水号
CREATE TABLE [dbo].[SriaNum] ( [Num] [int] NOT NULL) alter PROC dpIDS_GetSerialNumber@SerialNumber V ...
- qt 3 获取文件路径中的一部分
QList<QString> qlist = path.split(QRegExp("[\\\\/]")); QString FileName = qlist.at(q ...
- BZOJ 2879 美食节(费用流-动态加边)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2879 题意:有n道菜,每道菜需要b[i]份,m个厨师,第j个厨师做第i道菜需要时间a[i ...
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...
- weblogic解密工具
import org.bouncycastle.jce.provider.BouncyCastleProvider; import sun.misc.BASE64Decoder; import jav ...