当我们点击一个button的时候,会触发这么一系列的事件
// 分发 Activity.dispatchTouchEvent -> ViewGroup.dispatchTouchEvent -> ViewGroup.onInterceptTouchEvent -> View.dispatchTouchEvent -> // 消费 View.onTouch(setOnTouchListener) -> View.onTouchEvent -> View.onLongClick ->(由onTouchEvent 中触发,ACTION_DOWN超过一定时间就触发) View.onClick(由onTouchEvent 中触发,ACTION_UP)
Activity.dispatchTouchEvent -> ViewGroup.dispatchTouchEvent
Activity.dispatchTouchEvent -> ViewGroup.dispatchTouchEvent -> Activity.onTouchEvent(多层嵌套的viewgroup时,会ViewGroup.onTouchEvent)
关于onTouchEvent
view.dispatchTouchEvent源码
public boolean dispatchTouchEvent(MotionEvent event) { ... boolean result = false; ... //noinspection SimplifiableIfStatement ListenerInfo li = mListenerInfo; if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && li.mOnTouchListener.onTouch(this, event)) { result = true; } if (!result && onTouchEvent(event)) { result = true; } } ... return result; }
通过源码可以验证mOnTouchListener.onTouch(this, event)返回true时,事件将被消费,不再触发onTouchEvent
view.onTouchEvent源码
public boolean onTouchEvent(MotionEvent event) { ... if (mTouchDelegate != null) { if (mTouchDelegate.onTouchEvent(event)) { return true; } } ... if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) { switch (action) { case MotionEvent.ACTION_UP: ... if (mPerformClick == null) { // 触发 onClickListener mPerformClick = new PerformClick(); } if (!post(mPerformClick)) { performClickInternal(); } ... break; case MotionEvent.ACTION_DOWN: ... if (!clickable) { // 检查是否长按事件,是的话将触发onLongClick事件 checkForLongClick( ViewConfiguration.getLongPressTimeout(), x, y, TOUCH_GESTURE_CLASSIFIED__CLASSIFICATION__LONG_PRESS); break; } ... break; ... return false; }