Event distribution mechanism of Android View (1)

      I have been developing Android for 2 years, and I have a lot of development experience, but to be honest, I still know a little bit about the underlying things. Today, I will tell you about the click event distribution about Android View that I have seen.   

     Before introducing the delivery rules of click events, we must first understand that the object we want to analyze is MotionEvent, that is, click events. The so-called click event distribution is actually the distribution process of the MotionEvent event, that is, when a MotionEvent is generated, the system needs to pass the event to a specific View, and this transmission process is the distribution. The distribution process of the click event is completed by three important methods: diapathTouchEvent, onInterceptTouchEvent, onTouchEvent. Let's first introduce the following methods:

         public boolean dispatchTouchEvent(MotionEvent ev): used for event distribution. If the event can be passed to the current View, then this method will be called, and the returned result is affected by the OnTouchEvent of the current View and the dispatchTouchEvent method of the subordinate View, indicating whether the current event is consumed.

         public boolean onInterceptTouchEvent (MotionEvent ev): Called inside the above method to determine whether to intercept an event. If the current View intercepts an event, then in the same event sequence, this method will not be called and the result will be returned Indicates whether to intercept the current event.

         public boolean onTouchEvent(MotionEvent ev): Called  in the dispatchTouchEvent method to process the click event, and the returned result indicates whether the current event is consumed. If not, the current View cannot receive the event again in the same time series

         Next, let's take a chestnut to explain the specific calling relationship of these three methods: for a root ViewGroup, after the click event is generated, it will be passed first, then its dispatchTouchEvent will be called, if the dispatchTouchEvent method of this ViewGroup Returning true means that he wants to intercept this event, and then this event will be handed over to ViewGroup for processing, that is, its onTouchEvent method will be called; if his

The onInterceptTouchEvent method returns false, which means that it does not intercept the current event. At this time, the current event will continue to be passed to its child elements, and then the dispatchTouchEvent method of the child element will be called, and so on until the event is finally processed.

         When a View needs to process an event, if it sets OnTouchListener, then its Ontouch method of OnTouchListener will be called back. If it returns true, onTouchEvent will not be called, otherwise it will be called. It can be seen that the priority of the OnTouchListener set for the View is higher than that of the onTouchEvent. However, in the onTouchEvent method, if there is an OnClickListener currently set, then his OnClick will be called. That is, the priority of the OnClickListener method we usually use is the lowest.


         

Guess you like

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