Android View event delivery

1. Basic knowledge

  1. All touch events are encapsulated into MotionEvent objects, including the touch position, time, history, and several fingers.
  2. The time types are divided into ACTION_DOWN, ACTION_UP, ACTION_POINTER_DOWN, ACTION_POINTER_UP, ACTION_CANCEL, and each event starts with ACTION_DOWN and ends with ACTION_UP.
  3. The processing of the event includes three categories, namely the delivery-dispatchTouchEvent() function, the interception-onInterceptTouchEvent(), the consumption-onTouchEvent() function and the onTouchListener() function.

2. Delivery process

  1. Events are delivered from Activity.dispatchTouchEvent(), and as long as they are not stopped or intercepted, they will be delivered from the topmost View (ViewGroup) to the bottom (child View). Child View can handle events through onTouchEvent().
  2. The event is passed from the parent View to the child View, and the ViewGroup can intercept the event through onInterceptTouchEvent() and stop passing it down.
  3. If the event has not been stopped in the process of passing from top to bottom, and the bottom-level child View has not consumed the event, the event will be passed upward in the reverse direction. At this time, the parent View (ViewGroup) can be consumed. If it is still not consumed, it will eventually be consumed. to the onTouchEvent() function of the Activity.
  4. If View does not consume ACTION_DOWN, subsequent events will not be delivered.
  5. OnTouchListener consumes events prior to onTouchEvent(), and the above consumption means that the return value of the function is True.

 

For more information, please read the original English text of the PDF: Mastering the Android Touch System

Sample code: Demo@Github

 

Attached are the original two flow charts:

1. View does not handle event flow chart

 

 

2. View processing event flow chart

 

 

Personal additions:

All events will go through the activity's dispatchTouchEvent()

No matter at which layer the down event is consumed, subsequent events will not be passed down, such as (combined with the following figure):

1. The down event is consumed by the activity, and the subsequent events are directly passed from the activity's dispatchTouchEvent() to the activity's onTouchEvent(), and will not be passed to the ViewGroup layer.

2. The down event is consumed by the ViewGroup, then the subsequent events are passed from the dispatchTouchEvent() of the ViewGroup to the onTouchEvent() of the ViewGroup, and will not be passed to the View layer, nor will it go through the onInterceptTouchEvent(); but the dispatchTouchEvent() of the Activity Events can still be received and processed (of course, they can also be intercepted and resent), and the activity's onTouchEvent() will not receive any events.

3. If the down event is consumed by the View, the subsequent events will be passed to the View's onTouchEvent(), but will not be passed to the Activity and ViewGroup's onTouchEvent(); Activity's dispatchTouchEvent(), ViewGroup's dispatchTouchEvent() and onInterceptTouchEvent() are still Can receive and process events (and of course intercept and resend)

The following is a simple event delivery flow chart drawn by myself:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325788718&siteId=291194637