Android Touch event distribution mechanism learning, androidtouch
Android event distribution mechanism
ViewGroupdispatchTouchEvent returns true
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP MyrelativeLayout dispatchTouchEvent: ACTION_UP
ViewGroup itself consumes this event in dispatchTouchEvent
DispatchTouchEvent returns false
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOWN onTouchEvent: Activity ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP onTouchEvent: Activity ACTION_UP
Upload the onTouchEvent consumption event to the Activity
DispatchTouchEvent returns the super. dispatchTouchEvent (ev) subview, and also returns the super
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOW MyrelativeLayout onInterceptTouchEvent ACTION_DO MyImageView dispatchTouchEvent: ACTION_DOWN MyImageView onTouchEvent: ACTION_DOWN MyrelativeLayout: onTouchEvent ACTION_DOWN onTouchEvent: Activity ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP onTouchEvent: Activity ACTION_UP
ViewGroup returns the dispatchTouchEvent, which calls the onInterceptTouchEvent method in ViewGroup,
If onInterceptTouchEvent is returned, super. onInterceptTouchEvent (ev)
It is uploaded back to the subview.
Call the dispatchTouchEvent and onTouchEvent methods in the sub-View.
If both return super (). Then upload to ViewGroup,
In this case, the onTouchEvent of ViewGroup is called. If the returned value is super,
Return to Activity again, and call onTouchEvent of Activity to consume
OnInterceptTouchEvent in ViewGroup returns true, onTouchEvent returns super
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOWN MyrelativeLayout onInterceptTouchEvent ACTION_DOWN MyrelativeLayout: onTouchEvent ACTION_DOWN onTouchEvent: Activity ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP onTouchEvent: Activity ACTION_UP
The event starts with the dispatchTouchEvent of Activty.
Call the dispatchTouchEvent of ViewGroup. At this time, super is returned.
The onInterceptTouchEvent in ViewGroup is called to intercept the event.
OnInterceptTouchEvent returns true here, indicating Interception
It will be handed over to the onTouchEvent of ViewGroup.
OnTouchEvent Suepr is returned here. If it is not consumed, it will be passed up to Activity.
Finally, it is consumed by onTouchEvent in Activty.
The onInterceptTouchEvent in ViewGroup returns true, and the onTouchEvent returns true (same as the previous one, which is finally consumed by ViewGroup)
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DO MyrelativeLayout onInterceptTouchEvent ACTION_D MyrelativeLayout: onTouchEvent ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP MyrelativeLayout dispatchTouchEvent: ACTION_UP MyrelativeLayout: onTouchEvent ACTION_UP
The event starts with the dispatchTouchEvent of Activty.
Call the dispatchTouchEvent of ViewGroup. At this time, super is returned.
The onInterceptTouchEvent in ViewGroup is called to intercept the event.
OnInterceptTouchEvent returns true here, indicating Interception
It will be handed over to the onTouchEvent of ViewGroup.
Here, onTouchEvent returns true, indicating that the event will not be passed after consumption ends.
The onInterceptTouchEvent in ViewGroup returns true, and the onTouchEvent returns false (which is finally consumed by Activty)
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOWN MyrelativeLayout onInterceptTouchEvent ACTION_DOWN MyrelativeLayout: onTouchEvent ACTION_DOWN onTouchEvent: Activity ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP onTouchEvent: Activity ACTION_UP
The event starts with the dispatchTouchEvent of Activty.
Call the dispatchTouchEvent of ViewGroup. At this time, super is returned.
The onInterceptTouchEvent in ViewGroup is called to intercept the event.
OnInterceptTouchEvent returns true here, indicating Interception
It will be handed over to the onTouchEvent of ViewGroup.
Here, onTouchEvent returns false, indicating that the message is not consumed,
The event is passed to the Activity to call onTouchEvent for consumption.
OnInterceptTouchEvent in ViewGroup returns super
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOWN MyrelativeLayout onInterceptTouchEvent ACTION_DOWN MyImageView dispatchTouchEvent: ACTION_DOWN MyImageView onTouchEvent: ACTION_DOWN MyrelativeLayout: onTouchEvent ACTION_DOWN onTouchEvent: Activity ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP onTouchEvent: Activity ACTION_UP
From top to bottom
Activity dispatchTouchEvent
ViewGroup dispatchTouchEvent
ViewGroup onInterceptTouchEvent
DispatchTouchEvent of the sub-View
OnTouchEvent of the sub-View
OnTouchEvent of ViewGroud
OnTouchEvent of Acitivty
The dispatchTouchEvent of the View subview returns false.
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOWN MyrelativeLayout onInterceptTouchEvent ACTION_DOWN MyImageView dispatchTouchEvent: ACTION_DOWN MyrelativeLayout: onTouchEvent ACTION_DOWN onTouchEvent: Activity ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP onTouchEvent: Activity ACTION_UP
If all three ViewGroup Methods return super
From top to bottom
Activity dispatchTouchEvent
ViewGroup dispatchTouchEvent
ViewGroup onInterceptTouchEvent
Sub-View dispatchTouchEvent (return false to the onTouchEvent method of ViewGroup directly)
ViewGroup onTouchEvent
Activity onTouchEvent
Returns true for the dispatchTouchEvent of the sub-View.
dispatchTouchEvent: Activity ACTION_DOWN MyrelativeLayout dispatchTouchEvent: ACTION_DOWN MyrelativeLayout onInterceptTouchEvent ACTION_DOWN MyImageView dispatchTouchEvent: ACTION_DOWN dispatchTouchEvent: Activity ACTION_UP MyrelativeLayout dispatchTouchEvent: ACTION_UP MyrelativeLayout onInterceptTouchEvent ACTION_UP MyImageView dispatchTouchEvent: ACTION_UP
From top to bottom
Activity dispatchTouchEvent
ViewGroup dispatchTouchEvent
ViewGroup onInterceptTouchEvent
Sub-View dispatchTouchEvent (return true to consume this event directly)
The onTouchEvent of the sub-View returns true.
The condition subclass returns super ready: Activity ACTION_DOWN ready: ACTION_DOWN ready ACTION_DOWN MyImageView dispatchTouchEvent: ACTION_DOWN MyImageView onTouchEvent: ACTION_DOWN ready: Activity ACTION_UP ready: ACTION_UP ready ACTION_UP MyImageView ready: ACTION_UP MyImageView onTouchEvent: ACTION_UP
From top to bottom
Activity dispatchTouchEvent
ViewGroup dispatchTouchEvent
ViewGroup onInterceptTouchEvent
Sub-View dispatchTouchEvent (super)
The onTouchEvent of the sub-View returns true to consume this event and will not be passed up.
The onTouchEvent of the sub-View returns false.
The condition subclass returns super ready: Activity ACTION_DOWN ready: ACTION_DOWN ready ACTION_DOWN MyImageView ready: ACTION_DOWN MyImageView onTouchEvent: ACTION_DOWN ready: Ready ACTION_DOWN onTouchEvent: Activity ACTION_DOWN ready: Activity ACTION_UP onTouchEvent: activity ACTION_UP
From top to bottom
Activity dispatchTouchEvent
ViewGroup dispatchTouchEvent
ViewGroup onInterceptTouchEvent
Sub-View dispatchTouchEvent (super)
The onTouchEvent of the sub-View returns false.
OnTouchEvent of ViewGroud
OnTouchEvent of Acitivty
Event distribution:DispatchTouchEvent
If return true, the event is distributed to the current View and consumed by the dispatchTouchEvent method. At the same time, the event stops being passed down;
If return false, event distribution is divided into two situations:
If the event retrieved by the current View is directly from the Activity, the event is returned to the onTouchEvent of the Activity for consumption;
If the event retrieved by the current View comes from the outer parent control, the event is returned to the onTouchEvent of the parent View for consumption.
If the system returns the default super. dispatchTouchEvent (ev), the event is automatically distributed to the onInterceptTouchEvent method of the current View.
Event interception: public boolean onInterceptTouchEvent (MotionEvent ev)
When the dispatchTouchEvent (MotionEvent ev) method of the outer View returns the system's default super. dispatchTouchEvent (ev), the event is automatically distributed to the onInterceptTouchEvent method of the current View. The event interception logic of onInterceptTouchEvent is as follows:
If onInterceptTouchEvent returns true, it intercepts the event and submits the intercepted event to the onTouchEvent of the current View for processing;
If onInterceptTouchEvent returns false, the event is released. The event on the current View is passed to the subview, And the dispatchTouchEvent of the subview is used to distribute the event;
If onInterceptTouchEvent returns super. onInterceptTouchEvent (ev), the event is intercepted by default,
If a sub-View sends an event to the sub-View, the dispatchTouchEvent of the sub-View will process the event.
If no child View exists, the intercepted event is handed over to the onTouchEvent of the current View for processing.
Event Response: public boolean onTouchEvent (MotionEvent ev)
When dispatchTouchEvent returns super. dispatchTouchEvent (ev) and onInterceptTouchEvent returns true or super. onInterceptTouchEvent (ev), onTouchEvent is called. The Event Response logic of onTouchEvent is as follows:
If the event is passed to the onTouchEvent method of the current View, and the method returns false, the event will be passed up from the current View and received by the onTouchEvent of the upper View, if the onTouchEvent passed to the above method also returns false, the event will "disappear" and the next event will not be received.
If true is returned, the event is received and consumed.
If super. onTouchEvent (ev) is returned, the default event processing logic is the same as when false is returned.
Source code
Https://github.com/ln0491/AndroidTouchDemo