http://www.jianshu.com/p/34cb396104a7

有些无奈,期末考试抱佛脚,还好没有挂,现在继续进阶。

好久以前就看到了View的事件分发,但是当时功底不够,源码也不敢深究,也就是个模模糊糊过了,现在在看一面,才发现以前许多理解都是错的,也怪不得当时自己都没有真正弄清楚。


理解之前

首先我们应该明白的是,当我们一个触摸事件来的时候,它是被包装成的一个MotionEvent,其中就包含了这个事件是 downmoveup其中的一种,还有这个触摸发生的地点(也就是坐标)等等。
其次,我们还需要知道的是,每一次的触摸事件都是最先把MotionEvent发送到ActivitydispatchTouchEvent方法中的。
有这两点基础,我们就可以去探索源码了。

源码探索

既然我们现在已经知道了,一个触摸事件最先就是包装成一个MotionEvent给发送到ActivitydispatchTounchEvent了,那么我们当然从这个方法看起走呀。

public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}

在这个方法中,传递了一个MotionEvent作为参数,也就是我们的触摸事件传递给了这个方法。然后进行了一点简单的逻辑,首先判断一下MotionEvent是否为down,如果是的话就调用 onUserInteraction()。而onUserInteraction()就是一个空方法,目的就是实现这个方法,可以更加方便管理一些notfication

public void onUserInteraction() { }
所以和我们的事件分发并没有很大的关系,重要的是下面的几句。
这里调用了Activity所对应的WindowsuperDispatchTouchEvent(ev)方法来进行事件的分发。然后我们接着寻找这个方法,在Window这个抽象类中发现了这个抽象方法superDispatchTouchEvent(ev),有这个方法明我们也可以看出来,这里是调用的Window的实现类的方法啦。
于是我们就可以找到这个Window的唯一实现类PhoneWindow,在这个类中,我们找到了superDispatchTouchEvent(ev)方法。在这个方法中,也是相当的简单,就直接调用了mDecor.superDispatchTouchEvent,也就是这句话,我们的事件终于传到了View了。对,这里的mDecor就是我们ActivitysetContent中所设置的View的父容器,也就是顶级容器了。

看到了这里,才真正的开始进行View的事件分发了,不过再之前,还是先理一下,以便后面好理解。

  1. MotionEvent现在是传到Activity的顶级View的,我们的事件分发就是从这个顶级View向它的子View进行分发的。
  2. 顶级View所包含的子View,子View中又包含子View,形成一个View树。
  3. 事件分发就是把事件(MotionEvent) 按照先序遍历所有节点,直到找到一个View消费掉这个事件。所谓的消费这个事件,就是相应的ViewOntouchListener返回true或者OntouchEvent()返回为true
  4. 事件分发主要由三个函数控制,分别是dispatchTouchEvent分发事件,onInterceptTouchEvent拦截事件,onTouchEvent响应事件。

View的事件分发.png

深入分发

事件传到顶级View(ViewGroup)中时,就会调用dispatchTouchEvent进行分发。

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = false;
if (onFilterTouchEventForSecurity(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();
}
// 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;
}

看上面的dispatchTouchEvent的逻辑也是很好理解的,首先会判断我们传来的TouchEvent是不是down,如果是的话,就会调用resetTouchSate方法,不过现在我们暂时不需要知道这个方法的具体作用,但是从方法名中我们也能得到一些提示,也就是每当遇到down就会重新设置一些状态。
然后,这里就会判断是否需要调用onInterceptTouchEvent方法,也就是注释中的 Check for interception。值得注意的是这里是两层判断,也就是有两个嵌套的if
在第一个if中,会确定触摸事件是否为downmFirstTouchTarget是不是为空。其中mFirstTouchTarget表示的是事件是不是又子View消费了的,如果已经被消费,就不会为null。在第二个if中就会判断是否设置了FLAG_DISALLOW_INTERCEPT这个 标记符,这个FLAG_DISALLOW_INTERCEPT标记符的作用就是子View干涉父容器对事件的分发。如果子View设置了这个标记符,就不会调用onInterceptTouchEvent方法,从而intercepted为false。

如果两层if都满足,就会调用onInterceptTouchEvent来对事件进行拦截。

接下来,我们就看看如果父容器不拦截,即intercepted为false。

