viewgroup的事件分發(fā)

viewgroup的事件分發(fā)

  • 我們看下測試代碼
package com.example.administrator.myapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

/**
 * Created by Administrator on 2015/11/29.
 */
public class MyRealLayout extends RelativeLayout {
    public MyRealLayout(Context context) {
        super(context);
    }
    public MyRealLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyRealLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                Log.e("MyRealLayout", "dispatchTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("MyRealLayout", "dispatchTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.e("MyRealLayout", "dispatchTouchEvent ACTION_UP");
                break;

            default:
                break;
        }
        boolean b = super.dispatchTouchEvent(ev);
        Log.e("MyRealLayout", "dispatchTouchEvent==="+b);
        return b;
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                Log.e("MyRealLayout", "onInterceptTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("MyRealLayout", "onInterceptTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.e("MyRealLayout", "onInterceptTouchEvent ACTION_UP");
                break;
            default:
                break;
        }
        boolean b = super.onInterceptTouchEvent(ev);
        Log.e("MyRealLayout", "onInterceptTouchEvent==="+b);
        return b;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                Log.e("MyRealLayout", "onTouchEvent ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("MyRealLayout", "onTouchEvent ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.e("MyRealLayout", "onTouchEvent ACTION_UP");
                break;
            default:
                break;
        }
        boolean b = super.onTouchEvent(event);
        Log.e("MyRealLayout", "onTouchEvent==="+b);
        return b;
    }
    @Override
    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept)
    {
        Log.e("MyRealLayout", "requestDisallowInterceptTouchEvent ");
        super.requestDisallowInterceptTouchEvent(disallowIntercept);
    }
}


button的點擊事件的代碼和上篇一樣,我們看下log

        12-03 10:37:24.876 12965-12965/? E/MyRealLayout: dispatchTouchEvent ACTION_DOWN
        12-03 10:37:24.876 12965-12965/? E/MyRealLayout: onInterceptTouchEvent ACTION_DOWN
        12-03 10:37:24.876 12965-12965/? E/MyRealLayout: onInterceptTouchEvent===false
        12-03 10:37:24.876 12965-12965/? E/MyButton: dispatchTouchEvent ACTION_DOWN
        12-03 10:37:24.876 12965-12965/? E/mBtEvent: onTouch ACTION_DOWN
        12-03 10:37:24.876 12965-12965/? E/MyButton: onTouchEvent ACTION_DOWN
        12-03 10:37:24.877 12965-12965/? E/MyButton: onTouchEvent===true
        12-03 10:37:24.877 12965-12965/? E/MyButton: dispatchTouchEvent===true
        12-03 10:37:24.877 12965-12965/? E/MyRealLayout: dispatchTouchEvent===true
        12-03 10:37:24.923 12965-12965/? E/MyRealLayout: dispatchTouchEvent ACTION_UP
        12-03 10:37:24.923 12965-12965/? E/MyRealLayout: onInterceptTouchEvent ACTION_UP
        12-03 10:37:24.923 12965-12965/? E/MyRealLayout: onInterceptTouchEvent===false
        12-03 10:37:24.923 12965-12965/? E/MyButton: dispatchTouchEvent ACTION_UP
        12-03 10:37:24.923 12965-12965/? E/mBtEvent: onTouch ACTION_UP
        12-03 10:37:24.923 12965-12965/? E/MyButton: onTouchEvent ACTION_UP
        12-03 10:37:24.924 12965-12965/? E/MyButton: onTouchEvent===true
        12-03 10:37:24.924 12965-12965/? E/MyButton: dispatchTouchEvent===true
        12-03 10:37:24.924 12965-12965/? E/MyRealLayout: dispatchTouchEvent===true

從上面的日志我們可以看出基本的流程是:

MyRealLayout: dispatchTouchEvent ACTION_DOWN=====MyRealLayout: onInterceptTouchEvent ACTION_DOWN==MyButton: dispatchTouchEvent ACTION_DOWN====MyButton: onTouchEvent ACTION_DOWN======
基本就是事件是從父VIEW分發(fā)到子viewd的

