In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to talk to you about how to understand the Android event distribution mechanism, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
Preface
Sliding conflicts (such as ScrollView or the nesting of SliddingMenu and ListView) are often encountered in android development, which can only be solved by an in-depth understanding of android event response mechanism, which has become an indispensable knowledge for android developers.
Common methods related to event response
During finger-to-screen contact, the user generates a series of events through the MotionEvent object, which has four states:
MotionEvent.ACTION_DOWN: the moment your finger presses the screen (the beginning of all events)
MotionEvent.ACTION_MOVE: fingers move on the screen
MotionEvent.ACTION_UP: the moment your finger leaves the screen
MotionEvent.ACTION_CANCEL: cancel gestures, usually generated by the program, not by the user
Events in Android, such as onClick, onLongClick,onScroll, onFling, and so on, are made up of many Touch events (one ACTION_DOWN, n ACTION_MOVE,1 ACTION_UP).
The android event response mechanism is first distributed (received by the external View and then passed to the smallest View in its inner layer) and then processed (from the smallest View unit (event source) to the outer layer.) In the form of.
The complexity is shown in that you can control whether each layer of events continue to be delivered (distribute and intercept collaborative implementations), as well as the specific consumption of events (event distribution also has event consumption capacity).
Three important functions involved in android event handling
Event distribution: public boolean dispatchTouchEvent (MotionEvent ev)
When an event is monitored, it is first captured by Activity and enters the event distribution process. (because activity does not have event interception, View and ViewGroup do.) events are passed to the outermost View's dispatchTouchEvent (MotionEvent ev) method, which distributes events.
Return true: indicates that all events have been digested internally in the View.
Return false: events are no longer distributed at this layer and are consumed by the onTouchEvent method of the upper-level control (if the control at this layer is already Activity, then the event will be consumed or handled by the system).
If the event distribution returns the system default super.dispatchTouchEvent (ev), the event will be distributed to the event interceptor onInterceptTouchEvent method at this layer for processing
Event interception: public boolean onInterceptTouchEvent (MotionEvent ev)
Return true: means to intercept the event and leave the intercepted event to the onTouchEvent of the control at this layer
Return false: it means that the event is not intercepted and the event is successfully distributed to the child View. And processed by the dispatchTouchEvent of the sub-View.
If super.onInterceptTouchEvent (ev) is returned, the default means to intercept the event and pass it to the onTouchEvent method of the current View, just like return true.
Event response: public boolean onTouchEvent (MotionEvent ev)
In the case where dispatchTouchEvent (event distribution) returns super.dispatchTouchEvent (ev) and onInterceptTouchEvent (event intercept returns true or super.onInterceptTouchEvent (ev), the event is passed to the onTouchEvent method, which responds to the event.
If return true, it means that onTouchEvent consumed the event after it finished processing the event. This is the end of the event.
If return fasle means no response to the event, the event will continue to be passed to the onTouchEvent method of the upper View until the onTouchEvent method of a View returns true. If the top View still returns false, then the event is considered not to be consumed, then the current View cannot receive the event again in the same event series, and the event will be handed over to Activity's onTouchEvent for processing.
If return super.dispatchTouchEvent (ev), it does not respond to the event, and the result is the same as return false.
As can be seen from the above process, no matter whether dispatchTouchEvent returns true or false, the event is no longer distributed. Only when it returns super.dispatchTouchEvent (ev) does it indicate that it has the desire to distribute to the lower layer. But whether it can be successfully distributed or not, it needs to be audited by the event blocking onInterceptTouchEvent. Whether the event is passed up or not is determined by the return value of the onTouchEvent.
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.