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

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

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


理解之前

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

源码探索

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

  1. public boolean dispatchTouchEvent(MotionEvent ev) {
  2. if (ev.getAction() == MotionEvent.ACTION_DOWN) {
  3. onUserInteraction();
  4. }
  5. if (getWindow().superDispatchTouchEvent(ev)) {
  6. return true;
  7. }
  8. return onTouchEvent(ev);
  9. }

在这个方法中,传递了一个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进行分发。

  1. @Override
  2. public boolean dispatchTouchEvent(MotionEvent ev) {
  3. boolean handled = false;
  4. if (onFilterTouchEventForSecurity(ev)) {
  5. final int action = ev.getAction();
  6. final int actionMasked = action & MotionEvent.ACTION_MASK;
  7. // Handle an initial down.
  8. if (actionMasked == MotionEvent.ACTION_DOWN) {
  9. // Throw away all previous state when starting a new touch gesture.
  10. // The framework may have dropped the up or cancel event for the previous gesture
  11. // due to an app switch, ANR, or some other state change.
  12. cancelAndClearTouchTargets(ev);
  13. resetTouchState();
  14. }
  15. // Check for interception.
  16. final boolean intercepted;
  17. if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null) {
  18. final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
  19. if (!disallowIntercept) {
  20. intercepted = onInterceptTouchEvent(ev);
  21. ev.setAction(action);
  22. // restore action in case it was changed
  23. } else {
  24. intercepted = false;
  25. }
  26. }
  27. else {
  28. // There are no touch targets and this action is not an initial down
  29. // so this view group continues to intercept touches.
  30. intercepted = true;
  31. }

看上面的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。

  1. if (!canceled && !intercepted) {
  2. // If the event is targeting accessiiblity focus we give it to the
  3. // view that has accessibility focus and if it does not handle it
  4. // we clear the flag and dispatch the event to all children as usual.
  5. // We are looking up the accessibility focused host to avoid keeping
  6. // state since these events are very rare.
  7. View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() ? findChildWithAccessibilityFocus() : null;
  8. if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
  9. final int actionIndex = ev.getActionIndex();
  10. // always 0 for down
  11. final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) : TouchTarget.ALL_POINTER_IDS;
  12. // Clean up earlier touch targets for this pointer id in case they
  13. // have become out of sync.
  14. removePointersFromTouchTargets(idBitsToAssign);
  15. final int childrenCount = mChildrenCount;
  16. if (newTouchTarget == null && childrenCount != 0) {
  17. final float x = ev.getX(actionIndex);
  18. final float y = ev.getY(actionIndex);
  19. // Find a child that can receive the event.
  20. // Scan children from front to back.
  21. final ArrayList<View> preorderedList =buildOrderedChildList();
  22. final boolean customOrder = preorderedList == null && isChildrenDrawingOrderEnabled();
  23. final View[] children = mChildren;
  24. for (int i = childrenCount - 1; i >= 0; i--) {
  25. final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
  26. final View child = (preorderedList == null) ? children[childIndex] : preorderedList.get(childIndex);
  27. // If there is a view that has accessibility focus we want it
  28. // to get the event first and if not handled we will perform
  29. // normal dispatch. We may do a double iteration but this is
  30. // safer given the timeframe.
  31. if (childWithAccessibilityFocus != null) {
  32. if (childWithAccessibilityFocus != child) {
  33. continue;
  34. }
  35. childWithAccessibilityFocus = null;
  36. i = childrenCount - 1;
  37. }
  38. if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) {
  39. ev.setTargetAccessibilityFocus(false);
  40. continue;
  41. }
  42. newTouchTarget = getTouchTarget(child);
  43. if (newTouchTarget != null) {
  44. // Child is already receiving touch within its bounds.
  45. // Give it the new pointer in addition to the ones it is handling.
  46. newTouchTarget.pointerIdBits |= idBitsToAssign;
  47. break;
  48. }
  49. resetCancelNextUpFlag(child);
  50. if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
  51. // Child wants to receive touch within its bounds.
  52. mLastTouchDownTime = ev.getDownTime();
  53. if (preorderedList != null) {
  54. // childIndex points into presorted list, find original index
  55. for (int j = 0; j < childrenCount; j++) {
  56. if (children[childIndex] == mChildren[j]) {
  57. mLastTouchDownIndex = j;
  58. break;
  59. }
  60. }
  61. } else {
  62. mLastTouchDownIndex = childIndex;
  63. }
  64. mLastTouchDownX = ev.getX();
  65. mLastTouchDownY = ev.getY();
  66. newTouchTarget = addTouchTarget(child, idBitsToAssign);
  67. alreadyDispatchedToNewTouchTarget = true;
  68. break;
  69. }
  70. // The accessibility focus didn't handle the event, so clear
  71. // the flag and do a normal dispatch to all children.
  72. ev.setTargetAccessibilityFocus(false);
  73. }
  74. if (preorderedList != null) preorderedList.clear();
  75. }
  76. if (newTouchTarget == null && mFirstTouchTarget != null) {
  77. // Did not find a child to receive the event.
  78. // Assign the pointer to the least recently added target.
  79. newTouchTarget = mFirstTouchTarget;
  80. while (newTouchTarget.next != null) {
  81. newTouchTarget = newTouchTarget.next;
  82. }
  83. newTouchTarget.pointerIdBits |= idBitsToAssign;
  84. }
  85. }}

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

  1. private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, View child, int desiredPointerIdBits) {
  2. final boolean handled;
  3. // Canceling motions is a special case. We don't need to perform any transformations
  4. // or filtering. The important part is the action, not the contents.
  5. final int oldAction = event.getAction();
  6. if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
  7. event.setAction(MotionEvent.ACTION_CANCEL);
  8. if (child == null) {
  9. handled = super.dispatchTouchEvent(event);
  10. } else {
  11. handled = child.dispatchTouchEvent(event);
  12. }
  13. event.setAction(oldAction);
  14. return handled;
  15. }

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

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

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

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

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

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

  1. // Dispatch to touch targets, excluding the new touch target if we already
  2. // dispatched to it.Cancel touch targets if necessary.
  3. TouchTarget predecessor = null;
  4. TouchTarget target = mFirstTouchTarget;
  5. while (target != null) {
  6. final TouchTarget next = target.next;
  7. if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
  8. handled = true;
  9. } else {
  10. final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted;
  11. if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)) {
  12. handled = true;
  13. }
  14. if (cancelChild) {
  15. if (predecessor == null) {
  16. mFirstTouchTarget = next;
  17. } else {
  18. predecessor.next = next;
  19. }
  20. target.recycle();
  21. target = next;
  22. continue;
  23. }
  24. }
  25. predecessor = target;
  26. target = next;
  27. }}

