banner
jzman

jzman

Coding、思考、自觉。
github

Android Event Distribution Basics

PS: Make the greatest effort and prepare for the worst. The laziness you once indulged in will eventually become a slap in the face.

The Android event distribution mechanism is a relatively important part of the content. Summarize some knowledge about Android event distribution and plan to write it in four articles. The content is as follows:

image

This is the first article, hoping to describe the overall distribution process from a high-level perspective, so that readers can have a preliminary understanding of event distribution. The content is as follows:

  1. View and ViewGroup
  2. MotionEvent object
  3. View event distribution
  4. Summary

View and ViewGroup#

All components in an Android application inherit the View class, which is the base class for all UI components in Android. View has an important subclass called ViewGroup, which is usually used as a container for other views. It can contain ordinary views as well as other ViewGroups. The relationship between View and ViewGroup forms the structure of the entire View tree. For example, LinearLayout is not only a View, but also a ViewGroup that can contain various views, and this view can also be a ViewGroup, and so on.

In Android devices, operations mainly rely on various gestures, such as swiping, dragging, and clicking. This series of operations can easily interact with Android devices. As mentioned earlier, different views are at different levels. The question is, how do we correctly make a specific view respond to a certain operation when we operate it? Will there be any scrolling conflicts between different views? The answer is yes. To solve such problems, it is necessary to fully understand the working mechanism of View, the event distribution process, and the specific distribution objects.

MotionEvent object#

The event distribution in Android is based on the MotionEvent object. MotionEvent encapsulates many functions related to the position of various events and various related event types. Each MotionEvent contains a series of actions. For example, when a finger touches the screen, the system will generate a series of touch event objects. Each touch event object represents a different action, such as pressing, sliding, lifting, etc. These actions correspond to specific events such as ACTION_DOWN, ACTION_MOVE, ACTION_UP, etc. This series of events generally starts with the ACTION_DOWN event, followed by several ACTION_MOVE events, and finally ends with ACTION_UP. In addition, if the event is intercepted, it will also trigger the ACTION_CANCEL event. In short, the object of Android event distribution is the MotionEvent object. After the MotionEvent object is generated, the system will distribute this event to the view that can consume it.

View event distribution#

The event distribution in Android actually refers to the event distribution of View. The event distribution of View mainly includes the following three methods:

  1. dispatchTouchEvent()
  2. interceptTouchEvent()
  3. onTouchEvent()

These three methods correspond to event distribution, event interception, and event handling, respectively. In addition, View does not have the interceptTouchEvent() method. On the one hand, View does not have other child views that need to intercept events. On the other hand, it can be understood that if the interceptTouchEvent() method of View returns true, the event itself is intercepted by the View, and whether to consume it or not is the responsibility of onTouchEvent(). In any case, View does not consider event interception.

The event distribution in Android starts with the dispatchTouchEvent() method of the Activity. It is passed through a series of dispatchTouchEvent() methods of ViewGroup. If the current ViewGroup does not intercept the event, it continues to distribute the event to the child views, and so on until it is handled by a certain view. If no view handles the event, when the event is passed from the parent view to the deepest level view, the event will be passed back to the parent view and finally handled by the onTouchEvent() method of the Activity.

If the current ViewGroup intercepts the event, the event will not be distributed to the child views, but the onTouchEvent() method of the ViewGroup will be called to handle the event. Of course, whether the event is handled or not depends on the return value of the corresponding onTouchEvent() method. If the onTouchEvent() method returns true, it means that the event is consumed, otherwise, if it returns false, it means that the event has not been consumed and will be handled by the onTouchEvent() method of the parent view. If the parent view does not handle it, it will finally be handled by the onTouchEvent() method of the Activity.

If an event is handled by a certain view during the distribution process, such as the ACTION_DOWN event, when this event is handled, the subsequent ACTION_MOVE and ACTION_UP events will be directly received by the view that handles this event. In other words, once an event is handled by a certain view, the subsequent series of events will not judge whether to intercept these events, but will directly receive them. This is because a complete event sequence always starts with the ACTION_DOWN event, followed by several ACTION_MOVE events, and finally ends with ACTION_UP.

Summary#

The main content of the Android event transmission mechanism is roughly as described above, but the actual distribution process is certainly more complicated. The next article will look at the Android event distribution mechanism from the perspective of source code.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.