In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to implement the event distribution mechanism in Android. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
I. brief introduction
You might ask, "Why do I have to know about View's event distribution mechanism?" . Because in the actual development process, we often encounter multi-layer View nesting each other, when sliding on a certain View, it is particularly insensitive, or even unable to slide. The resolution of this kind of sliding conflict requires us to clearly grasp the event distribution mechanism of View. Then let's explain the whole event mechanism of View in detail.
Android encapsulates the event of View into the class MotionEvent, which is also the parameter public boolean onTouchEvent (MotionEvent event) that is called back to us in the listening touch event. Usually we are mainly concerned with the following types of events:
MotionEvent.ACTION_DOWN
The first event when we press our finger on the screen is ACTION_DOWN, which means the beginning of the event.
MotionEvent.ACTION_MOVE
When we press our fingers on the screen and slide across the screen, this event will be triggered again and again.
MotionEvent.ACTION_UP
This event is triggered when our fingers are lifted from the screen.
MotionEvent.ACTION_CANCEL
This event is a little more complicated, take a chestnut: when our outer View passes the event to the inner View for processing, the intercepting method of the outer View usually returns false, but when a condition is triggered, the outer View wants to handle the next event on its own, so it intercepts the event distribution, and the inner View will receive the event from ACTION_CANCEL.
MotionEvent.ACTION_OUTSIDE
We don't often use this event. Consider this scenario. We have another Diallog pop-up when we press a screen other than Dialog to disappear the Dialog. At this point, you can consider listening for this event. To use this event, we must set a Flag:FLAG_WATCH_OUTSIDE_TOUCH to the current Window.
Let's introduce several methods related to event distribution:
DispatchTouchEvent (MotionEvent event)
This method is used to handle the downward distribution event logic. We know by observing the code in the ViewGrope source code that this method has many details. The logic we are more concerned about is that this method will first determine whether the child View has called the disallowIntercept parent View to intercept the event. If not, the parent View will call onInterceptTouchEvent to determine whether it has intercepted the event. If it intercepts the event, it will call the parent View's own onTouchEvent method to handle the event. If there is no intercepted event, the event will continue to be distributed to the child View for processing.
OnInterceptTouchEvent (MotionEvent event)
Used to declare whether intercepting events continue to be distributed downwards, and if true is returned, the events will not continue to be distributed downwards, but will be handled by their own onTouchEvent methods.
OnTouchEvent (MotionEvent event)
Obviously, this is the way to handle events.
OnTouch (MotionEvent event)
This method is called back when we call back a setOnTouchListener, that is, when passing an event, determine whether there is a listening TouchListener before handing it to the onTouchEvent of the View itself, and if there is a priority to call the onTouch method of TouchListener.
Second, analyze the distribution events of View in detail
As we all know, the View of Android is tree-shaped, so when an event comes, it is usually distributed from the root. To facilitate our understanding, let's build an example like this:
Suppose we have a page with a ViewGroup An in the outermost layer and a ViewGroup B in B with a ViewGroup C in it.
Scenario 1:
Suppose we do not intercept the incident and do nothing to deal with it. When we click on View C, the Log we see shows the call sequence as follows:
A-> dispatchTouchEvent
A-> onInterceptTouchEvent
B-> dispatchTouchEvent
B-> onInterceptTouchEvent
C-> dispatchTouchEvent
C-> onInterceptTouchEvent
C-> onTouchEvent ACTION_DOWN
B-> onTouchEvent ACTION_DOWN
A-> onTouchEvent ACTION_DOWN
Since there is no View handling event, it will eventually be called back to Activity's onTouchEvent to handle it. From this scenario, we can know the process of the downward transmission of events and the process of dealing with the upward transmission of events.
Scenario 2:
Suppose we return true in the onTouchEvent of View B, click the event again and swipe, and we get the Log as follows:
A-> dispatchTouchEvent
A-> onInterceptTouchEvent
B-> dispatchTouchEvent
B-> onInterceptTouchEvent
C-> dispatchTouchEvent
C-> onInterceptTouchEvent
C-> onTouchEvent ACTION_DOWN
B-> onTouchEvent ACTION_DOWN
A-> dispatchTouchEvent
A-> onInterceptTouchEvent
B-> dispatchTouchEvent
B-> onTouchEvent ACTION_MOVE
A-> dispatchTouchEvent
A-> onInterceptTouchEvent
B-> dispatchTouchEvent
B-> onTouchEvent ACTION_UP
We found that in addition to the ACTION_DOWN event will be sent to C, subsequent events will not be sent because, when we find that the onTouchEvent of a certain layer of View returns true, there will be a flag bit indicating that subsequent events are handled by View, and subsequent events are no longer sent to sub-View until the flag bit is reset after the ACTION UP event.
Scenario 3:
Suppose we return true in the onInterceptTouchEvent of View B and click C again? We get the following Log record:
A-> dispatchTouchEvent
A-> onInterceptTouchEvent
B-> dispatchTouchEvent
B-> onInterceptTouchEvent
B-> onTouchEvent ACTION_DOWN
A-> onTouchEvent ACTION_DOWN
Compared with scenario 2, the event called down will not be sent to C, because there is no View to handle, so the subsequent events are canceled.
III. Summary
Through our actual operation and analysis of the source code, we found that the distribution process of our ViewGroup event is as follows:
The above is how to implement the event distribution mechanism in Android. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.