#ViewGroup#dispatchTouchEvent摘要
// 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(); //每一次down重置mGroupFlags
}
// Check for interception.
final boolean intercepted;
//mFirstTouchTarget是child.dispatchTouchEvent返回ture时赋值为child。
if (actionMasked == MotionEvent.ACTION_DOWN|| mFirstTouchTarget != null) {
//可以利用child.getParent.requestDisallowInterceptTouchEvent设置
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev); //重写此方法可以自行判断是否拦截
ev.setAction(action); // restore action in case it was changed
} else {
//设置之后ViewGroup不再拦截事件。
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;
}
从代码中可以看到TouchEvent拦截的一些规则:
1 VIew没有办法通过requestDisallowIntercepTouchEvent阻止View'Group拦截Down事件,因为Donw事件会重置mGroupFlags
2 假如在Down事件,所有child.dispatchTouchEvent均返回false。那么接下来的所有事件,ViewGroup将会直接拦截。
3 只有在Down事件,VIewGroup.onInterceptTouchEvent返回false,child.dispatchTouchEvent返回true。接下来的事件才会继续判断哪一个拦截,child可以通过requestDisallowInterceptTouchEvent阻止ViewGroup拦截事件,优先级比VIewGroup.onInterceptTouchEvent高