标签:
理解事件的分发机制,需要对View和ViewGroup事件的分发分别探讨。View和ViewGroup的区别,一个View控件是指它里面不能再包含子控件了,常见的如TextView、Button、ImageView等,而ViewGroup是继承自View的,但是它里面可以包含一些子控件,包括View或者嵌套的ViewGroup,常用的大部分布局都是ViewGroup组件,如LinearLayout、RelativeLayout、FrameLayout等。
首先要明白的是,当我们触摸一个控件时(不论是View还是ViewGroup),都会调用dispatchTouchEvent()方法,开始事件的分发处理。我们先自定义一个简单的线性布局:
public class MyLinearLayout extends LinearLayout { public MyLayout(Context context, AttributeSet attrs) { super(context, attrs); } }
<com.scu.viewtouch.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" android:background="#808080" android:text="点击View" /> </com.scu.viewtouch.MyLinearLayout>
运行后,我们点击Button控件,当事件传递到Button时会调用Button的dispatchTouchEvent方法(Button本身并没有dispatchTouchEvent方法,往上最终寻找到其父类View的dispatchTouchEvent方法)。根据dispatchTouchEvent源码来分析其处理流程:
public boolean dispatchTouchEvent(MotionEvent event) { if (<span style="color:#ff0000;">mOnTouchListener != null</span> && (mViewFlags & ENABLED_MASK) == ENABLED && <span style="color:#ff0000;">mOnTouchListener.onTouch(this, event)</span>) { //第一步 return true; } return onTouchEvent(event); //第二步 }第一步:首先进行三个条件的判断:
如果条件都满足,则该事件被消耗掉,不再进入onTouchEvent中处理。
第二步:上述三个条件不同时满足时,事件将交给onTouchEvent方法处理。再根据onTouchEvent源码分析其处理流程:
public boolean onTouchEvent(MotionEvent event) { final int viewFlags = mViewFlags; if ((viewFlags & ENABLED_MASK) == DISABLED) { // A disabled view that is clickable still consumes the touch // events, it just doesn't respond to them. return (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)); } if (mTouchDelegate != null) { if (mTouchDelegate.onTouchEvent(event)) { return true; } } if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {//第一点 switch (event.getAction()) { case MotionEvent.ACTION_UP: boolean prepressed = (mPrivateFlags & PREPRESSED) != 0; if ((mPrivateFlags & 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 (!mHasPerformedLongPress) { // 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(); //第二点 } } } if (mUnsetPressedState == null) { mUnsetPressedState = new UnsetPressedState(); } if (prepressed) { mPrivateFlags |= PRESSED; refreshDrawableState(); postDelayed(mUnsetPressedState, ViewConfiguration.getPressedStateDuration()); } else if (!post(mUnsetPressedState)) { // If the post failed, unpress right now mUnsetPressedState.run(); } removeTapCallback(); } break; case MotionEvent.ACTION_DOWN: if (mPendingCheckForTap == null) { mPendingCheckForTap = new CheckForTap(); } mPrivateFlags |= PREPRESSED; mHasPerformedLongPress = false; postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); break; case MotionEvent.ACTION_CANCEL: mPrivateFlags &= ~PRESSED; refreshDrawableState(); removeTapCallback(); break; case MotionEvent.ACTION_MOVE: final int x = (int) event.getX(); final int y = (int) event.getY(); // Be lenient about moving outside of buttons int slop = mTouchSlop; if ((x < 0 - slop) || (x >= getWidth() + slop) || (y < 0 - slop) || (y >= getHeight() + slop)) { // Outside button removeTapCallback(); if ((mPrivateFlags & PRESSED) != 0) { // Remove any future long press/tap checks removeLongPressCallback(); // Need to switch from pressed to not pressed mPrivateFlags &= ~PRESSED; refreshDrawableState(); } } break; } return true; } return false; }源码很长,我们只需关注重要的几点即可。
第一点:这里有一个长长的if语句
if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE))用于判断该View是否是可点击的或是否可长按的View,明显我们的Button属于可点击的View控件。进入到if里面后转入到switch中,当执行完switch语句后,直接执行
return true;呵呵,说明什么?只要是该控件是可点击的或者可长按的View,这个事件就会被消耗掉!这也符合我们的认知,按钮Button之类的不就是让人来点击处理的么,但是对于那些TextView、ImageView之类的非可点击控件,我们平常不是也能够处理点击事件吗?回忆一下,我们在处理这些点击事件的时候,一定通过setOnClickListener()给它设置了点击监听OnClickListener(或者在布局中声明了android:clickable="true"),setOnClickListener源码如下:
public void setOnClickListener (OnClickListener l) { if (!isClickable()) { setClickable( true); } mOnClickListener = l; }可知,设置了点击事件或长按事件的控件自动变成了CLICKABLE 或LONG_CLICKABLE的状态;
第二点:我们设置的onclick事件是在手指抬起ACTION_UP的时候执行的。
到这,我们可以总结一下,View事件的处理流程。对于View来说,事件首先进入到dispatchTouchEvent方法中进行分发处理,在dispatchTouchEvent中首先查看该View是否设置了OnTouchListener事件并且实现的监听中的onTouch方法的返回值是否为true,如果满足,这个事件就到此被消耗,不再往下处理;如果条件不满足,则进入到onTouchEvent方法中进行处理,在onTouchEvent方法中,先检查该View是否是可点击或长按的(设置监听、布局中设置android:clickable),如果是则该事件被消耗。
可以看出,onTouch()方法的执行优先于onTouchEvent(),onTouch()方法的返回值决定了能否执行到onTouchEvent()方法,dispatchTouchEvent()方法的返回值,依赖于OnTouchListener的onTouch()方法或者onTouchEvent()方法。
继续以我们上面那个自定义布局为例,当我们点击button时,事件其实是先到我们自定义的MyLinearLayout中分发的,同样,首先进入MyLinearLayout的dispatchTouchEvent(LinearLayout中本身也没有dispatchTouchEvent,最终找到其父类ViewGroup的dispatchTouchEvent)方法中进行分发,根据dispatchTouchEvent源码进行分析:
public boolean dispatchTouchEvent(MotionEvent ev) { final int action = ev.getAction(); final float xf = ev.getX(); final float yf = ev.getY(); final float scrolledXFloat = xf + mScrollX; final float scrolledYFloat = yf + mScrollY; final Rect frame = mTempRect; //这个值默认是false, 然后我们可以通过requestDisallowInterceptTouchEvent(boolean disallowIntercept)方法 //来改变disallowIntercept的值 boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; //这里是ACTION_DOWN的处理逻辑 if (action == MotionEvent.ACTION_DOWN) { //清除mMotionTarget, 每次ACTION_DOWN都很设置mMotionTarget为null if (mMotionTarget != null) { mMotionTarget = null; } //disallowIntercept默认是false, 就看ViewGroup的onInterceptTouchEvent()方法 if (disallowIntercept || !onInterceptTouchEvent(ev)) { //第一点 ev.setAction(MotionEvent.ACTION_DOWN); final int scrolledXInt = (int) scrolledXFloat; final int scrolledYInt = (int) scrolledYFloat; final View[] children = mChildren; final int count = mChildrenCount; //遍历其子View for (int i = count - 1; i >= 0; i--) { //第二点 final View child = children[i]; //如果该子View是VISIBLE或者该子View正在执行动画, 表示该View才 //可以接受到Touch事件 if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { //获取子View的位置范围 child.getHitRect(frame); //如Touch到屏幕上的点在该子View上面 if (frame.contains(scrolledXInt, scrolledYInt)) { // offset the event to the view's coordinate system final float xc = scrolledXFloat - child.mLeft; final float yc = scrolledYFloat - child.mTop; ev.setLocation(xc, yc); child.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; //调用该子View的dispatchTouchEvent()方法 if (child.dispatchTouchEvent(ev)) { // 如果child.dispatchTouchEvent(ev)返回true表示 //该事件被消费了,设置mMotionTarget为该子View mMotionTarget = child; //直接返回true return true; } // The event didn't get handled, try the next view. // Don't reset the event's location, it's not // necessary here. } } } } } //判断是否为ACTION_UP或者ACTION_CANCEL boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || (action == MotionEvent.ACTION_CANCEL); if (isUpOrCancel) { //如果是ACTION_UP或者ACTION_CANCEL, 将disallowIntercept设置为默认的false //假如我们调用了requestDisallowInterceptTouchEvent()方法来设置disallowIntercept为true //当我们抬起手指或者取消Touch事件的时候要将disallowIntercept重置为false //所以说上面的disallowIntercept默认在我们每次ACTION_DOWN的时候都是false mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; } // The event wasn't an ACTION_DOWN, dispatch it to our target if // we have one. final View target = mMotionTarget; //mMotionTarget为null意味着没有找到消费Touch事件的View, 所以我们需要调用ViewGroup父类的 //dispatchTouchEvent()方法,也就是View的dispatchTouchEvent()方法 if (target == null) { // We don't have a target, this means we're handling the // event as a regular view. ev.setLocation(xf, yf); if ((mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { ev.setAction(MotionEvent.ACTION_CANCEL); mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; } return super.dispatchTouchEvent(ev); } //这个if里面的代码ACTION_DOWN不会执行,只有ACTION_MOVE //ACTION_UP才会走到这里, 假如在ACTION_MOVE或者ACTION_UP拦截的 //Touch事件, 将ACTION_CANCEL派发给target,然后直接返回true //表示消费了此Touch事件 if (!disallowIntercept && onInterceptTouchEvent(ev)) { final float xc = scrolledXFloat - (float) target.mLeft; final float yc = scrolledYFloat - (float) target.mTop; mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; ev.setAction(MotionEvent.ACTION_CANCEL); ev.setLocation(xc, yc); if (!target.dispatchTouchEvent(ev)) { } // clear the target mMotionTarget = null; // Don't dispatch this event to our own view, because we already // saw it when intercepting; we just want to give the following // event to the normal onTouchEvent(). return true; } if (isUpOrCancel) { mMotionTarget = null; } // finally offset the event to the target's coordinate system and // dispatch the event. final float xc = scrolledXFloat - (float) target.mLeft; final float yc = scrolledYFloat - (float) target.mTop; ev.setLocation(xc, yc); if ((target.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { ev.setAction(MotionEvent.ACTION_CANCEL); target.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; mMotionTarget = null; } //如果没有拦截ACTION_MOVE, ACTION_DOWN的话,直接将Touch事件派发给target return target.dispatchTouchEvent(ev); }ViewGroup的dispatchTouchEvent方法很长,主要看两点:
第一点:首先通过onInterceptTouchEvent方法判断该ViewGroup是否进行拦截该事件,默认该方法返回false,即不进行拦截,将事件分发给它的子View或子ViewGroup。很多时候,我们在自定义一些复杂控件时,我们可以重写该方法,根据情况灵活处理其返回值。
第二点:
1、当onInterceptTouchEvent返回false不进行拦截时,if判断条件成立,进入到if内,开始遍历该ViewGroup的子ViewGroup或子View,将事件分发给子ViewGroup或子View的dispatchTouchEvent方法,在分发过程中如果中间的ViewGroup没有进行拦截,Touch事件就会一直往下分发到手指按下的最里面的View,这个时候,就会按照View事件的分发处理过程调用View的dispatchTouchEvent方法了,从而交给onTouchEvent方法进行处理,
(1)如果onTouchEvent返回true,也即该View的dispatchTouchEvent返回true,表示消耗掉了此事件,事件也就终止传递。
(2)如果onTouchEvent返回false,即不消费Touch事件,这个Touch事件就会向上找父布局调用其父布局的onTouchEvent,让父布局处理。
2、当onInterceptTouchEvent返回true的时候,表示该ViewGroup需要进行拦截事件,此事件就交给该ViewGroup自己来处理,从而调用该ViewGroup的onTouchEvent方法。
可以看出,ViewGroup的事件传递实际上是分为两步的:事件分发和事件处理,首先是Touch事件的分发,先从从顶层的View一直往下分发到手指按下的最里面的View,到了这里之后,判断该View是否进行事件的处理(或消耗),如果不处理(false),就反过来一层一层往上交给父布局处理,如果消耗(true),就不会再交给父布局,事件终止。流程图如下:
(图转自某位大神,http://blog.csdn.net/dmk877/article/details/49055815)
到这里,我们总结一下,View和ViewGroup的事件分发和处理的总流程,当我们手指触摸屏幕时,事件会传入到我们编写的布局文件的根布局上,如上面我们自定义的MyLinearLayout中,寻找MyLinearLayout的dispatchTouchEvent方法进行事件分发,由于LinearLayout中本身没有该方法,就往上寻找到其父类ViewGroup的dispatchTouchEvent方法,往下分发前查看该ViewGroup的onInterceptTouchEvent方法判断是否需要拦截掉该事件,如果不拦截遍历其子ViewGroup或子View,直到碰到该往下分发过程中被某个ViewGroup拦截掉,或者最后分发到手指按下的最里面的View,然后按照View的事件处理流程处理该事件。在ViewGroup事件分发过程中,会根据子View或者ViewGroup的dispatchTouchEvent方法的返回值决定是否继续遍历分发下去。
好了,到这里事件分发的理论是差不多了,后面最重要的是多看看一些大神的自定义控件,慢慢在实战中灵活掌握事件分发的机制。同时结合Android View和ViewGroup的绘制流程,融会贯通。
Android事件分发参考文章:
http://blog.csdn.net/guolin_blog/article/details/9097463
http://blog.csdn.net/guolin_blog/article/details/9153747
http://blog.csdn.net/xiaanming/article/details/21696315
http://blog.csdn.net/dmk877/article/details/48781845
http://blog.csdn.net/dmk877/article/details/49055815
标签:
原文地址:http://blog.csdn.net/shakespeare001/article/details/50991947