1、View 只有消費(fèi)了 ACTION_DOWN 事件,才能接收到后續(xù)的事件(可點(diǎn)擊控件會默認(rèn)消費(fèi)所有事件)凿蒜,并且會將后續(xù)所有事件傳遞過來哥艇,不會再傳遞給其他 View绝编,除非上層 View 進(jìn)行了攔截。
結(jié)論:如果在自定義View中重寫了onTouchEvent()貌踏,且case:MotionEvent.ACTION_DOWN中返回了false十饥,則onClick()無法生效,因?yàn)閛nClick在MotionEvent.ACTION_UP中祖乳。
2逗堵、parent.requestDisallowInterceptTouchEvent()決定了是否進(jìn)入ViewGroup.onInterceptTouchEvent()方法。
代碼解釋:
boolean ViewGroup.dispatchTouchEvent(){
// Handle an initial down. 僅對于ACTION_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(); // 重置mGroupFlags對FLAG_DISALLOW_INTERCEPT的狀態(tài)凡资,可以理解成把disallowIntercept重置為false->intercepted = false;
}
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; // disallowIntercept默認(rèn)false
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false; // disallowIntercept = false->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;
}
// 如果攔截了砸捏。則mFirstTouchTarget == null,直接進(jìn)入ViewGroup.onTouchEvent()處理事件階段
if (!canceled && !intercepted) {
// 只有down和move事件
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE){
/**
* 如果View不消費(fèi) ACTION_DOWN 事件(onTouchEvent()的ACTION_DOWN返回false)隙赁,則dispatchTransformedTouchEvent()返回false垦藏,
* 則mFirstTouchTarget = null,后續(xù)事件的dispatchTransformedTouchEvent()方法的child參數(shù)為null伞访,不會將事件傳給子View
**/
if (dispatchTransformedTouchEvent()){
// addTouchTarget()中給mFirstTouchTarget賦值
newTouchTarget = addTouchTarget(child, idBitsToAssign);
}
}
}
// 如果攔截了掂骏。則mFirstTouchTarget == null,直接進(jìn)入ViewGroup.onTouchEvent()處理事件階段
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
// child參數(shù)為null厚掷,相當(dāng)于ViewGroup.onTouchEvent()
handled = dispatchTransformedTouchEvent(ev, canceled, child:null,
TouchTarget.ALL_POINTER_IDS);
} else {
if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)) {
handled = true;
}
}
return handled;
}
---------------------ViewGroup.dispatchTransformedTouchEvent---------------------------------------
boolean ViewGroup.dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, child, int desiredPointerIdBits) {
// 沒有 child弟灼,將當(dāng)前 ViewGroup 當(dāng)作普通的 View 處理级解。
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent); //即ViewGroup.onTouchEvent() -> View.onTouchEvent()
}
return handled;
}
------------------View.dispatchTouchEvent----------------------------
public boolean dispatchTouchEvent(MotionEvent event) {
if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED // 注意ENABLED標(biāo)志
&& mOnTouchListener.onTouch(this, event)){ //處理
return true;
}
return onTouchEvent(event)); //處理
}
-------------源碼補(bǔ)充ViewGroup.requestDisallowInterceptTouchEvent-------
@Override
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) {
// 0|0=0 0|1=1
// true,則mGroupFlags被FLAG_DISALLOW_INTERCEPT置位田绑。
mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
} else {
// 0&0=0 0&1=0 1&0=0 1&1=1
// 先取反(優(yōu)先級高)勤哗,再與。
// 重置mGroupFlags對FLAG_DISALLOW_INTERCEPT的狀態(tài)掩驱,可以理解成把disallowIntercept重置為false
// 即resetTouchState()
mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
}
// Pass it up to our parent
if (mParent != null) {
mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
}
}