View事件分发(传递)机制

2017-11-17  本文已影响113人  xlsong

View事件分发(传递)机制

前言:View事件分发机制是Android中比较重要和复杂的部分,只有理解了事件分发,才能更好的自定义控件解决滑动冲突,本篇文章基于API25(7.1)。

相关方法

事件分发方法,触控事件从该方法开始传递,返回true表示分发完成,返回false则失败

事件拦截方法,只有ViewGroup中才存在,在dispatchTouchEvent中判断是否拦截事件由自己处理,或者交给子View(ViewGroup)分发,返回true表示拦截事件,默认返回false

事件处理方法,处理事件,返回true表示消耗事件,返回false表示不消耗事件

dispatchTouchEvent OnInterceptTouchEvent onTouchEvent
Activity ×
ViewGroup
View ×

源码分析

Activity

 /**
 * Called to process touch screen events.  You can override this to
 * intercept all touch screen events before they are dispatched to the
 * window.  Be sure to call this implementation for touch screen events
 * that should be handled normally.
 *
 * @param ev The touch screen event.
 *
 * @return boolean Return true if this event was consumed.
 */
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        onUserInteraction();
    }
    if (getWindow().superDispatchTouchEvent(ev)) {
        return true;
    }
    return onTouchEvent(ev);
}

  /**
 * Retrieve the current {@link android.view.Window} for the activity.
 * This can be used to directly access parts of the Window API that
 * are not available through Activity/Screen.
 *
 * @return Window The current window, or null if the activity is not
 *         visual.
 */
public Window getWindow() {
    return mWindow;
}

事件首先从Activity的disptachTouchEvent的方法开始调用,调用了Window的superDispatchTouchEvent方法,至于事件MotionEvent的来源不在本章讨论的范围内

/**
 * Called when a touch screen event was not handled by any of the views
 * under it.  This is most useful to process touch events that happen
 * outside of your window bounds, where there is no view to receive it.
 *
 * @param event The touch screen event being processed.
 *
 * @return Return true if you have consumed the event, false if you haven't.
 * The default implementation always returns false.
 */
public boolean onTouchEvent(MotionEvent event) {
    if (mWindow.shouldCloseOnTouch(this, event)) {
        finish();
        return true;
    }

    return false;
}

Activity默认不会消耗事件

Window

可以看到这是个抽象方法,Window是一个抽象类,Window的唯一实现是PhoneWindow

 /**
 * Abstract base class for a top-level window look and behavior policy.  An
 * instance of this class should be used as the top-level view added to the
 * window manager. It provides standard UI policies such as a background, title
 * area, default key processing, etc.
 *
 * <p>The only existing implementation of this abstract class is
 * android.view.PhoneWindow, which you should instantiate when needing a
 * Window.
 */
public abstract class Window {
...
 /**
 * Used by custom windows, such as Dialog, to pass the touch screen event
 * further down the view hierarchy. Application developers should
 * not need to implement or call this.
 *
 */
public abstract boolean superDispatchTouchEvent(MotionEvent event);
...
 }

PhoneWindow

// This is the top-level view of the window, containing the window decor.
private DecorView mDecor;
 @Override
public boolean superDispatchTouchEvent(MotionEvent event) {
    return mDecor.superDispatchTouchEvent(event);
}

PhoneWindow的该方法代码就一行,调用了DecorView的superDisptachTouchEvent方法

DecorView

/** @hide */
public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks {
...
public boolean superDispatchTouchEvent(MotionEvent event) {
    return super.dispatchTouchEvent(event);
}
...
}

DecorView调用了父类FrameLayout的disptachTouchEvent,FrameLayout继承于ViewGroup,且没有重写该方法,所以终于到了ViewGroup的dispatchTouchEvent方法,下面开始重点

ViewGroup

ViewGroup的dispatchTouchEvent方法比较长,先分开看

dispatchTouchEvent#1

        // Handle an initial down.
        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;
       if (actionMasked == MotionEvent.ACTION_DOWN
               || mFirstTouchTarget != null) {
           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;
       }

