Event distribution mechanism of Android view

1、 The Android view framework provides three main operation concepts for events.

1. Event distribution mechanism, dispatchtouchevent. It mainly refers to the mechanism that the parent distributes events to its child according to the generation location of touch events and whether the child is willing to handle the series of events.

2. Event interception mechanism, onintercepttouchevent. The main mechanism is that the parent intercepts events according to its internal state or the state of the child to prevent them from being further transmitted to the child.

3. Event processing mechanism, ontouchevent. It is mainly a mechanism for the receiver of the event sequence (which can be a view or ViewGroup) to process the event and transfer the processing results to its parents.

2、 In Java, there are many ways to transfer calculation results. Here is a method suitable for synchronous call, the method of return value. Each mechanism uses boolean type as its return value, so what does each return value of each mechanism mean.

1. Event distribution mechanism, dispatchtouchevent.

True - the event is successfully processed by the view tree with this node as the root node. At this time, even if the processing is completed, the event will not be returned to the view's parent node (the node that distributes the event).

False - for a view with this node as the root node, no view (including this view) has successfully handled this event, so the event will be returned upward to the view's parent node (the node that distributes the event).

2. Event interception mechanism, onintercepttouchevent. The main mechanism is that the parent intercepts events according to its internal state or the state of the child to prevent them from being further transmitted to the child.

True - the current ViewGroup (because there is no such method in the view, and the view without child does not need to have an interception mechanism) hopes that this event will no longer be passed to its child, but that it will be handled by itself.

False - the current ViewGroup is not ready to intercept this event. The event is normally distributed down to its children.

3. Event processing mechanism, ontouchevent. It is mainly a mechanism for the receiver of the event sequence (which can be a view or ViewGroup) to process the event and transfer the processing results to its parents.

True - indicates that the view successfully processed the event, and the processing result will be notified to its parent.

False - indicates that the view failed to process the event successfully, then its parent will have the opportunity to process the event (the parent is marked as the receiver of the event sequence, and the parent's ontouchevent returns true when the down event occurs).

3、 Source code analysis

View:

1、dispatchTouchEvent:

/**Distribute the event to the target object. Because this is a view object and does not contain child by default, he will distribute the event to himself*/

public boolean dispatchTouchEvent(MotionEvent event);

ViewGroup:

1、onInterceptTouchEvent

/**The default implementation returns false, that is, it does not intercept any events by default*/

public boolean onInterceptTouchEvent(MotionEvent ev);

2、dispatchTouchEvent

/**Distribute events to their children or themselves according to the internal interception status*/

public boolean dispatchTouchEvent(MotionEvent ev);

3、requestDisallowInterceptTouchEvent

/**The event distribution mechanism of dry parent informs the parent whether to intercept subsequent events. If it is set to true, the parent will not intercept the event regardless of its status. Set to false, and the parent follows the normal interception process*/

public void requestDisallowInterceptTouchEvent(boolean disallowIntercept);

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>