事件顺序
正常的情况下,一次事件点击会触发一系列点击事件。
- 点击屏幕后立即松开,事件顺序是DOWN->UP
- 点击屏幕一会后再松开,事件顺序是DOWN->MOVE->…->MOVE->UP
事件分发的对象
- Activity:控制生命周期、处理事件
- ViewGroup:一组View的集合 (还有多个View)
- View:所有UI组件的基类
事件分发主要方法
- dispatchTouchEvent:用来进行事件分发
- onInterceptTouchEvent:判断是否拦截事件 (只存在于ViewGroup中)
- onTouchEvent:处理点击事件
Activity的事件分发
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//空方法,子类可重写
onUserInteraction();
}
//getWindow是PhoneWindow对象
//经过PhoneWIndow->DecorView,最终调用ViewGroup.dispatchTouchEvent()
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
ViewGroup的事件分发
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = false;
if (actionMasked == MotionEvent.ACTION_DOWN) {
...
// 1.进行初始化
// 对mFirstTouchTarget进行了制空操作
cancelAndClearTouchTargets(ev);
// mGroupFlags标志位进行了清除
resetTouchState();
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
// 2.mGroupFlags & FLAG_DISALLOW_INTERCEPT 得到 disallowIntercept
// 该值用来禁止或允许ViewGroup拦截除了DOWN之外的事件
// 一般由子类来调用requestDisallowInterceptTouchEvent来赋值给mGroupFlags
// 此处因为前面mGroupFlags进行了清除,所以disallowIntercept一定为false
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
//3.调用onInterceptTouchEvent
//onInterceptTouchEvent默认返回false,表示默认情况下不会去拦截
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
intercepted = true;
}
}
...
if (!canceled && !intercepted) { //如果不拦截
for (int i = childrenCount - 1; i >= 0; i--) {
//完成ViewGroup到子View的事件传递
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
//对mFirstTouchTarget进行了赋值
newTouchTarget = addTouchTarget(child, idBitsToAssign);
//将alreadyDispatchedToNewTouchTarget置为true
alreadyDispatchedToNewTouchTarget = true;
break;
}
}
}
if (mFirstTouchTarget == null) { //如果mFirstTouchTarget为null,表示没有子View要消费事件
// No touch targets so treat this as an ordinary view.
///将第三个参数传为NULL,事件交由自己处理
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
//这是是直接从内存缓存中遍历,TouchTarget 是一个链表结构的缓存池
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
//DOWN事件
//将handled设为true
handled = true;
} else {
//DOWN之后的后续事件的处理
if (dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)) {
handled = true;
}
...
}
}
}
}
可以看到1处,如果是ACTION_DOWN事件,首先会进行初始化,对mGroupFlags标志位进行清除。
接着,2处获取disallowIntercept,该值子类通过requestDisallowInterceptTouchEvent可以进行设置。但是由于ACTION_DOWN事件mGroupFlags标志位已被清除,所以这里永远都都会执行3
onInterceptTouchEvent默认返回false,表示默认情况下不会去拦截
如果不拦截,会遍历View,调用dispatchTransformedTouchEvent完成ViewGroup到子View的事件传递。
接着如果拦截或没有子View要消费事件,则调用dispatchTransformedTouchEvent并第三个参数传null,表示事件交由自己处理
伪代码
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean consume = false;
//调用onInterceptTouchEvent判断是否拦截
if(onInterceptTouchEvent(ev)){
//拦截则调用自身的onTouchEvent
consume = onTouchEvent(ev);
} else {
//不拦截,将事件分发给子View
consume = child.dispatchTouchEvent(ev)
}
}
View的事件分发
从dispatchTouchEvent开始
public boolean dispatchTouchEvent(MotionEvent event) {
...
boolean result = false;
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(event, 0);
}
...
if (onFilterTouchEventForSecurity(event)) {
...
ListenerInfo li = mListenerInfo;
//如果设置了OnTouchListener,会优先执行OnTouchListener
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;
}
可以看到如果设置了OnTouchListener,会优先执行OnTouchListener。
如果没有设置,才会去执行onTouchEvent
再来看onTouchEvent
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
final int viewFlags = mViewFlags;
final int action = event.getAction();
//只有一个为true,clickable就为true
final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;
//disable依然消费事件 (直接返回true)
if ((viewFlags & ENABLED_MASK) == DISABLED) {
...
return clickable;
}
...
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
//如果clickable为true,会执行到这里
switch (action) {
case MotionEvent.ACTION_UP:
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
if ((viewFlags & TOOLTIP) == TOOLTIP) {
handleTooltipUp();
}
if (!clickable) {
removeTapCallback();
removeLongPressCallback();
mInContextButtonPress = false;
mHasPerformedLongPress = false;
mIgnoreNextUpEvent = false;
break;
}
//是否有PFLAG_PRESSED或PFLAG_PREPRESSED标记
boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (prepressed) {
setPressed(true, x, y);
}
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
// This is a tap, so remove the longpress check
//如果没有处理长按事件,那么会把长按事件remove掉
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
//调用mOnClickListener.onClick
performClick();
}
}
}
if (mUnsetPressedState == null) {
mUnsetPressedState = new UnsetPressedState();
}
if (prepressed) {
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
} else if (!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();
}
removeTapCallback();
}
mIgnoreNextUpEvent = false;
break;
case MotionEvent.ACTION_DOWN:
...
//mHasPerformedLongPress:表示是否处理了长按
mHasPerformedLongPress = false;
...
//是否在一个可滑动的容器里
boolean isInScrollingContainer = isInScrollingContainer();
if (isInScrollingContainer) {
mPrivateFlags |= PFLAG_PREPRESSED;
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
mPendingCheckForTap.x = event.getX();
mPendingCheckForTap.y = event.getY();
//如果是一个可滑动的容器,那么延迟100毫秒后,设置Press状态,并再延迟400毫秒后检查长按事件
//如果总共500毫秒后,该Runnable没有被取消掉,那么会执行mOnLongClickListener.onLongClick,并根据onLongClick结果赋值给mHasPerformedLongPress
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
} else {
// 不是在一个滚动的容器中,所以立即执行
setPressed(true, x, y);
checkForLongClick(0, x, y);
}
break;
case MotionEvent.ACTION_CANCEL:
...
break;
case MotionEvent.ACTION_MOVE:
...
break;
}
return true;
}
return false;
}
可以看到,在ACTION_DOWN中,如果是一个可以滑动的容器,那么等待100毫秒后,设置PRESS状态,并再等待400毫秒,调用onLongClickListener,并根据onLongClickListener的返回值,赋值给mHasPerformedLongPress。
在ACTION_UP中,如果没有处理长按事件或OnLongClickListener返回false(mHasPerformedLongPress被赋值为false),那么会把长按事件remove掉。接着会调用OnClickListener方法。
onLongClickListener和OnClickListener可以被同时执行,只要onLongClickListener返回false,此时,mHasPerformedLongPress为false,还会去调用OnClickListener
事件分发结论
- 一个事件序列从手指接触屏幕到手指离开屏幕,在这个过程中产生一系列事件,以DOWN事件开始,中间含有不定数的MOVE事件,以UP事件结束。
- 正常情况下,一个事件序列只能被一个View拦截并消耗。
- 某个View一旦决定拦截,那个这个事件序列都将由它的onTouchEvent处理,并且它的onInterceptTouchEvent不会再调用。
- 某个View一旦开始处理事件,如果它不消耗ACTION_DOWN事件(onTouchEvent返回false),那么同一序列中其他事件都不再交由它处理。并且重新交由它的父元素处理 (父元素onTouchEvent被调用)
- 事件的传递过程是由外向内的,即事件总是先传递给父元素,然后再由父元素分发给子View,通过requestDisallowInterceptTouchEvent方法可以在子View中干预父元素的事件分发过程,但ACtiON_DOWN除外。
- ViewGroup默认不拦截任何事件,即onInterceptTouchEvent默认返回false。View没有onInterceptTouchEvent方法,一旦有点击事件传递给它,那么它的onTouchEvent方法就会被调用。
- View的onTouchEvent默认会消耗事件 (默认true),除非它是不可点击的(clickable和longclickable同时为false)。View的longClickable默认都为false,clickable要分情况,比如Button的clickable默认为true,TextView的clickable默认为false。