首先在ACTION_DOWN中清除所有的触摸状态和目标, 然后检查是否需要拦截事件,当为ACTION_DOWN事件,首选检查标志位FLAG_DISALLOW_INTERCEPT是否已设置,即是否不拦截事件,通过requestDisallowInterceptTouchEvent方法可以设置该标志位,默认没有设置,disallowIntercept是false,如果已设置该标志位,则不截断子View(ViewGroup)事件,否则会调用自身的onInterceptTouchEvent方法。

@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {
        // We're already in this state, assume our ancestors are too
        return;
    }

    if (disallowIntercept) {
        mGroupFlags |= FLAG_DISALLOW_INTERCEPT;
    } else {
        mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
    }

    // Pass it up to our parent
    if (mParent != null) {
        mParent.requestDisallowInterceptTouchEvent(disallowIntercept);
    }
}

 /**
 * @param ev The motion event being dispatched down the hierarchy.
 * @return Return true to steal motion events from the children and have
 * them dispatched to this ViewGroup through onTouchEvent().
 * The current target will receive an ACTION_CANCEL event, and no further
 * messages will be delivered here.
 */
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
            && ev.getAction() == MotionEvent.ACTION_DOWN
            && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
            && isOnScrollbarThumb(ev.getX(), ev.getY())) {
        return true;
    }
    return false;
}

可以看到ViewGroup默认情况是不会拦截事件,所以接下来遍历子View,并调用disptachTransformedTouchEvent
dispatchTouchEvent#2

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

                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 = buildTouchDispatchChildList();
                    final boolean customOrder = preorderedList == null
                            && isChildrenDrawingOrderEnabled();
                    final View[] children = mChildren;
                    for (int i = childrenCount - 1; i >= 0; i--) {
                        final int childIndex = getAndVerifyPreorderedIndex(
                                childrenCount, i, customOrder);
                        final View child = getAndVerifyPreorderedView(
                                preorderedList, children, childIndex);
                        ...
                        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();
                            newTouchTarget = addTouchTarget(child, idBitsToAssign);
                            alreadyDispatchedToNewTouchTarget = true;
                            break;
                        }
                    ...     
                    }
                }

如果有符合条件的child(触摸点和变换后的点在该View内)对应于dispatchTransformedTouchEvent返回true的,则newTouchTraget = addTouchTarget(child, idBitsToAssign)会走到

/**
 * Adds a touch target for specified child to the beginning of the list.
 * Assumes the target child is not already present.
 */
private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
    final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
    target.next = mFirstTouchTarget;
    mFirstTouchTarget = target;
    return target;
}

可以看到如果mFirstTouchTarget赋值了,则后续事件序列继续走进第一步事件拦截判断语句内,否则后续的事件序列都由该ViewGroup拦截

