继上篇Android 事件分发机制源码攻略(二) —— ViewGroup篇的介绍后,我们知道事件如何从Activity的dispatchTouchEvent经由顶层ViewDecorView 再到ViewGroup的dispatchTouchEvent,ViewGroup层的分发,我个人觉得是整个事件分发最为关键的一部分,理解透了ViewGroup层的事件传递,相当于对整个事件分发传递也就差不多了。现在事件传递到View层,这一篇,我们将对分析事件在View层的dispatchTouchEvent、onTouchEvent、OnTouchListener、OnClickListener这些方法的传递顺序。首先,我们来看下dispatchTouchEvent这个方法。
/**
* Pass the touch screen motion event down to the target view, or this
* view if it is the target.
*
* @param event The motion event to be dispatched.
* @return True if the event was handled by the view, false otherwise.
*/
public boolean dispatchTouchEvent(MotionEvent event) {
// If the event should be handled by accessibility focus first.
if (event.isTargetAccessibilityFocus()) {
// We don't have focus or no virtual descendant has it, do not handle the event.
if (!isAccessibilityFocusedViewOrHost()) {
return false;
}
// We have focus and got the event, then use normal event dispatch.
event.setTargetAccessibilityFocus(false);
}
boolean result = false;
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(event, 0);
}
final int actionMasked = event.getActionMasked();
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Defensive cleanup for new gesture
stopNestedScroll();
}
//跟ViewGroup层的一样,都是安全策略
if (onFilterTouchEventForSecurity(event)) {
//鼠标拖拉处理,不是分发的重点
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//noinspection SimplifiableIfStatement
//内部类,包含各种触摸监听
ListenerInfo li = mListenerInfo;
//优先判断mOnTouchListener是否为空,为空的话就跳过,不为空的话,就走到onTouchEvent方法
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
//result为false的情况
if (!result && onTouchEvent(event)) {
result = true;
}
}
if (!result && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
}
// Clean up after nested scrolls if this is the end of a gesture;
// also cancel it if we tried an ACTION_DOWN but we didn't want the rest
// of the gesture.
if (actionMasked == MotionEvent.ACTION_UP ||
actionMasked == MotionEvent.ACTION_CANCEL ||
(actionMasked == MotionEvent.ACTION_DOWN && !result)) {
stopNestedScroll();
}
return result;
}
View的dispatchTouchEvent方法相对简单,没有复杂的逻辑。整个方法最为关键的地方是从第41行开始,先对OnTouchListener这个监听进行判断
。
如果给这个View设置了OnTouchListener监听并且返回true,那result就直接为true了,这直接导致事件没有进到onTouchEvent方法就结束分发了。如果没有设置OnTouchListener监听,那就会进到onTouchEvent方法,然后View的dispatchTouchEvent方法到此也就结束了。接着,我们再来看看onTouchEvent()方法。
/**
* Implement this method to handle touch screen motion events.
* <p>
* If this method is used to detect click actions, it is recommended that
* the actions be performed by implementing and calling
* {@link #performClick()}. This will ensure consistent system behavior,
* including:
* <ul>
* <li>obeying click sound preferences
* <li>dispatching OnClickListener calls
* <li>handling {@link AccessibilityNodeInfo#ACTION_CLICK ACTION_CLICK} when
* accessibility features are enabled
* </ul>
*
* @param event The motion event.
* @return True if the event was handled, false otherwise.
*/
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y