[Android] event interception and distribution mechanism

preamble

In Android, the event distribution mechanism refers to a set of mechanisms for how events (such as clicks, touches, slides, etc.) are delivered and processed when the user interacts with the application.

Steps for event reception and processing

1 When the user performs on the device 交互(such as clicking, touching the screen), the operating system will send the corresponding information 输入事件to the application.

2 The application's 窗口管理器(Window Manager) is responsible for delivering input events to the correct window.

3 In Android, each window has a corresponding ViewRootImpl object, which is the handler of the window's root view (Root View).

4 When a window is created and displayed on the screen, the ViewRootImpl object creates a WindowInputEventReceiverreceiver named to receive input events.

5 When an input event arrives, the operating system sends it to WindowInputEventReceiver, which handles it.

6 In the WindowInputEventReceiver, the input event is put into a 队列pending processing.

7 The ViewRootImpl object periodically 检查输入事件队列and sequentially processes events within it.

8 When an event is dequeued, it first goes through a series of InputStage stages.

9 Different InputStage implementation classes correspond to different 事件处理stages, such as ViewPostImeInputStage, ViewPreImeInputStage, etc.

10 In each InputStage, events can be 处理、转换或传递received 下一个阶段.

11 When the event reaches ViewPostImeInputStage the stage, the method is called DecorView.dispatchTouchEvent() to send the event to 最顶层的视图.

12 DecorView 是整个应用程序窗口的根视图, usually a FrameLayout.

13 In DecorView.dispatchTouchEvent() the method, events are handled based on the touch location and other properties 分发给子元素(即子 View).

14 child Views are available 决定是否消耗事件(处理事件)并做出响应.

15 The sub-View that the event finally reaches 交互目标, the method of the sub-View onTouchEvent() will be called to implement the specific processing logic for the event.

touch event type

ACTION_DOWN:
Fired when a finger touches the screen for the first time. This event is usually triggered when the user presses the touch screen. Can be used to implement functions such as clicking and dragging.

ACTION_MOVE:
Triggered when the finger slides on the screen, it may be triggered multiple times. When the user presses the screen and moves the finger, this event will be triggered continuously, which can be used to achieve interactive effects such as sliding and dragging.

ACTION_UP:
Fired when the finger leaves the screen. This event is typically fired when the user releases the touch from the screen. Can be used to implement functions such as click, button release, etc.

ACTION_CANCEL:
Triggered when the event is intercepted by the upper layer. If other controls or touch listeners intercept the current event, the event delivery will be canceled and the ACTION_CANCEL event will be triggered. It is usually used to notify the cancellation of the touch event, clear the corresponding state or perform some special processing.

View inheritance relationship

insert image description here

Several important methods of event processing mechanism

dispatchTouchEvent():
Used to distribute touch events to individual views in the view hierarchy. This method is usually called by the parent view to pass touch events to child views or do other processing.

onInterceptTouchEvent():
Used to intercept touch events. If a view's onInterceptTouchEvent() method returns true, it indicates that the view wishes to intercept touch events and handle them itself, without passing the event to its subviews.

onTouchEvent():
Logic for handling touch events. In this method, corresponding operations can be performed according to event types (such as ACTION_DOWN, ACTION_MOVE, etc.), such as handling sliding gestures, click events, etc.

event conflict resolution

internal intercept method

In the internal interception method, subviews onInterceptTouchEvent() decide whether to intercept touch events by overriding methods.

Subviews can judge whether to intercept touch events according to their own logic and conditions.

If the subview intercepts the touch event, subsequent touch events (such as MOVE, UP, etc.) will be handled by the subview itself.

If the child view does not intercept the touch event, the touch event will continue to be passed to the parent container for processing.

At this point, the child view can getParent().requestDisallowInterceptTouchEvent(true)request the parent container not to intercept subsequent touch events by calling a method.

external intercept method

In the external interception method, the parent container monitors the touch event of the child view, and decides whether to intercept the event according to certain logic.

When the parent container intercepts touch events, child views cannot handle these events directly, but are handled by the parent container.

When the parent container does not intercept touch events, child views handle these events normally.

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/131824656