/**
 * Transforms a motion event into the coordinate space of a particular child view,
 * filters out irrelevant pointer ids, and overrides its action if necessary.
 * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
 */
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
        View child, int desiredPointerIdBits) {
    final boolean handled;

    // Canceling motions is a special case.  We don't need to perform any transformations
    // or filtering.  The important part is the action, not the contents.
    final int oldAction = event.getAction();
    if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
        event.setAction(MotionEvent.ACTION_CANCEL);
        if (child == null) {
            handled = super.dispatchTouchEvent(event);
        } else {
            handled = child.dispatchTouchEvent(event);
        }
        event.setAction(oldAction);
        return handled;
    }

    // Calculate the number of pointers to deliver.
    final int oldPointerIdBits = event.getPointerIdBits();
    final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits;

    // If for some reason we ended up in an inconsistent state where it looks like we
    // might produce a motion event with no pointers in it, then drop the event.
    if (newPointerIdBits == 0) {
        return false;
    }

    // If the number of pointers is the same and we don't need to perform any fancy
    // irreversible transformations, then we can reuse the motion event for this
    // dispatch as long as we are careful to revert any changes we make.
    // Otherwise we need to make a copy.
    final MotionEvent transformedEvent;
    if (newPointerIdBits == oldPointerIdBits) {
        if (child == null || child.hasIdentityMatrix()) {
            if (child == null) {
                handled = super.dispatchTouchEvent(event);
            } else {
                final float offsetX = mScrollX - child.mLeft;
                final float offsetY = mScrollY - child.mTop;
                event.offsetLocation(offsetX, offsetY);

                handled = child.dispatchTouchEvent(event);

                event.offsetLocation(-offsetX, -offsetY);
            }
            return handled;
        }
        transformedEvent = MotionEvent.obtain(event);
    } else {
        transformedEvent = event.split(newPointerIdBits);
    }

    // Perform any necessary transformations and dispatch.
    if (child == null) {
        handled = super.dispatchTouchEvent(transformedEvent);
    } else {
        final float offsetX = mScrollX - child.mLeft;
        final float offsetY = mScrollY - child.mTop;
        transformedEvent.offsetLocation(offsetX, offsetY);
        if (! child.hasIdentityMatrix()) {
            transformedEvent.transform(child.getInverseMatrix());
        }

        handled = child.dispatchTouchEvent(transformedEvent);
    }

    // Done.
    transformedEvent.recycle();
    return handled;
}

可以看到如果child为null则会调用ViewGroup父类的dispatchTouchEvent,ViewGroup的父类是View则会走到View的dispatchTouchEvent中;否则会调用child的dispatchTouchEvent,即事件继续传递下去

disptachTouchEvent#3

     // 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 (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
                    handled = true;
                } else {
                    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;
            }
        }

所以如果mFirstTouchTarget为null,即ViewGroup拦截事件或者ViewGroup没有child消耗该事件,则调用super#disptachTouchEvent否则调用child的dispatchTouchEvent,如果child不消耗ACTION_DOWN除外的事件,则该事件消失,不会回传给父ViewGroup,最终会传给Activity的onTouchEvent,并且后续的事情此child也能收到。如果ViewGroup在ACTION_MOVE中拦截事件,则会走到dispatch(ev,true,target.child,target.pointIdBits)中,在该方法中child会收到一个ACTION_CANCEL事件,VieGroup不处理该事件,且predecessor == nullmFirstTouchTarget = next 最终,mFirstTouchTarget置为null,从下个事件开始都交由ViewGroup处理。

View

/**
 * Pass the touch screen motion event down to the target view, or this
 * view if it is the target.
 *
 * @param event The motion event to be dispatched.
 * @return True if the event was handled by the view, false otherwise.
 */
public boolean dispatchTouchEvent(MotionEvent event) {
    // If the event should be handled by accessibility focus first.
    if (event.isTargetAccessibilityFocus()) {
        // We don't have focus or no virtual descendant has it, do not handle the event.
        if (!isAccessibilityFocusedViewOrHost()) {
            return false;
        }
        // We have focus and got the event, then use normal event dispatch.
        event.setTargetAccessibilityFocus(false);
    }

    boolean result = false;

    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onTouchEvent(event, 0);
    }

    final int actionMasked = event.getActionMasked();
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Defensive cleanup for new gesture
        stopNestedScroll();
    }

    if (onFilterTouchEventForSecurity(event)) {
        if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
            result = true;
        }
        //noinspection SimplifiableIfStatement
        ListenerInfo li = mListenerInfo;
        if (li != null && li.mOnTouchListener != null
                && (mViewFlags & ENABLED_MASK) == ENABLED
                && li.mOnTouchListener.onTouch(this, event)) {
            result = true;
        }

        if (!result && onTouchEvent(event)) {
            result = true;
        }
    }

    if (!result && mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
    }

    // Clean up after nested scrolls if this is the end of a gesture;
    // also cancel it if we tried an ACTION_DOWN but we didn't want the rest
    // of the gesture.
    if (actionMasked == MotionEvent.ACTION_UP ||
            actionMasked == MotionEvent.ACTION_CANCEL ||
            (actionMasked == MotionEvent.ACTION_DOWN && !result)) {
        stopNestedScroll();
    }

    return result;
}

