探究requestDisallowInterceptTouchEvent失效的原因

昨天在用requestDisallowInterceptTouchEvent的时候,发现在设置了requestDisallowInterceptTouchEvent(true)之后,父View的onInterceptTouchEvent方法照样执行。怎么会这样呢?只能通过查看源码来一探究竟了。

首先看下requestDisallowInterceptTouchEvent方法的源码,在android.view.ViewGroup类中:

[java] view plaincopy
 
  1. public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {  
  2.   
  3.     if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {  
  4.         // We're already in this state, assume our ancestors are too  
  5.         return;  
  6.     }  
  7.   
  8.     if (disallowIntercept) {  
  9.         mGroupFlags |= FLAG_DISALLOW_INTERCEPT;  
  10.     } else {  
  11.         mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;  
  12.     }  
  13.   
  14.     // Pass it up to our parent  
  15.     if (mParent != null) {  
  16.         mParent.requestDisallowInterceptTouchEvent(disallowIntercept);  
  17.     }  
  18. }  

这里看到,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方法的源代码:

[java] view plaincopy
 
  1. /** 
  2.  * Resets all touch state in preparation for a new cycle. 
  3.  */  
  4. private void resetTouchState() {  
  5.     clearTouchTargets();  
  6.     resetCancelNextUpFlag(this);  
  7.     mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;  
  8.     mNestedScrollAxes = SCROLL_AXIS_NONE;  
  9. }  

这里看到,resetTouchState方法会把ViewGroup的FLAG_DISALLOW_INTERCEPT置为false。

再来看下涉及到读操作的dispatchTouchEvent方法的源代码:

[java] view plaincopy
 
  1. @Override  
  2.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  3.   
  4.             ......  
  5.   
  6.             // Check for interception.  
  7.             final boolean intercepted;  
  8.             if (actionMasked == MotionEvent.ACTION_DOWN  
  9.                     || mFirstTouchTarget != null) {  
  10.                 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;  
  11.                 if (!disallowIntercept) {  
  12.                     intercepted = onInterceptTouchEvent(ev);  
  13.                     ev.setAction(action); // restore action in case it was changed  
  14.                 } else {  
  15.                     intercepted = false;  
  16.                 }  
  17.             } else {  
  18.                 // There are no touch targets and this action is not an initial down  
  19.                 // so this view group continues to intercept touches.  
  20.                 intercepted = true;  
  21.             }  
  22.   
  23.             ......  
  24.     }  

这里就是整个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的调用关系,发现只有一个方法调用了:

[java] view plaincopy
 
  1. @Override  
  2.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  3.   
  4.             ......  
  5.   
  6.             final int action = ev.getAction();  
  7.             final int actionMasked = action & MotionEvent.ACTION_MASK;  
  8.   
  9.             // Handle an initial down.  
  10.             if (actionMasked == MotionEvent.ACTION_DOWN) {  
  11.                 // Throw away all previous state when starting a new touch gesture.  
  12.                 // The framework may have dropped the up or cancel event for the previous gesture  
  13.                 // due to an app switch, ANR, or some other state change.  
  14.                 cancelAndClearTouchTargets(ev);  
  15.                 resetTouchState();  
  16.             }  
  17.   
  18.             ......    
  19.   
  20.             // Update list of touch targets for pointer up or cancel, if needed.  
  21.             if (canceled  
  22.                     || actionMasked == MotionEvent.ACTION_UP  
  23.                     || actionMasked == MotionEvent.) {  
  24.                 resetTouchState();  
  25.             } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {  
  26.                 final int actionIndex = ev.getActionIndex();  
  27.                 final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);  
  28.                 removePointersFromTouchTargets(idBitsToRemove);  
  29.             }  
  30.   
  31.             ......  
  32.     }  

看完代码就发现,原来在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)方法重置。

原文地址:https://www.cnblogs.com/Free-Thinker/p/4800832.html