banner
jzman

jzman

Coding、思考、自觉。
github

Android Event Distribution Basics

PS: Try your best and prepare for the worst. The laziness you once indulged in will eventually come back to slap you in the face.

The Android event dispatch mechanism is a relatively important part of the system. This article aims to summarize some knowledge about Android event dispatch and will be divided into four articles. The content is as follows:

image

This is the first article, which aims to provide a general overview of the event dispatch process so that readers can have a preliminary understanding of event dispatch. The content is as follows:

  1. View and ViewGroup
  2. MotionEvent object
  3. Event dispatch of View
  4. Summary

View and ViewGroup#

All components in an Android application inherit from the View class, which is the base class for all UI components in Android. ViewGroup is an important subclass of View and is commonly used as a container for other Views. It can contain both regular Views and 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, which can contain various Views, and this View can also be a ViewGroup, and so on.

In Android devices, various gestures such as swiping, dragging, and tapping are used for interaction. Different Views are at different levels, so how do we make a specific View respond to a certain operation correctly? Will there be conflicts between different Views? The answer is yes. To solve such problems, it is necessary to have a thorough understanding of the working mechanism of View, the event dispatch process, and the specific dispatch objects.

MotionEvent object#

In Android, event dispatch is based on the MotionEvent object. MotionEvent encapsulates many functions related to the position of various events and various event types. Each MotionEvent contains a series of actions. For example, when a finger touches the screen, a series of touch event objects will be generated, and 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 ACTION_DOWN, followed by several ACTION_MOVE events, and ends with ACTION_UP. In addition, if the event is intercepted, an ACTION_CANCEL event will also be triggered. In summary, the object of Android event dispatch is the MotionEvent object. After the MotionEvent object is generated, the system will dispatch this event to the View that can consume it.

Event dispatch of View#

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

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

These three methods correspond to event dispatch, 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 dispatch in Android starts with the dispatchTouchEvent() method of Activity, and then passes through a series of dispatchTouchEvent() methods of ViewGroup. If the current ViewGroup does not intercept the event, it will continue to dispatch 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 Activity.

If the current ViewGroup intercepts the event, the event will not be dispatched to the child Views, but will be handled by its onTouchEvent() method. 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 is not 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 Activity.

If an event is handled by a certain View during the dispatch process, such as the ACTION_DOWN event, once 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 check whether to intercept these events, but will directly receive them. This is because a complete event sequence always starts with ACTION_DOWN, followed by several ACTION_MOVE events, and ends with ACTION_UP.

Summary#

The main content of the Android event dispatch mechanism is roughly as described above, but the actual dispatch process is more complex. The next article will examine the Android event dispatch mechanism from the perspective of source code.

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