然后我們進入源碼查看:

  • ViewGroup - dispatchTouchEvent - ACTION_DOWN
 @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!onFilterTouchEventForSecurity(ev)) {
            return false;
        }
        //獲取當前的動作
        final int action = ev.getAction();
        final float xf = ev.getX();
        final float yf = ev.getY();
        //獲取view的正確的坐標
        final float scrolledXFloat = xf + mScrollX;
        final float scrolledYFloat = yf + mScrollY;
        final Rect frame = mTempRect;
        //判斷是否設(shè)置不攔截的標志
        boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
        //將mMotionTarget置為null
        if (action == MotionEvent.ACTION_DOWN) {
            if (mMotionTarget != null) {
                // this is weird, we got a pen down, but we thought it was
                // already down!
                // XXX: We should probably send an ACTION_UP to the current
                // target.``
                mMotionTarget = null;
            }   
            //disallowIntercept=true就表示不允許攔截,進入,disallowIntercept 可以通過viewGroup.requestDisallowInterceptTouchEvent(boolean);進行設(shè)置是否攔截而onInterceptTouchEvent(ev)我們可以復(fù)寫自己控制返回值!
            // intercept
            if (disallowIntercept || !onInterceptTouchEvent(ev)) {
                // reset this event's action (just to protect ourselves)
                ev.setAction(MotionEvent.ACTION_DOWN);
                // We know we want to dispatch the event down, find a child
                // who can handle it, start with the front-most child.
                final int scrolledXInt = (int) scrolledXFloat;
                final int scrolledYInt = (int) scrolledYFloat;
                final View[] children = mChildren;
                final int count = mChildrenCount;
                //開始遍歷子view注意這個遍歷是倒著遍歷的因為最后添加的view肯定在最上面!child.dispatchTouchEvent(ev)
                for (int i = count - 1; i >= 0; i--) {
                    final View child = children[i];
                    if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE
                            || child.getAnimation() != null) {
                        child.getHitRect(frame);
                        //進行判斷當前點擊的坐標是否包含子view如果包含這個子view就調(diào)用
                        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的事件分發(fā)流程了
                            if (child.dispatchTouchEvent(ev))  {
                                // Event handled, we have a target now.
                                mMotionTarget = child;
                                return true;
                            }
                            // The event didn't get handled, try the next view.
                            // Don't reset the event's location, it's not
                            // necessary here.
                        }
                    }
                }
            }
        }                                                                                                                          //down中做的事情就是判斷是否需要攔截事件,如果不需要攔截就遍歷字view執(zhí)行view的分發(fā)

  • ViewGroup - dispatchTouchEvent - ACTION_UP
         public boolean dispatchTouchEvent(MotionEvent ev) {
                if (!onFilterTouchEventForSecurity(ev)) {
                    return false;
                }

                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;

                boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;

                if (action == MotionEvent.ACTION_DOWN) {...}

            boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) ||
                        (action == MotionEvent.ACTION_CANCEL);
            //判讀當前的事件是否等于up或者ACTION_CANCEL
            if (isUpOrCancel) {
                    mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
                }
            final View target = mMotionTarget;
            if(target ==null ){...}
           //判斷當前的view是否攔截
            if (!disallowIntercept && onInterceptTouchEvent(ev)) {...}

                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);

                return target.dispatchTouchEvent(ev);
            }
   up的時候主要是重置一些屬性。然后在調(diào)用dispatchTouchEvent

總結(jié)下

  • dowun的時候ViewGroup捕獲到事件去判斷是否需要攔截事件,如果不需要攔截事件,就去遍歷子view,倒著遍歷然后循環(huán)的判斷當前的點擊區(qū)域是否包含當前的view,包含就會執(zhí)行view的dispatchTouchEvent進行View的事件分發(fā)mMotionTarget就是childen的賦值
  • up的時候ViewGroup捕獲到事件去判斷需不需要攔截事件,然后清空標記,調(diào)用view的dispatchTouchEvent進行View的事件分發(fā)
  • movew的時候ViewGroup捕獲到事件,然后判斷是否攔截,如果沒有攔截,則直接調(diào)用mMotionTarget.dispatchTouchEvent(ev)
  • 分發(fā)之前的坐標都會經(jīng)過修正之后傳遞給View當前的x,y分別減去child.left 和 child.top
  • 就是為什么ViewGrou的onTouchEvent不會執(zhí)行因為button設(shè)置了Ontouch,他一定會返回true,所以在dispatchTouchEvent就不會調(diào)用onTouchEvent

關(guān)于攔截事件

  • 如果你需要攔截,只要return true就行了,這要該事件就不會往子View傳遞了,并且如果你在DOWN retrun true ,則DOWN,MOVE,UP子View都不會捕獲事件;如果你在MOVE return true , 則子View在MOVE和UP都不會捕獲事件。
  • 原因很簡單,當onInterceptTouchEvent(ev) return true的時候,會把mMotionTarget 置為null ;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容