3.4 Viewのイベント配信メカニズム(二)

4071 ワード

ViewGroupのイベント配布疑似コード
public boolean dispatchTouchEvent(MotionEvent ev) {
    /*
    ACTION_DOWNA     
                 DOWN  , UP  ,
         DOWN  ,             ,            。
    */
    if (action == ACTIONN_DOWN) {
        mFrstTouchTarge = null;
        FLAG_DISALLOW_INTERCEPT=false;
    }

    //       
    boolean intercepted;
    if (action == ACTIONN_DOWN || mFrstTouchTarge != null) {
        // FLAG_DISALLOW_INTERCEPT     View requestDisallowInterceptTouchEvent   。
        if (!FLAG_DISALLOW_INTERCEPT) {
            // ViewGroup        ,                ,      onInterceptTouchEvent()   ,                 。
            intercepted = onInterceptTouchEvent(event);
        }else {
            intercepted=true;
        }
    }else{
        intercepted = true;
    }

    TouchTarget newTouchTarget = null;  // 1
    boolean alreadyDispatchedToNewTouchTarget = false;
    if (!canceled && !intercepted) {
        
        if (actionMasked == MotionEvent.ACTION_DOWN
                || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
                || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
            // ACTION_DOWN 
            final int childrenCount = mChildrenCount;
            if (newTouchTarget == null && childrenCount != 0) {
                //   View
                final View[] children = mChildren;
                for (int i = childrenCount - 1; i >= 0; i--) { 
                    
                    if (!canViewReceivePointerEvents(child)                
                            || !isTransformedTouchPointInView(x, y, child, null)) {  
                        //              Veiw      ,  continue
                        continue;
                    }
                    //          View    
                    newTouchTarget = getTouchTarget(child);
                    
                    //       View
                    if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {  
                        newTouchTarget = addTouchTarget(child, idBitsToAssign);
                        //       View      true,   View      ,alreadyDispatchedToNewTouchTarget  true
                        alreadyDispatchedToNewTouchTarget = true;
                        break;
                    }
                }      
            }
        }
    }

    //        
    if (mFirstTouchTarget == null) {
        //        View,  dispatchTransformedTouchEvent  ,      null,   super     ,   viewgroup    
        handled = dispatchTransformedTouchEvent(ev, canceled, null,
                TouchTarget.ALL_POINTER_IDS);
    } else {
        //      View
        TouchTarget target = mFirstTouchTarget;
        while (target != null) {
            if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
                //  Veiw      ,     true。   Down,               。
                handled = true;
            } else {
                //       Down      ,     View  
                if (dispatchTransformedTouchEvent(ev, cancelChild,
                        target.child, target.pointerIdBits)) {
                    handled = true;
                }
            }
        }
    }

}
//   View     
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
            View child, int desiredPointerIdBits) {
    final boolean handled;
    if (child == null) {
        //    View, super,   View     (View       viewgroup   ,      ,   onTouchEvent  )
        handled = super.dispatchTouchEvent(event);
    } else {
        //   View,  View     ,   View View  ViewGroup
        handled = child.dispatchTouchEvent(event);
    }
    return handled;
}

//  mFirstTouchTarget  ,       
private TouchTarget addTouchTarget(View child, int pointerIdBits) {
    TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
    target.next = mFirstTouchTarget;
    mFirstTouchTarget = target;
    return target;
}

//      ,    false,         
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    return false; 
}