Event distribution core dispatchTouchEvent

The whole distribution of events between views is essentially one big recursive function, and that recursive function is the dispatchTouchEvent method. In this recursive process, onInterceptTouchEvent is called to intercept the event, or onTouchEvent is called to handle the event.

Step 1: Determine whether the current ViewGroup needs to intercept the touch event. If it does, the touch event will no longer be passed to the child View (or notify the child View with CANCEL).

Step 2: If there is no interception, distribute the event to the child View for further processing. If the child View captures the event, assign the mFirstTouchTarget value to the View that captures the touch event.

Step 3: Redistribute events according to mFirstTouchTarget.

conclusion

This class focuses on analyzing the process mechanism of dispatchTouchEvent, which is mainly divided into three parts:

The onInterceptTouchEvent method returns the value of the onInterceptTouchEvent method.

If a child View captures and consumes the touch event, the mFirstTouchTarget is assigned;

As a final step, the DOWN, MOVE, and UP events decide whether to handle the touch event themselves or redistribute it to the child View, depending on whether the mFirstTouchTarget is null.

It then introduces several special points in the overall event distribution.

DOWN events are special: the starting point of the event; Decide who will consume subsequent events;

MFirstTouchTarget is a linked list of views that capture touch events.

CANCEL event trigger scenario: When the parent View does not intercept and then intercepts again in a MOVE event, the child View receives a CANCEL event.