首先会检查是否该View会设置OnTouchListener、是否enableonTouchListener.onTouch(this,event)是否返回true,如果条件都满足则标识该View消耗了事件,传递完成,否则会继续调用onTouchEvent

/**
 * Implement this method to handle touch screen motion events.
 * <p>
 * If this method is used to detect click actions, it is recommended that
 * the actions be performed by implementing and calling
 * {@link #performClick()}. This will ensure consistent system behavior,
 * including:
 * <ul>
 * <li>obeying click sound preferences
 * <li>dispatching OnClickListener calls
 * <li>handling {@link AccessibilityNodeInfo#ACTION_CLICK ACTION_CLICK} when
 * accessibility features are enabled
 * </ul>
 *
 * @param event The motion event.
 * @return True if the event was handled, false otherwise.
 */
public boolean onTouchEvent(MotionEvent event) {
    final float x = event.getX();
    final float y = event.getY();
    final int viewFlags = mViewFlags;
    final int action = event.getAction();

    if ((viewFlags & ENABLED_MASK) == DISABLED) {
        if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
            setPressed(false);
        }
        // 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)
                || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);
    }
    if (mTouchDelegate != null) {
        if (mTouchDelegate.onTouchEvent(event)) {
            return true;
        }
    }

    if (((viewFlags & CLICKABLE) == CLICKABLE ||
            (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
            (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
        switch (action) {
            case MotionEvent.ACTION_UP:
                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();
                            }
                        }
                    }

                    if (mUnsetPressedState == null) {
                        mUnsetPressedState = new UnsetPressedState();
                    }

                    if (prepressed) {
                        postDelayed(mUnsetPressedState,
                                ViewConfiguration.getPressedStateDuration());
                    } else if (!post(mUnsetPressedState)) {
                        // If the post failed, unpress right now
                        mUnsetPressedState.run();
                    }

                    removeTapCallback();
                }
                mIgnoreNextUpEvent = false;
                break;

            case MotionEvent.ACTION_DOWN:
                mHasPerformedLongPress = false;

                if (performButtonActionOnTouchDown(event)) {
                    break;
                }

                // Walk up the hierarchy to determine if we're inside a scrolling container.
                boolean isInScrollingContainer = isInScrollingContainer();

                // For views inside a scrolling container, delay the pressed feedback for
                // a short period in case this is a scroll.
                if (isInScrollingContainer) {
                    mPrivateFlags |= PFLAG_PREPRESSED;
                    if (mPendingCheckForTap == null) {
                        mPendingCheckForTap = new CheckForTap();
                    }
                    mPendingCheckForTap.x = event.getX();
                    mPendingCheckForTap.y = event.getY();
                    postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
                } else {
                    // Not inside a scrolling container, so show the feedback right away
                    setPressed(true, x, y);
                    checkForLongClick(0, x, y);
                }
                break;

            case MotionEvent.ACTION_CANCEL:
                setPressed(false);
                removeTapCallback();
                removeLongPressCallback();
                mInContextButtonPress = false;
                mHasPerformedLongPress = false;
                mIgnoreNextUpEvent = false;
                break;

            case MotionEvent.ACTION_MOVE:
                drawableHotspotChanged(x, y);

                // Be lenient about moving outside of buttons
                if (!pointInView(x, y, mTouchSlop)) {
                    // Outside button
                    removeTapCallback();
                    if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
                        // Remove any future long press/tap checks
                        removeLongPressCallback();

                        setPressed(false);
                    }
                }
                break;
        }

        return true;
    }

    return false;
}

如果控件不是enable的,但是是可以点击的clickablelongClickable或者contextClickable的也会消耗该事件;如果控件是enable的,则会根据事件的时间长短来执行checkForLongClick或者perfomrClick方法,可点击的View的onTouchEvent默认返回true,如Button,不可点击的则返回false,如TextView和ImageView

