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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to achieve View event distribution in Android". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve View event distribution in Android".
(1) ViewGroup.dispatchTouchEvent (event)
Boolean dispatchTouchEvent (MotionEvent event) {int action = event.getAction (); / / determines whether ViewGroup intercepts touch events. When it is ACTION_DOWN or a child View that can receive touch events is found, it is up to onInterceptTouchEvent (event) to decide whether to intercept or not. In other cases, that is, ACTION_MOVE/ACTION_UP and no child View that can receive touch events is found, intercept directly. Boolean intercepted; if (action = = MotionEvent.ACTION_DOWN | | mFirstTouchTarget! = null) {intercepted = onInterceptTouchEvent (event);} else {intercepted = true;} / / if ViewGroup does not intercept touch events. Iterate through all the child View while ACTION_DOWN, looking for the child View that can receive the touch event. If found, set mFirstTouchTarget and jump out of the loop. Boolean alreadyDispatchedToNewTouchTarget = false; if (! intercepted) {if (action = = MotionEvent.ACTION_DOWN) {for (int I = childrenCount-1; I > = 0; iMub -) {if (! canViewReceivePointerEvents (child) | |! isTransformedTouchPointInView (x, y, child, null)) {continue;} if (dispatchTransformedTouchEvent (event, child)) {/ / find mFirstTouchTarget newTouchTarget = addTouchTarget (child); alreadyDispatchedToNewTouchTarget = true; break Event issuance and consumption. If no child View is found that can receive the touch event, the ViewGroup handles and consumes it. If a child View that can receive touch events is found, the child View recursively handles the touch event and consumption. Boolean handled = false; if (mFirstTouchTarget = = null) {handled = dispatchTransformedTouchEvent (event, null);} else {if (alreadyDispatchedToNewTouchTarget) {handled = true;} else {while (touchTarget) {handled = dispatchTransformedTouchEvent (event, child);}} return handled;} / / ViewGroup event. If there is no child View that receives the touch event, the touch event is sent by the parent class of the ViewGroup (that is, View). If the child is not empty, the touch event is sent by the child View, and the child View can be ViewGroup or View. Boolean dispatchTransformedTouchEvent (MotionEvent event, View child) {boolean handled; if (child = = null) {handled = super.dispatchTouchEvent (event);} else {handled = child.dispatchTouchEvent (event);} return handled;}
(2) View.dispatchTouchEvent (event)
/ / Touch event distribution for View. When mOnTouchListener is set externally, it is first consumed by mOnTouchListener.onTouch (event). If it is not consumed, it will be given to View's onTouchEvent (event) for consumption. The implementation of onTouchEvent is to execute the mOnClickListener.onClick () click event if mOnClickListener is set. The return value is true, which means consumption, otherwise it is not consumed. Boolean dispatchTouchEvent (MotionEvent event) {boolean result = false; if (mOnTouchListener! = null & mOnTouchListener.onTouch (this, event)) {result = true;} if (! result & & onTouchEvent (event)) {result = true;} return result;} boolean onTouchEvent (MotionEvent event) {performClick ();}
3. Summary
Summarize the event distribution and consumption process of ViewGroup:
The whole process consists of three parts: judging whether to intercept or not-> finding the child View that receives the touch event-> event delivery and consumption.
Determine whether to intercept:
(1) when ACTION_DOWN or non-ACTION_DOWN is found and the sub-View that receives the touch event is found, it is up to onInterceptTouchEvent (event) to decide whether to intercept
(2) if it is not ACTION_DOWN and the child View that receives the touch event is not found, it indicates that the touch event needs to be intercepted.
As explained here, there are two factors that affect whether ViewGroup can intercept touch events: whether the child View and onInterceptTouchEvent (event) that receive touch events are found. The process of finding the child View that receives the touch event only needs to be determined at the time of ACTION_DOWN. If the ACTION_DOWN is not found, then ACTION_MOVE and ACTION_UP must not be found, so the touch event is directly intercepted by ViewGroup. If the child View that receives the touch event is found, check the onInterceptTouchEvent (event) of ViewGroup in the case of ACTION_MOVE and ACTION_UP to see if it is intercepted.
Find the child View that receives the touch event:
(1) search in two cases: ACTION_DOWN and ViewGroup does not intercept.
(2) search method: traversing all sub-View, if the xy coordinate of touch event is within the range of some sub-View of the ViewGroup, the recursive distribution touch event operation is executed for the sub-View, and if a sub-View is found to handle the touch event (return true), then jump out of the cycle.
Explain the search criteria here. Finding the child View that receives the touch event obviously requires only the case of ACTION_DOWN, and it is not necessary to check both ACTION_MOVE and ACTION_UP, otherwise the operation will be repeated. If the ViewGroup has been intercepted, there is obviously no need to consider what happened to the sub-View.
Event distribution and consumption:
(1) two situations: ViewGroup distribution and consumption or sub-View distribution and consumption of ViewGroup
(2) if the sub-View that receives the Touch event is not found after the above two steps, then the ViewGroup sends and consumes, and the process of sending and calling is: ViewGroup.dispatchTouchEvent-> View.dispatchTouchEvent-> mOnTouchListener.onTouch-> onTouchEvent-> onClick
(3) if the sub-View that receives the touch event is found, the operation of recursively sending and consuming the touch event will be performed on the sub-View.
Add:
(1) in the source code, mFirstTouchEvent represents the sub-View that receives touch events.
(2) both steps 2 and 3 perform the operation of dispatchTransformedTouchEvent (event, child). Step 2 is only to find the sub-View that receives touch events, and the main purpose of step 3 is to distribute and consume events. If the method has been executed for a child View in step 2, it will not be repeated in step 3. Personally understand, do not know whether there is a mistake.
4. Conclusion
(1) callback method
ViewGroup:dispatchTouchEvent-> onInterceptTouchEvent-> onTouchEvent
View: dispatchTouchEvent-> onTouch
(2) call order
Action execution order: ACTION_DOWN-> ACTION_MOVE-> ACTION_UP
ViewGroup: dispatchTouchEvent-> onInterceptTouchEvent-> onTouchEvent ()
View: dispatchTouchEvent-> onTouchEvent
Event distribution delivery order: Parent View-> Child View
ViewGroup1.dispatchTouchEvent-> ViewGroup2.dispatchTouchEvent
-> View3.dispatchTouchEvent
(this is followed by View3.onTouchEvent.)
Event consumption delivery order: Child View-> Parent View
View3.onTouchEvent-> ViewGroup2.onTouchEvent
-> ViewGroup1.onTouchEvent
Thank you for reading, the above is the content of "how to achieve View event distribution in Android". After the study of this article, I believe you have a deeper understanding of how to achieve View event distribution in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.