if (!canceled && !intercepted) {
// If the event is targeting accessiiblity focus we give it to the
// view that has accessibility focus and if it does not handle it
// we clear the flag and dispatch the event to all children as usual.
// We are looking up the accessibility focused host to avoid keeping
// state since these events are very rare.
View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() ? findChildWithAccessibilityFocus() : null;
if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
final int actionIndex = ev.getActionIndex();
// always 0 for down
final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) : TouchTarget.ALL_POINTER_IDS;
// Clean up earlier touch targets for this pointer id in case they
// have become out of sync.
removePointersFromTouchTargets(idBitsToAssign);
final int childrenCount = mChildrenCount;
if (newTouchTarget == null && childrenCount != 0) {
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);
// Find a child that can receive the event.
// Scan children from front to back.
final ArrayList<View> preorderedList =buildOrderedChildList();
final boolean customOrder = preorderedList == null && isChildrenDrawingOrderEnabled();
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
final View child = (preorderedList == null) ? children[childIndex] : preorderedList.get(childIndex);
// If there is a view that has accessibility focus we want it
// to get the event first and if not handled we will perform
// normal dispatch. We may do a double iteration but this is
// safer given the timeframe.
if (childWithAccessibilityFocus != null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus = null;
i = childrenCount - 1;
}
if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
newTouchTarget = getTouchTarget(child);
if (newTouchTarget != null) {
// Child is already receiving touch within its bounds.
// Give it the new pointer in addition to the ones it is handling.
newTouchTarget.pointerIdBits |= idBitsToAssign;
break;
}
resetCancelNextUpFlag(child);
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
// The accessibility focus didn't handle the event, so clear
// the flag and do a normal dispatch to all children.
ev.setTargetAccessibilityFocus(false);
}
if (preorderedList != null) preorderedList.clear();
}
if (newTouchTarget == null && mFirstTouchTarget != null) {
// Did not find a child to receive the event.
// Assign the pointer to the least recently added target.
newTouchTarget = mFirstTouchTarget;
while (newTouchTarget.next != null) {
newTouchTarget = newTouchTarget.next;
}
newTouchTarget.pointerIdBits |= idBitsToAssign;
}
}}

代码有点多,不过抓重点看的话也就那几行。
这里主要是有一个for循环,对子View进行了遍历,然后判断是否能够接受触摸事件,可以接受的话就会调用dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)MotionEvent给传给子View,这个方法的返回值就是表示是否消费了该事件,也就是OnTouchListener或者OntouchEvent是否返回了true

private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,        View child, int desiredPointerIdBits) {
final boolean handled;
// Canceling motions is a special case. We don't need to perform any transformations
// or filtering. The important part is the action, not the contents.
final int oldAction = event.getAction();
if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
event.setAction(MotionEvent.ACTION_CANCEL);
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
return handled;
}

可以看到,这里dispatchTransformedTouchEvent就会让子View重复父容器类似的分发方式。

如果有子View消费的话就会跳出for循环,并且在addTouchTarget(child, idBitsToAssign);方法中给前面所说的mFirstTouchTarget赋值。

要是没有View消费该事件或者父容器拦截该事件的话,

// Dispatch to touch targets.
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS);
}

可以看到,会调用一个和上面一样的方法,只是参数不同而已,因为第三个参数传的是null,所以就会调用super.dispatchTouchEvent(event)方法,这里需要注意的是,这里的super不是父容器,而是指的是本身ViewGroup的父类View的方法,其对象还是这个 ViewGroup

接着我们再考虑一种情况,当我们的触摸事件不为downmFirstTouchTarget != null的话,就会直接在我们TouchTarget中分发了,也就是 mFirstTouchTarget所保存中进行分发。

// Dispatch to touch targets, excluding the new touch target if we already
// dispatched to it.Cancel touch targets if necessary.
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled = true;
} else {
final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted;
if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)) {
handled = true;
}
if (cancelChild) {
if (predecessor == null) {
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}}

首先我们要知道的是,TouchTarget是一种单链表结构,保存了每一次我们不拦截所分发的View,所以满足上述情况的时候,就会遍历这个链表进行分发。

上面的所有基本上就是View的事件分发了,当然,当一MotionEventViewGroup传到了View的时候,对应的就相当简单了,因为View并没有子View,而单纯的是对于MotionEvent事件的消费----OntouchListenerOnTouchEvent的返回值而已,不过值得注意的是OntouchListener的优先级比OnTouchEvent,这点从源码中很轻松就能发现。

总结

View的事件分发 (1).png

最后,还是通过这一张相同的图进行总结一下。

触摸事件最初是由Activity传给Window再传到顶级View mDercorView中,也就是这里的树根,然后按照前序遍历,把触摸事件向下传。当事件传到了ViewGroup1的时候,就会遍历它下面的三个子View,当这三个子View都没有消费这个事件的时候,就会调用ViewGruop1的父类View去试着消费这个事件,要是还是没有被消费,则ViewGroup2就会重复ViewGroup1,当然,如果ViewGroup2也没消费掉事件(包括它的子View),ViewGroup3还是会继续重复。要是这三个ViewGroup都没有消费掉的话,则又会传到ViewGroup0的父View去试着消费,如果也没有消费掉,最后就会传到Activity中进行消费。

