The original article was first published in the wechat public number: Jzm-blog, welcome to pay attention to the exchange!

Android event distribution mechanism is an important part of the content, sum up some knowledge of Android event distribution, plan to be divided into four articles to write, the content is as follows:

This is the first chapter, hoping to describe the general distribution process from the overall process, so that readers can have a preliminary understanding of the event distribution, the content is as follows:

  1. The View and ViewGroup
  2. The MotionEvent object
  3. View event distribution

The View and ViewGroup

The View class is the base class for all UI components in Android. An important subclass of View is ViewGroup, which is usually used as a container for other Views. It can contain ordinary views or other viewgroups. The relationship between View and ViewGroup together forms the structure of the whole View tree. For example, the LinearLayout is not only a View but also a ViewGroup. It can have all kinds of views in it, and of course that View can be a ViewGroup, and so on.

In Android devices, operations mainly rely on a variety of gestures, such as sliding, dragging, clicking and other operations. This series of operations can be very convenient to interact with Android devices. So when we operate when how to correctly let the specific View response to a certain operation, different views will slide conflict between it, the answer is yes, to solve such a problem must fully understand the working mechanism of View, the distribution process of various events and the specific distribution object.

The MotionEvent object

Events in Android are distributed by MotionEvent objects, which encapsulate many functions related to the location of various events and various related event types. Each MotionEvent contains a series of actions, such as the moment when a finger touches the screen. The system will generate a series of touch event objects, each of which represents the different actions, such as pressing down, sliding, lifting and so on. These actions respectively correspond to ACTION_DOWN, ACTION_MOVE, ACTION_UP and other specific events. This sequence of events typically starts with an ACTION_DOWN event, then several ACTION_MOVE events, and finally an ACTION_UP event. In addition, an ACTION_CANCEL event is triggered if the event is intercepted. In short, the object that Android events distribute is the MotionEvent object, and when the MotionEvent object is generated, the system distributes the event to the View that can eventually consume the event.

View event distribution

Event distribution in Android actually refers to the event distribution of View, View event distribution is mainly the following three methods:

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

In addition, there is no interceptTouchEvent() method in the View. There are no other sub-views in the View that do not need to intercept events. The View interceptTouchEvent() method returns true. The event itself is intercepted by the View. It is up to the onTouchEvent() to consume it or not.

Android event distribution starts with the Activity’s dispatchTouchEvent() method and passes a series of dispatchTouchEvent() methods to the ViewGroup. If the current ViewGroup does not intercept the event, If no View handles the event, when the event is passed from the parent View to the child View at the deepest level, the event will be sent back to the parent View. This is handled by the Activity’s onTouchEvent().

If the current ViewGroup intercepts the event, instead of distributing the event to the child View, it calls its onTouchEvent() method to handle the event. Whether the event is handled depends on the return value of the corresponding onTouchEvent() method. If the onTouchEvent() method returns true, the event was consumed; if the onTouchEvent() method returns false, the event was not consumed and will be handled by the parent View’s onTouchEvent() method. The Activity’s onTouchEvent() is finally handled.

If an event is handled by a View during distribution, such as an ACTION_DOWN event, then subsequent ACTION_MOVE and ACTION_UP events will be received directly by the View that handled the event. That is, once an event has been processed by a View, the subsequent sequence of events does not determine whether to intercept the event, but rather receives it directly, because a complete sequence of events always starts with ACTION_DOWN events, then ACTION_MOVE events, and finally ends with ACTION_UP.

conclusion

The main content of the Android event distribution mechanism is described above, but the actual distribution process is definitely complicated. The next article will look at the Android event distribution mechanism from a source point of view. Personal wechat public number: practice, can exchange learning together!