private void checkForLongClick(int delayOffset, float x, float y) {
    if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
        mHasPerformedLongPress = false;

        if (mPendingCheckForLongPress == null) {
            mPendingCheckForLongPress = new CheckForLongPress();
        }
        mPendingCheckForLongPress.setAnchor(x, y);
        mPendingCheckForLongPress.rememberWindowAttachCount();
        postDelayed(mPendingCheckForLongPress,
                ViewConfiguration.getLongPressTimeout() - delayOffset);
    }
}

private final class CheckForLongPress implements Runnable {
    private int mOriginalWindowAttachCount;
    private float mX;
    private float mY;

    @Override
    public void run() {
        if (isPressed() && (mParent != null)
                && mOriginalWindowAttachCount == mWindowAttachCount) {
            if (performLongClick(mX, mY)) {
                mHasPerformedLongPress = true;
            }
        }
    }

    public void setAnchor(float x, float y) {
        mX = x;
        mY = y;
    }

    public void rememberWindowAttachCount() {
        mOriginalWindowAttachCount = mWindowAttachCount;
    }
}

/**
 * Calls this view's OnLongClickListener, if it is defined. Invokes the
 * context menu if the OnLongClickListener did not consume the event,
 * anchoring it to an (x,y) coordinate.
 *
 * @param x x coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @param y y coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @return {@code true} if one of the above receivers consumed the event,
 *         {@code false} otherwise
 */
public boolean performLongClick(float x, float y) {
    mLongClickX = x;
    mLongClickY = y;
    final boolean handled = performLongClick();
    mLongClickX = Float.NaN;
    mLongClickY = Float.NaN;
    return handled;
}

 /**
 * Calls this view's OnLongClickListener, if it is defined. Invokes the
 * context menu if the OnLongClickListener did not consume the event.
 *
 * @return {@code true} if one of the above receivers consumed the event,
 *         {@code false} otherwise
 */
public boolean performLongClick() {
    return performLongClickInternal(mLongClickX, mLongClickY);
}

    
/**
 * Calls this view's OnLongClickListener, if it is defined. Invokes the
 * context menu if the OnLongClickListener did not consume the event,
 * optionally anchoring it to an (x,y) coordinate.
 *
 * @param x x coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @param y y coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @return {@code true} if one of the above receivers consumed the event,
 *         {@code false} otherwise
 */
private boolean performLongClickInternal(float x, float y) {
    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);

    boolean handled = false;
    final ListenerInfo li = mListenerInfo;
    if (li != null && li.mOnLongClickListener != null) {
        handled = li.mOnLongClickListener.onLongClick(View.this);
    }
    if (!handled) {
        final boolean isAnchored = !Float.isNaN(x) && !Float.isNaN(y);
        handled = isAnchored ? showContextMenu(x, y) : showContextMenu();
    }
    if (handled) {
        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
    }
    return handled;
}

/**
 * Call this view's OnClickListener, if it is defined.  Performs all normal
 * actions associated with clicking: reporting accessibility event, playing
 * a sound, etc.
 *
 * @return True there was an assigned OnClickListener that was called, false
 *         otherwise is returned.
 */
public boolean performClick() {
    final boolean result;
    final ListenerInfo li = mListenerInfo;
    if (li != null && li.mOnClickListener != null) {
        playSoundEffect(SoundEffectConstants.CLICK);
        li.mOnClickListener.onClick(this);
        result = true;
    } else {
        result = false;
    }

    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
    return result;
}

结论

