View事件分发机制

View事件分发机制_第1张图片

进入Actvity的
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
可以看到会直接调用
public boolean onTouchEvent(MotionEvent event) {
if (mWindow.shouldCloseOnTouch(this, event)) {
finish();
return true;
}
return false;
}
而Activity中的TouchEvent,如果窗口关闭就返回true,自己消费事件,一般情况下return false;
分发到一下层级的dispatchTouchEvent();
ViewGroup中的dispatchTouchEvent();
首先会做拦截判断,看ViewGroup是否拦截
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;
}
会扫描子View看是否有能处理事件的子View如果有,分发下去(这都是默认处理),

你可能感兴趣的:(View事件分发机制)