1. DOWN Event flow direction

【 Key points 】

  • dispatchTouchEvent,onTouchEventreturntrue: The event is stopped (no one can receive the event again).
  • dispatchTouchEventreturnfalse: Events stop passing and distributing to the child View and start backtracking to the parent control, just like recursive stops and backtracking begins.
  • dispatchTouchEvent,onTouchEvent,onInterceptTouchEventreturnsuper.xxxxxx(): Events follow a U-shaped path through the entire event.

2. Flow direction of MOVE and UP events

2.1. DispatchTouchEvent Returns true

ACTION_MOVE and ACTION_UP events are not passed down the View and are consumed immediately.

2.2. OnTouchEvent Returns true

ACTION_MOVE and ACTION_UP events are not passed down the View and will be consumed by onTouchEvent.

2.3. Summarize

  • ACTION_DOWNIn which control is the event consumed (return true), thenACTION_MOVEACTION_UPIt goes from the top downdispatchTouchEventEvent distribution is passed down to the control, not down.
  • ifACTION_DOWNEvent is indispatchTouchEventConsume, then the event stops passing.
  • ifACTION_DOWNEvent is inonTouchEventConsumer, then will putACTION_MOVEACTION_UPEvent to the controlonTouchEventProcess and end the pass.

3. setOnTouchListener

public boolean dispatchTouchEvent(MotionEvent event) {
    // ...
    boolean result = false;
    // ...
    if (onFilterTouchEventForSecurity(event)) {
        // ...
        if(li ! =null&& li.mOnTouchListener ! =null
                && (mViewFlags & ENABLED_MASK) == ENABLED
                && li.mOnTouchListener.onTouch(this, event)) {
                    // Handle OnTouchListener first
                    // If the return value is true, its own onTouchEvent will not be executed and consumed
            result = true;
        }
        // Pass onTouchEvent to yourself
        if(! result && onTouchEvent(event)) { result =true; }}// ...
    return result;
}
Copy the code

【 conclusion 】

  • Is not setOnTouchListenerIs executed by defaultonTouchEvent(event)Methods.
  • Set upOnTouchListener, the system will execute it first and decide whether to execute it based on its return valueonTouchEvent(event).
  • seeOnTouchListenerPriority ratio ofonTouchEventHigh.