View事件传递.png
  1. Activity和View中没有OnInterceptTouchEvent方法,只有ViewGroup才有
  2. 分发过程由上到下,Activity->Window(PhoneWindow)->DecorView->ViewGroup->View,处理过程由下到上,View->ViewGroup->DecorView->Window(PhoneWindow)->Activity,如果分发(处理)过程中有View/ViewGroup消耗事件[返回true]则事件不会往下传递(往上传递)
  3. 事件分发序列是有一个ACTION_DOWN,零或多个ACTION_MOVE,单个ACTION_UP组成,即ACTION_DOWN->[ACTION_MOVE->...->ACTION_MOVE->]ACTION_UP
  4. ViewGroup的ACTION_DOWN事件必定调用OnInterceptTouchEvent,如果拦截了ACTION_DOWN事件则后续的事件序列都不会再调用OnInterceptTouchEvent, 事件都由该ViewGroup处理,ViewGroup的Child也不会再收到后续的事件序列
  5. 如果ViewGroup在ACTION_MOVE中拦截事件,则会走到dispatch(ev,true,target.child,target.pointIdBits)中,在该方法中child会收到一个ACTION_CANCEL事件,ViewGroup不处理该事件,最终,mFirstTouchTarget置为null,从下个事件开始都交由ViewGroup处理
  6. 子类可以通过调用的父类的requestDisallowInterceptTouchEvent来改变父类是否拦截事件,但是如果ACTION_DOWN事件已被父类拦截,则无法改变;且ACTION_DOWN事件不受该方法标志位FLAG_DISALLOW_INTERCEPT控制,因为ViewGroup的disptachTouchEvent方法在收到ACTION_DOWN事件时会调用resetTouchState方法重置该标志位
  7. 如果child不消耗ACTION_DOWN,即dispatchTouchEvent返回false,则后续的事件序列都由其父ViewGroup处理,调用View#dispatchTouchEvent
  8. 如果child不消耗ACTION_DOWN除外的事件,则该事件消失,不会回传给父ViewGroup,最终会传给Activity的onTouchEvent,但是后续的事情此child也能收到
  9. View#disptachTouchEvent中首先会判断是否有设置OnTouchListener、控件enableonTouch返回true,满足条件则事件分发完成,否则会继续调用View#onTouchEvent
  10. View#onTouchEvent中,即使该View不是enable的,但是其是clickablelongClickable或者contextClickable的则也消耗事件,如果View是enable的,则根据时间判断是执行performClick还是performLongClick,可点击的View的onTouchEvent默认返回true,如Button,不可点击的则返回false,如TextView和ImageView
  11. View方法调用顺序: dispatchTouchEvent->mOnTouchListener#onTouch->onTouchEvent->performLongClick->performClick
  12. ViewGroup拦截ACTION_DOWN:dispatchTouchEvent->onInterceptTouchEvent->super.disptachTouchEvent,后续事件: dispatchTouchEvent->super.disptachTouchEvent
  13. ViewGroup不拦截ACTION_DOWNchild消耗ACTION_DOWN, dispatchTouchEvent->onInterceptTouchEvent->child#disptachTouchEvent->,后续事件事件dispatchTouchEvent->onInterceptTouchEvent->child#disptachTouchEvent->;child不消耗ACTION_DOWNdispatchTouchEvent->onInterceptTouchEvent->child#disptachTouchEvent->super.disptachTouchEvent,后续事件
    dispatchTouchEvent->super.disptachTouchEvent
  14. ViewGroup拦截ACTION_MOVEdispatchTouchEvent->onInterceptTouchEvent->child#dispatchTouchEvent(ACTION_CANCEL),后续事件disptachTouchEvent->super.dispatchTouchEvent

参考文献

  1. Android事件分发机制完全解析,带你从源码的角度彻底理解(上) ,
    Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
  2. 图解 Android 事件分发机制
  3. 十分钟彻底弄明白 View 事件分发机制
  4. Android View事件分发机制源码分析
  5. Android事件传递机制
  6. View 的事件分发机制
  7. 安卓自定义View进阶-事件分发机制原理,安卓自定义View进阶-事件分发机制详解
  8. Activity, Window, PhoneWindow,ViewGroup,View,MotionEvent
  9. 《Android开发艺术探索》第三章View事件分发机制
上一篇下一篇

猜你喜欢

热点阅读