文/MathiasLuo(简书作者)
原文链接:http://www.jianshu.com/p/34cb396104a7
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

Android的进阶学习(六)--理解View事件分发的更多相关文章

  1. Android View事件分发-从源码分析

    View事件分发-从源码分析 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/details/78555397?lo ...

  2. Android高手进阶——Adapter深入理解与优化

    Android高手进阶--Adapter深入理解与优化 通常是针对包括多个元素的View,如ListView,GridView.ExpandableListview,的时候我们是给其设置一个Adapt ...

  3. Android面试必问!View 事件分发机制,看这一篇就够了!

    在 Android 开发当中,View 的事件分发机制是一块很重要的知识.不仅在开发当中经常需要用到,面试的时候也经常被问到. 如果你在面试的时候,能把这块讲清楚,对于校招生或者实习生来说,算是一块不 ...

  4. Android View框架总结(七)View事件分发机制

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52282833 View布局告一段落,从本篇开始View事件相关分析, ...

  5. View 事件分发

    View 事件分发 学习自 <Android开发艺术探索> 官方文档-MotionEvent 事件分发机制漫谈 View的事件分发机制,使我们了解View的工作原理继而学习如何自定义Vie ...

  6. Atitit View事件分发机制

    1. Atitit View事件分发机制 1. Atitit View事件分发机制1 1.1. 三个关键方法 dispatchTouchEvent onInterceptTouchEvent onTo ...

  7. 【Android面试查漏补缺】之事件分发机制详解

    前言 查漏补缺,查漏补缺,你不知道哪里漏了,怎么补缺呢?本文属于[Android面试查漏补缺]系列文章第一篇,持续更新中,感兴趣的朋友可以[关注+收藏]哦~ 本系列文章是对自己的前段时间面试经历的总结 ...

  8. 谈谈我对Android View事件分发的理解

    写这篇博客的缘由.近期因为项目中用到相似一个LinearLayout中水平布局中,有一个TextView和Button,然后对该LinearLayout布局设置点击事件.点击TextView能够触发该 ...

  9. Android View 事件分发机制详解

    想必很多android开发者都遇到过手势冲突的情况,我们一般都是通过内部拦截和外部拦截法解决此类问题.要想搞明白原理就必须了解View的分发机制.在此之前我们先来了解一下以下三个非常重要的方法: di ...

随机推荐

  1. python的Web框架,中间件middleware及djangoAdmin

    简介 用于处理request和response的中间处理的函数,可以创建在项目中的任意位置,只要可以导入即可. 建议创建在APP目录下,方便管理. 函数范式与激活 中间件的范式: # 必须接受get_ ...

  2. npm包

    https://www.cnblogs.com/xinxingyu/p/5736244.html     node - glob模块讲解 https://github.com/isaacs/node- ...

  3. Oracle入门《Oracle介绍》第一章1-4 Oracle 用户管理

    1.Oracle 默认用户 只有用合法的用户帐号才能访问Oracle数据库 Oracle 有几个默认的数据库用户 数据库中所有数据字典表和视图都存储在 SYS 模式中.SYS用户主要用来维护系统信息和 ...

  4. 两个有序数组长度分别为m,n,最多m+n次查找找出相同的数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Jetbrains软件永久破解

    说明:该破解到期时间为2099年,基本为永久破解! 1.点击右侧链接下载脚本JetbrainsCrack-3.1-release-enc.jar [JetbrainsCrack-release-enc ...

  6. Java基础——Servlet(七)过滤器&监听器 相关

    一.过滤器简介 Filter 位于客户端和请求资源之间,请求的资源可以是 Servlet Jsp html (img,javascript,css)等.用于拦截浏览器发给服务器的请求(Request) ...

  7. 快速排序 java详解

    1.快速排序简介: 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此 ...

  8. UdPloyer交付系统设计思路

    宏观愿景: 一键搞定项目依赖环境,将软件交付过程管理化,实现DevOps研发测试运维一体化. 一.一站式版本交付生命周期管理  业务线[私有权限] 1.SVN源码交付 合主干.版本归档.拉分支.版本回 ...

  9. Flask的Context(上下文)学习笔记

    上下文是一种属性的有序序列,为驻留在环境内的对象定义环境.在对象的激活过程中创建上下文,对象被配置为要求某些自动服务,如同步.事务.实时激活.安全性等等. 比如在计算机中,相对于进程而言,上下文就是进 ...

  10. Python十讲 - 第一讲:从零开始学Python

    之后慢慢添加... Python语言的背景知识