首先我们要知道的是,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. 支付宝(alipay)即时到账收款接口开发中的那些事儿

    不会做,看看也可以会,要做好就还是需要多学习 国庆回来就一直没状态,看完<银河护卫队>,印象最深的竟然是只有两句台词的呆萌groot,昨天才休息一天,大耍大吃,今天还是把昨天的知识学习一下 ...

  2. 一个复杂Json的解析

    { "website": { "1": { "basic": { "homepage": "http://py ...

  3. [JZOJ5837] Omeed

    Description Solution 有两种做法 一种是线段树维护一次方程系数,一种是线段树维护矩阵 准备都写一写 维护系数 首先把式子推出来 \[CS=B\times \sum\limits_{ ...

  4. 常用的NoSQL数据库类型简述

    一.文档存储类型(Document Stores) 文档存储,也称为面向文档的数据库系统,其主要特点在于它们的无模式的数据组织. 特点: 1.记录数据不需要具有统一的结构,即不同的记录可以具有不同的列 ...

  5. 使用 New Relic 监控接口服务性能 (APM)

    偶然看到贴子在使用[Rails API] 使用这个APM监控,今天试了下.NET IIS环境下,配置一路NEXT即可. 主要指标 服务响应时间 Segment SQL执行时间 安全问题 1.走HTTP ...

  6. .net 服务端 访问共享文件夹

    共享文件夹所在电脑为A服务器,网站部署在B服务器 A,B服务器上拥有同名账户,且密码也要相同.如账户名share,密码123. A服务器上,共享文件夹设置share账户有读写权限 B服务器上,IIS中 ...

  7. vb.net 发Mail

    Private Sub A1() '创建发件连接,根据你的发送邮箱的SMTP设置填充 Dim a As System.Net.Mail.Attachment Dim smtp As New Syste ...

  8. webstrom vue项目让局域网访问

    vue项目package.json "dev": "webpack-dev-server --inline --progress --config build/webpa ...

  9. Oracle总结一

    1 数据库相关概念 1.1 数据 数据是描述事物的符号,它有多种表现形式:文本,图形,音频,视频.计算机处理数据的基本单位是字节. 1.2 数据库(Database, 简称DB) 同粮库,车库类似,数 ...

  10. github上值得关注的前端项目【转】

    今天突然看到了这些资源,所以就转载过来了,虽然是2015年的,但是可以看一下 综合/资源 frontend-dev-bookmarks 一个巨大的前端开发资源清单.star:15000 front-e ...