关闭

ViewGroup的OnInterceptTouchEvent方法的调用特点

标签: AndroidViewGroupTouchEvent
432人阅读 评论(0) 收藏 举报
分类:

ViewGroup的OnInterceptTouchEvent方法的调用特点

  • 当ViewGroup的childView的onTouchEvent方法返回true时表示该childView可以接受MotionEvent事件, 此时在ViewGroup上的有MotionEvent时间发生时,每次都会调用OnInterceptTouchEvent方法。
  • 当ViewGroup的childView的onTouchEvent方法返回false时表示该childView不接受MotionEvent事件, 如果ViewGroup的onTouchEvent方法返回true表示ViewGroup可以接受MotionEvent方法,此后onInterceptTouchEvent方法不会再调用,直接调用ViewGroup 的onTouchEvent方法。

    一个MotionEvent事件由一个ACTION_DOWN事件,若干个ACTION_MOVE事件,一个ACTION_UP事件组成。当一个MotionEvent事件发生时,ViewGroup接到MotionEvent的上述的事件后,ViewGroup调用自己的dispatchTouchEvent方法来向其childView以及自身来分配MotionEvent的事件。来看ViewGroup的dispatchTouchEvent方法:

public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
        }

        boolean handled = false;
        if (onFilterTouchEventForSecurity(ev)) {
            final int action = ev.getAction();
            final int actionMasked = action & MotionEvent.ACTION_MASK;

            // Handle an initial down.
            /**ACTION_DOWN表示新的事件到来,取消并且清楚之前保留的TouchTarget,并且重置touchState,
            *TouchTarget表示接受MotionEvent的childView。
            **/
            if (actionMasked == MotionEvent.ACTION_DOWN) {
                // Throw away all previous state when starting a new touch gesture.
                // The framework may have dropped the up or cancel event for the previous gesture
                // due to an app switch, ANR, or some other state change.
                cancelAndClearTouchTargets(ev);
                resetTouchState();
            }

            // Check for interception.
            final boolean intercepted;
            /*
            *下面的判断表示当ACTION_DOWN事件到来时,需要调用OnInterceptTouchEvent方法。同时如果               *mFirstTouchTarget不为null时需要调用OnInterceptTouchEvent方法,mFirstTouchTarget不为null
            *表示childView可以接受MotionEvent事件,即childView中有onTouchEvent返回true的。当ACTION_DOWN事
            *件到来时,mFirstTouchEvent必然为null。当MotionEvent后面的ACTION_MOVE和ACTION_UP事件到来时,
            *mFirstTouchTarget为null(childView都不接受MotionEvent事件),就会进入else语句,不会再调用
            *onInterceptTouchEvent方法。
            */
            if (actionMasked == MotionEvent.ACTION_DOWN
                    || mFirstTouchTarget != null) {
                //disallowIntercept可以让childView通过调用requestDisallowInterceptTouchEvent方法来设置
                final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
                if (!disallowIntercept) {
                    intercepted = onInterceptTouchEvent(ev);
                    ev.setAction(action); // restore action in case it was changed
                } else {
                    intercepted = false;
                }
            } else {
                // There are no touch targets and this action is not an initial down
                // so this view group continues to intercept touches.
                intercepted = true;
            }

            // Check for cancelation.
            final boolean canceled = resetCancelNextUpFlag(this)
                    || actionMasked == MotionEvent.ACTION_CANCEL;

            // Update list of touch targets for pointer down, if needed.
            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
            TouchTarget newTouchTarget = null;
            boolean alreadyDispatchedToNewTouchTarget = false;
            /*
            *当上面的intercepted为false,并且canceled为false时,就要向childview传递MotionEvent事件了。
            *
            *当ACTION_DOWN事件到来时,再上面调用了onInterceptTouchEvent方法,该方法默认返回false,当该方法
            *返回false,即intercepted为false(resetCancelNextUpFlag(this)大多情况下放回false,所以
            *canceled肯定为false,因为是ACTION_DOWN),if语句满足。
            */
            if (!canceled && !intercepted) {
                  /*
                  *当ACTION_DOWN或者ACTION_MOVE或者ACTION_POINTER_DOWN同时ViewGroup可以讲一个事件分给多个
                  *childView时,进入if语句。
                  */
                if (actionMasked == MotionEvent.ACTION_DOWN
                        || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
                        || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
                    final int actionIndex = ev.getActionIndex(); // always 0 for down
                    final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
                            : TouchTarget.ALL_POINTER_IDS;

                    // Clean up earlier touch targets for this pointer id in case they
                    // have become out of sync.
                    removePointersFromTouchTargets(idBitsToAssign);
                    /*
                    *下面找出MotionEvent对应的那个childView
                    */
                    final int childrenCount = mChildrenCount;
                    if (newTouchTarget == null && childrenCount != 0) {
                        final float x = ev.getX(actionIndex);
                        final float y = ev.getY(actionIndex);
                        // Find a child that can receive the event.
                        // Scan children from front to back.
                        final ArrayList<View> preorderedList = buildOrderedChildList();
                        final boolean customOrder = preorderedList == null
                                && isChildrenDrawingOrderEnabled();
                        final View[] children = mChildren;
                        for (int i = childrenCount - 1; i >= 0; i--) {
                            final int childIndex = customOrder
                                    ? getChildDrawingOrder(childrenCount, i) : i;
                            final View child = (preorderedList == null)
                                    ? children[childIndex] : preorderedList.get(childIndex);
                            /*
                            *当该childView不是可见,或者childView的区域不包含MotionEvent的位置,则继续遍历
                            */
                            if (!canViewReceivePointerEvents(child)
                                    || !isTransformedTouchPointInView(x, y, child, null)) {
                                continue;
                            }
                            //获取该childView对应的touchTarget
                            newTouchTarget = getTouchTarget(child);
                            //在ACTION_DOWN阶段,newTouchTarget肯定为null,这里在ACTION_POINTER_DOWN阶段进入if条件
                            if (newTouchTarget != null) {
                                // Child is already receiving touch within its bounds.
                                // Give it the new pointer in addition to the ones it is handling.
                                newTouchTarget.pointerIdBits |= idBitsToAssign;
                                break;
                            }

                            resetCancelNextUpFlag(child);
                            /*
                            *dispatchTransformedTouchEvent方法中会调用child的dispatchTouchEvent方法。
                            *如果child不是ViewGroup,则child的dispatchTouchEvent调用了OnTouchListener
                            *的onTouch方法,如果onTouch返回false,调用onTouchEvent方法。
                            *如果dispatchTransformedTouchEven返回true,说明child接受Touch事件。
                            */
                            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();
                                /*
                                *调用addTouchTarget后,mFirstTouchTarget就不为null了。
                                */
                                newTouchTarget = addTouchTarget(child, idBitsToAssign);
                                alreadyDispatchedToNewTouchTarget = true;
                                break;
                            }
                        }
                        if (preorderedList != null) preorderedList.clear();
                    }

                    if (newTouchTarget == null && mFirstTouchTarget != null) {
                        // Did not find a child to receive the event.
                        // Assign the pointer to the least recently added target.
                        newTouchTarget = mFirstTouchTarget;
                        while (newTouchTarget.next != null) {
                            newTouchTarget = newTouchTarget.next;
                        }
                        newTouchTarget.pointerIdBits |= idBitsToAssign;
                    }
                }
            }
            /*
            *没有child可以接受MotionEvent, 让自己来。
            */
            // Dispatch to touch targets.
            if (mFirstTouchTarget == null) {
                // No touch targets so treat this as an ordinary view.
                handled = dispatchTransformedTouchEvent(ev, canceled, null,
                        TouchTarget.ALL_POINTER_IDS);
            } else {
                // Dispatch to touch targets, excluding the new touch target if we already
                // dispatched to it.  Cancel touch targets if necessary.
                TouchTarget predecessor = null;
                TouchTarget target = mFirstTouchTarget;
                while (target != null) {
                    final TouchTarget next = target.next;
                    //下面的if语句表示在ACTION_DOWN阶段已经处理过了就不再处理
                    if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
                        handled = true;
                    } else {
                        /*
                        *下面将ACTION_MOVE事件和ACTION_UP事件对之前接受ACTION_DOWN事件的childView进行传递。
                        */
                        final boolean cancelChild = resetCancelNextUpFlag(target.child)
                                || intercepted;
                        if (dispatchTransformedTouchEvent(ev, cancelChild,
                                target.child, target.pointerIdBits)) {
                            handled = true;
                        }
                        if (cancelChild) {
                            if (predecessor == null) {
                                mFirstTouchTarget = next;
                            } else {
                                predecessor.next = next;
                            }
                            target.recycle();
                            target = next;
                            continue;
                        }
                    }
                    predecessor = target;
                    target = next;
                }
            }

            // Update list of touch targets for pointer up or cancel, if needed.
            if (canceled
                    || actionMasked == MotionEvent.ACTION_UP
                    || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
                resetTouchState();
            } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
                final int actionIndex = ev.getActionIndex();
                final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
                removePointersFromTouchTargets(idBitsToRemove);
            }
        }

        if (!handled && mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
        }
        return handled;
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16961次
    • 积分:277
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条
    文章分类