AndroidView event distribution mechanism (2)

           We continue the Android View distribution mechanism mentioned in the last article.

           First, let's take another chestnut:

           When a click event is generated, its delivery process follows the following sequence: Activity->Window->View, that is, the event is always delivered to Activity first, Acitivity and then to Window, and finally window to the top-level View, which is received by the top-level View. After the event, the event will be distributed according to the event distribution mechanism. Consider another case, if a View's OnTouchEvent returns false, then its parent container's OnTouchEvent will be called, and so on. If all elements do not handle this event, then this event will eventually be passed to the Activity for processing, that is, the Activity's OnTouchEvent method will be called.

         Regarding the mechanism of event delivery, here are some conclusions from which the entire delivery mechanism can be better understood, as follows:

         1. The same sequence of events refers to a series of events generated in the process from the moment the finger touches the screen to the moment the finger leaves the screen. This sequence of events starts with the down event, and there are countless The move event ends with the up event.

         2. Under normal circumstances, an event sequence can only be intercepted and consumed by one View. That is to say, all events in the same event sequence can only be handled by one View. However, through special means, a View can forcibly pass events that should be handled by itself to other Views for processing through onTouchEvent.

         3. Once a View decides to intercept, this event sequence can only be handled by it, and its OnInterceptTouchEvent will not be called again. This is also easy to understand, that is, when a View decides to intercept an event, the system will directly hand over other methods in the same event sequence to it for processing, so it will no longer call the View's OnInterceptTouchEvent to ask. Whether it is about to intercept.

         4. ViewGroup does not intercept any events by default.

         5. View has no OnInterceptTouchEvent method. Once the event is passed to it, its onTouchEvent will be called.

         6. View's OnTouchEvent consumes events by default (returns true), unless it is not clickable (clickable and longclickable are both false). View's longclick attribute defaults to false, and the clickable attribute depends on the situation. For example, Button's is true, and TextView's default is false.

         7. The enable property of View does not affect the default return value of onTouchEvent. Even if a View is disabled, as long as one of its clickable and longclickable is true, its onTouchEvent will return true.

         8. The event delivery process is from outside to inside, that is, the event is always delivered to the parent element, and then distributed by the parent element to the child View. The requestDisallowInterceptTouchEvent method can interfere with the event distribution process of the parent element in the child element, but the ACTION_DOWN event except.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325392976&siteId=291194637