View的事件分发机制
事件分发机制图解
由上图可看出所涉及到了三个重要的方法
dispatchTouchEvent(MotionEvent ev):用于事件的分发
onIerveptTouchEvent(MotionEvent ev):用来进行事件的拦截,
在dispatchTouchEvent()中进行调用,且View没有该方法,只有ViewGroup有。onTouchEvent(MotionEvent ev):用来处理点击事件,
在dispatchTouchEvent()中进行调用。
事件分发
:当我们点击屏幕时,就产生了点击事件,该事件被封装成了一个MotionEvent类。系统会将MotionEvent传递给View的层级,MotionEvent在View中的层级传递过程就是事件分发。
View的事件分发机制
当点击事件产生后,事件会首先传递给Activity,Activity会调用dispatchEvent(),将事件分发给phoneWindow,phoneWindow再分发给DecorView,最后由DecorView分发给根ViewGroup.
进入ViewGroup的dispatchTouchEvent
1 |
|
当ViewGroup要拦截事件的时候,后续事件全部交由它处理,而不用再调用onInterceptTouchEvent()进行判断,因此onInterceptTouchEvent()并不是每次事件都会执行。
1 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
onInterceptTouchEvent()默认返回false,不进行拦截,可在ViewGroup中重写该方法进行拦截。
查看上述转换触控分发事件dispatchTransformedTouchEvent():
1 | private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, |
如果viewGroup有子View,则调用子View的dispatchTouchEvent(event),没有则调用super.dispatchTouchEvent(event),因为ViewGroup是继承自View,进入View的dispatchTouchEvent(event)
1 | public boolean dispatchTouchEvent(MotionEvent event) { |
如果OnTouchListener不为null且onTouch()返回为true,则表示事件被消费,不会执行onTouchEvent(event),否则就会执行onTouchEvent(event).根据执行顺序可看出OnTouchListener.onTouch()优先级>onTouchEvent(event).下面进入omTouchEvent()。
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
final int viewFlags = mViewFlags;
final int action = event.getAction();
final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;
if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
// A disabled view that is clickable still consumes the touch
// events, it just doesn't respond to them.
return clickable;
}
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
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;
}
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) {
// The button is being released before we actually
// showed it as pressed. Make it show the pressed
// state now (before scheduling the click) to ensure
// the user sees it.
setPressed(true, x, y);
}
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
// This is a tap, so remove the longpress check
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)) {
performClick();
}
}
}
...
}
return true;
}
return false;
}
只要View的CLICKABLE和LONG_CLICKABLE有一个为true,那么onTouchEvent()就会返回true消费这个事件。只要设置View设置了对应的监听事件setOnClickListener和setOnLongClickListener来设置,其会自动将对应属性设置为true。进入至performClick()。
public boolean performClick() {
final boolean result;
final ListenerInfo li = mListenerInfo;
if (li != null && li.mOnClickListener != null) {//如果view设置了onClickListener,则就会执行其onClick(),
playSoundEffect(SoundEffectConstants.CLICK);
li.mOnClickListener.onClick(this);
result = true;
} else {
result = false;
}
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
notifyEnterOrExitForAutoFillIfNeeded(true);
return result;
}
点击事件分发的传递原则
根ViewGroupdispatchTouchEvent()遍历子View询问是否允许拦截,不允许则将事件分发给子View的dispatchTouchEvent(),允许则进入onInterceptTouchEvent(event)判断自己是否要拦截这个事件,不拦截则将事件分发给子View的dispatchTouchEvent(),拦截则进入自己的onTouchEvent()进行处理。当传递到子View,如果其onTouchEvent返回true表明其处理了该事件,返回false,会将事件传递给父View进行处理。