<span id="Label3"></p>The event distribution mechanism for Android payroll doubling<p><p><br></p></p>1 Case Description Narrative<p><p>We often encounter sliding conflicts and click Collisions in development, such as sliding collisions between <span style="font-family:Times New Roman">ScrollView</span> <span style="font-family:宋体">and</span> the <span style="font-family:Times New Roman">ListView</span> <span style="font-family:宋体">. </span>the <span style="font-family:Times New Roman">button</span> <span style="font-family:宋体">Click event</span> <span style="font-family:宋体">in</span> the <span style="font-family:Times New Roman">ListView</span> and <span style="font-family:Times New Roman"></span> <span style="font-family:宋体">The Click event Conflict of the ListView Itself. These problems are often encountered in development, and are more difficult to deal with. The following is a detailed analysis</span> of the event distribution mechanism of the <span style="font-family:Times New Roman">View</span> <span style="font-family:宋体">, from the principle of figuring out exactly what caused the conflict event. </span></p></p><p><p><br></p></p><p><p></p></p>2 Case Study 2.1 conflict event replay<p><p></p></p><p><p>For example, The blue area is the <span style="font-family:Times New Roman">horizontal ScrollView</span><span style="font-family:宋体">, and</span> the <span style="font-family:Times New Roman">listView</span><span style="font-family:宋体">(yellow area)</span> is nested in the <span style="font-family:Times New Roman">scrollView</span> <span style="font-family:宋体">.</span> <span style="font-family:Times New Roman">ScrollVIew</span> <span style="font-family:宋体">need to swipe left and right, while</span> <span style="font-family:Times New Roman">List</span> The view needs to swipe up and down, this time assuming that you swipe the screen, who is going to handle the sliding event, in which case there will be a sliding conflict. You can swipe both to achieve both. It is important <span style="font-family:Times New Roman"></span> to understand <span style="font-family:宋体">the event distribution mechanism of the VIEW. </span></p></p><p><p></p></p><p><p><span style="font-family:宋体"></span></p></p><p><p>Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "></p></p><p><p><span style="font-family:宋体"></span></p></p>2.2 Understanding the events distributed<span style="font-family:Arial"><span style="font-family:Arial">motionevent</span></span><p><p></p></p><p><p></p></p><p><p>To understand <span style="font-family:Times New Roman"></span> <span style="font-family:宋体">the event distribution mechanism for VIEW. We need to understand</span> <span style="font-family:Times New Roman">the motionevent</span> <span style="font-family:宋体">incident first, because</span> the <span style="font-family:宋体">event distribution of View is in fact the distribution of</span> <span style="font-family:Times New Roman">motionevent</span> <span style="font-family:Times New Roman"></span> <span style="font-family:宋体">. </span></p></p><p><p></p></p><p><p>In a series of events that occur after the finger touches the screen, typical event types are as follows</p></p><p><p>Action_down <span style="font-family:宋体">Finger just touching the screen</span></p></p><p><p>Action_move <span style="font-family:宋体">finger swipe on the screen</span></p></p><p><p>Action_up <span style="font-family:宋体">the moment the finger is released from the screen</span></p></p><p><p>Under normal circumstances, a finger-touch screen behavior triggers a series of Click Events. The following two cases</p></p><p><p>Click on the screen to release, the sequence of events is <span style="font-family:Times New Roman">down-up;</span></p></p><p><p>Click on the screen with a sliding distance and release <span style="font-family:Times New Roman">Down-move-move-move</span>...-up</p></p><p><p>The <span style="font-family:Times New Roman">MOVE</span> <span style="font-family:宋体">event will always be triggered, which is a complete sequence of events. </span></p></p><p><p>At the same time, the <span style="font-family:Times New Roman">xy</span> coordinates <span style="font-family:宋体">of click events can be obtained</span> by <span style="font-family:Times New Roman">motionevent</span> objects, and the <span style="font-family:宋体">system provides two sets of methods</span> <span style="font-family:Times New Roman">g</span>etx/gety and <span style="font-family:Times New Roman">get </span>Rawx/getrawy to return the <span style="font-family:Times New Roman">xy</span> coordinates relative to the upper- <span style="font-family:宋体">left corner</span> of the current <span style="font-family:Times New Roman">View</span> <span style="font-family:宋体">and the XY in relation to the Upper-left corner of the screen</span> <span style="font-family:Times New Roman"></span> , respectively <span style="font-family:宋体">coordinates, the filter inference for very many events requires both sets of METHODS. </span></p></p><p><p></p></p>2.3 motionevent<span style="font-family:黑体"><span style="font-family:黑体">the delivery rules</span></span><p><p></p></p><p><p></p></p><p><p>The distribution of the so-called Click event is actually the process of distributing the <span style="font-family:Times New Roman">Motion</span> <span style="font-family:宋体">event events, that is, when an</span> <span style="font-family:Times New Roman">M</span>otonevent is Generated. The system needs to pass this event to a detailed <span style="font-family:Times New Roman">View</span><span style="font-family:宋体">. And the process of delivery is the process of distribution, click the event distribution process by three very important methods to jointly complete:</span><span style="font-family:Times New Roman">dis</span>patchtouchevent,<span style="font-family:Times New Roman">onintercepttouchevent</span><span style="font-family:宋体">, </span> <span style="font-family:Times New Roman">ontouchevent</span><span style="font-family:宋体">. </span></p></p>2.3.1 Public boolean dispatchtouchevent (motionevent ev)<p><p>This method is used to distribute the event, assuming that the event can be passed to the current <span style="font-family:Times New Roman">view</span><span style="font-family:宋体">, then this method must be called, and the result will be returned by the current</span> <span style="font-family:Times New Roman">view</span> <span style="font-family:宋体">'s</span> <span style="font-family:Times New Roman">ontouchevent </span> <span style="font-family:宋体">and</span> <span style="font-family:Times New Roman"></span> <span style="font-family:宋体"></span> <span style="font-family:Times New Roman"></span> <span style="font-family:宋体">The influence of the Dispatchtouchevent method of the lower VIEW. Indicates whether the current event is Consumed. </span></p></p><p><p>See the <span style="font-family:Times New Roman">dispatchtouchevent</span> method in the <span style="font-family:Times New Roman">view</span> <span style="font-family:宋体">source code</span> below <span style="font-family:宋体">. </span></p></p><p><p></p></p><p><p>Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "><br></p></p><p><p>As can be seen from the source code, when this <span style="font-family:Times New Roman">View</span> is <span style="font-family:宋体">set</span> to <span style="font-family:Times New Roman">Setonclicklistener</span> <span style="font-family:宋体">and</span> <span style="font-family:Times New Roman">setontouchlistener</span><span style="font-family:宋体">, he returns </span> <span style="font-family:Times New Roman">true</span><span style="font-family:宋体">, which means that when</span> <span style="font-family:Times New Roman">the View</span> <span style="font-family:宋体">has its own click event, he consumes the</span> <span style="font-family:Times New Roman">motionevent</span> <span style="font-family:宋体">event. </span></p></p><p><p>Which means that the <span style="font-family:Times New Roman">OnTouch</span> <span style="font-family:宋体">and</span> <span style="font-family:Times New Roman">onClick</span> <span style="font-family:宋体">events take precedence</span> over <span style="font-family:Times New Roman">ontouchevent</span> <span style="font-family:宋体">.</span></p></p>2.3.2 Public boolean onintercepttouchevent (motonevent ev)<p><p>This method is called in the <span style="font-family:Times New Roman">dispatchtouchevent</span> <span style="font-family:宋体">method to infer whether an event is Intercepted. Assume that the current</span> <span style="font-family:Times New Roman">View</span> <span style="font-family:宋体">has been intercepted. Then in the same event sequence Therein. This method is not called again and returns a result indicating whether the current event is Blocked. </span></p></p><p><p>The following is <span style="font-family:Times New Roman"></span> a look at the <span style="font-family:宋体">method source code in viewgroup, it can be seen</span> that <span style="font-family:Times New Roman">viewgroup</span> <span style="font-family:宋体">default to all events will not be blocked. </span></p></p><br>2.3.3 Public boolean ontouchevent (motonevent ev)<p><p>Called in the <span style="font-family:Times New Roman">dispatchtouchevent</span> <span style="font-family:宋体">method to handle the click Event. The return result indicates whether the current event is consumed, assuming no consumption, and the current</span> <span style="font-family:Times New Roman">View</span> cannot receive the event again in the same sequence of events <span style="font-family:宋体">. </span></p></p><p><p></p></p>The relationship between the three 2.3.4<p><p>Here we analyze the relationship between the three of them through a pseudo-code.</p></p><br><p><p>Take a general look at the delivery rules for a click event from the above code: for a root <span style="font-family:Times New Roman">viewgroup</span> <span style="font-family:宋体">. After the Click event has been Generated. It will be passed to him first, and then his</span> dispatchtouchevent will be called, assuming that the <span style="font-family:Times New Roman">viewgroup</span> <span style="font-family:宋体"></span> onintercepttouchevent method returns <span style="font-family:Times New Roman">true </span> <span style="font-family:宋体">It means that he is going to intercept the current event, and this event will be given to the</span> <span style="font-family:Times New Roman">ViewGroup</span> <span style="font-family:宋体">to deal with. His</span> <span style="font-family:Times New Roman"></span>on TouchEvent <span style="font-family:宋体">method will be called, assuming</span> that the <span style="font-family:Times New Roman">viewgroup</span> <span style="font-family:宋体"></span> onintercepttouchevent returns <span style="font-family:Times New Roman"> False</span><span style="font-family:宋体">, he does not intercept, and the current event continues to pass to his child elements. The Dispatchtouchevent method of the child element is then</span> Called. So repeat until the event is finally processed</p></p>2.4 Event Distribution Process<p><p>Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "></p></p><p><p></p></p><p><p><span style="color:#000000">When a click event is generated, his pass-through process follows, for example, the following order:<span style="font-family:Consolas">Activity-window-view</span></span></p></p><p><p><span style="color:#000000">The event is first passed to the <span style="font-family:Consolas">activity</span><span style="font-family:宋体">, then</span> the <span style="font-family:Consolas">activity</span> is <span style="font-family:宋体">passed to</span> the <span style="font-family:Consolas">window</span><span style="font-family:宋体">, and the last</span> <span style="font-family:Consolas">window</span> <span style="font-family:宋体">when passed to the top-level</span> <span style="font-family:Consolas">View</span> <span style="font-family:宋体">. The top</span> <span style="font-family:Consolas">View</span> is <span style="font-family:宋体">received after the Event. Events are distributed according to the distribution mechanism of the Event. </span></span></p></p><p><span style="color:#000000">for <span style="color:#000000">instance, when a <span style="font-family:Consolas">View</span> <span style="font-family:宋体">'s</span> <span style="font-family:Consolas">ontouchevent</span> <span style="font-family:宋体">returns</span> <span style="font-family:Consolas">false</span>, the on of<span style="font-family:宋体">his parent container</span> <span style="font-family:Consolas"> </span></span></span><span style="color:#000000"><span style="color:#000000">TouchEvent</span></span><span style="color:#000000"><span style="font-family:consolas" will be called, and so on, all of the elements do not handle event. then this event finally handed over to <span>activity </span> <span style="font-family: arial"> </span> <span style=" Font-family:consolas ">ontouchevent </span> <span style=" font-family: Song body "> processing. It's like the leader distributes the task to the manager, the manager assigns the task to the team leader, and the team leader distributes the task to the group member (similar to the </span> <span style="font-family:consolas">motionevent </span> <span style="font-family: song body"> from </span> <span style="font-family:consolas">viewgroup </span> <span style="font-family: arial"> has been distributed to the topmost child </span> <span style="font-family:consolas">view </span> <span style="font-family: Song body">). Assume that the group cannot handle (top-level </span> <span style="font-family:consolas">view </span> <span style="font-family: arial" and < span> <span style="font-family:consolas">ontouchevent </span> <span style="font-family: arial"> return </span> <span style="font-family:consolas">false </span> <span style="font-family: Song body">) will be feedback to the team leader, the group can not handle the feedback to the Manager. The manager can not handle the feedback to the Leader. The distribution mechanism for </span> <span style="font-family:consolas">view </span> <span style="font-family: arial" events is so similar. < span></span></span></span></p><p><p><span style="color:#000000"><span style="font-family:宋体"></span></span></p></p>2.5 Some conclusions of the event distribution<p><p></p></p><p><p><span style="color:#000000">Here are some conclusions that can better help us understand the entire delivery mechanism, which is found on the internet and is the result of source code Parsing.</span></p></p><p><p><span style="color:#000000">(1)</span> <span style="color:#000000">the same sequence of events is from <span style="font-family:Consolas">Mo</span> </span> <span style="color:#000000">tionevent</span> <span style="color:#000000"> <span style="font-family:Consolas"></span> <span style="font-family:等线">down starts at</span> <span style="font-family:Consolas"></span> the end of the up <span style="font-family:等线">. The middle contains a variable number of</span> <span style="font-family:Consolas">Move</span> <span style="font-family:等线">events. </span></span></p></p><p><p>(2) under normal circumstances, an event sequence can only be intercepted and consumed by a view, because once an element intercepts an event, all events in the same sequence of events are handed over to it, and the Onintercepttouchevent method of the element is no longer called.</p></p><p><p>(3) Once a view has started processing Events. Assuming that it does not consume the Action_down event (ontouchevent returns false), other events in the same sequence of events are no longer given to it for Processing. And the event will once again be handed over to its parent container to handle (calling the parent Container's ontouchevent method), just like the first thing the leader gives you is not handled well, then the leader might not let you continue to deal with it, assuming it consumes Action_down events. But without consuming other types of events, the click event disappears and the Ontouchevent method of the parent container is not called. The current view still receives a possible event, but these events are eventually passed on to the activity Handler.</p></p><p><p>(4) Once a view has been decided to intercept. Then this sequence of events can only be handled by him, and his onintercepttouchevent will not be called again, which means that when a view decides to intercept an Event. Then the system will hand over the other methods in the same sequence of events directly to him, so he will not call the View's onintercepttouchevent to ask if he wants to intercept.</p></p><p><p>(5) viewgroup default does not intercept no matter what event, can see the source code</p></p><p><p><span style="color:#000000"></span></p></p><p><p><span style="color:#000000">(6)</span> <span style="color:#000000">View <span style="font-family:等线">has no</span> </span> onintercepttouchevent method, and once a click event is passed to it, its Ontouchevent method is Called. Can be understood from the pseudo-code of 2.3.4.</p></p><p><p></p></p><p><p> <span style="color: #000000"> (7) </span> <span style="color: #000000">view<span style="font-family: equal line" < span> <span style="font-family:consolas">on </span> </span> <span style="color: #000000">touchevent </span> <span style="font-family:consolas" the event is consumed by default (return <span>true </span> <span style="font-family: equal Line" ) , unless it is not clickable. < span></span></span></p></p><p><p><span style="color:#000000">(8)</span> <span style="color:#000000">the process of passing an event is made out of the Inward. That is, the event is always passed to the parent element First. The parent element is then distributed to the child <span style="font-family:Consolas">View</span><span style="font-family:等线">through the</span> <span style="font-family:Consolas">request</span></span><span style="color:#000000">disallowintercepttouchevent</span> <span style="color:#000000">method</span> <span style="color:#000000">( </span> <span style="color:#000000">requests that the parent element disagree with the interception Event. This means that a request to pass an event to itself is</span> <span style="color:#000000"></span> <span style="color:#000000">able to intervene in the child Element's event distribution process for the parent element, except for the <span style="font-family:Consolas">action_down</span> <span style="font-family:等线">event. </span></span></p></p>3 resolution Process<p><p>Looking back at the beginning of the sliding conflict, let's go back and comb the distribution mechanism of the Event.</p></p><p><span style="color:#000000"><span style="color:#000000">Clicking an event first arrives at the top <span style="font-family:Consolas">View</span><span style="font-family:宋体">(usually a</span> <span style="font-family:Consolas">viewgroup</span><span style="font-family:宋体">)</span><span style="font-family:Consolas">and</span> <span style="font-family:宋体">calls</span> <span style="font-family:Consolas">viewgroup</span> <span style="font-family:宋体">of the</span> <span style="font-family:Consolas">dispatchtouchevent</span> <span style="font-family:宋体">method, and then the logic is this: suppose the top-level</span> <span style="font-family:Consolas">ViewGroup</span> <span style="font-family:宋体">Intercept Events</span> <span style="font-family:Consolas"></span> </span> on</span><span style="color:#000000"><span style="color:#000000">intercepttouchevent</span></span><span style="color:#000000"><span style="color:#000000">returns <span style="font-family:Consolas">True</span>when the<span style="font-family:宋体">event is handled by</span> <span style="font-family:Consolas">ViewGroup</span> <span style="font-family:宋体">, assuming</span> that the <span style="font-family:Consolas">viewgroup</span> <span style="font-family:宋体"></span> <span style="font-family:Consolas"> The Montouchlistener</span> <span style="font-family:宋体">is set</span> <span style="font-family:Consolas"></span> </span> to on</span><span style="color:#000000"><span style="color:#000000">Touch</span></span><span style="color:#000000"><span style="color:#000000">will be called, otherwise the <span style="font-family:Consolas">ontouchevent</span> <span style="font-family:宋体">will be Called. Assuming that the top-level</span> <span style="font-family:Consolas">viewgroup</span> <span style="font-family:宋体">does not intercept the event, the event is passed to the word view on the chain of the Click event where</span> <span style="font-family:Consolas"></span>the<span style="font-family:宋体">child</span> <span style="font-family:Consolas">view</span> <span style="font-family:宋体"></span> <span style="font-family:Consolas">dispatchtouchevent</span> <span style="font-family:宋体">is called, and then loops and so on, the entire event is Distributed. </span></span></span></p><p><p></p></p>3.1 Causes of conflict<p><p><span style="color:#000000">Once we understand the distribution mechanism of <span style="font-family:Consolas">view</span> , <span style="font-family:宋体">we'll look at the sliding conflict events mentioned at the Beginning. Because the first to receive the sliding event is the top</span> <span style="font-family:Consolas">viewgroup</span><span style="font-family:宋体">, namely</span> <span style="font-family:Consolas">ScrollView</span><span style="font-family:宋体">. We were able to look at</span> <span style="font-family:Consolas"></span> <span style="font-family:宋体">The source code of ScrollView and found the interception event</span> </span> <span style="color:#000000; background:rgb(192,192,192)">onintercepttouchevent</span> <span style="color:#000000; background:rgb(192,192,192)">method to intercept the sliding event. </span> <span style="color:#000000">ScrollView <span style="font-family:宋体">handles the swipe event,</span> the <span style="font-family:Consolas">listView</span> <span style="font-family:宋体">cannot receive the sliding Time. Causes</span> the <span style="font-family:Consolas">listView</span> <span style="font-family:宋体">to fail the normal implementation of the Slide. </span></span></p></p><p><p></p></p>3.2 Workaround<p><p>After knowing the reason, it is very good to solve the Problem.</p></p><p><p><span style="color:#000000">When we swipe left and right, we need to let the <span style="font-family:Consolas">horizontalscrollview</span> slide <span style="font-family:宋体">, and when you swipe up or down, you need to slide the</span> <span style="font-family:Consolas">ListView</span> <span style="font-family:宋体">. So we can</span> </span> figure out who will intercept the slide based on whether it is sliding horizontally or vertically. We can <span style="font-family:Times New Roman"></span> <span style="font-family:宋体">get the horizontal and vertical sliding distance by the coordinates returned by the Getx/gety method, and analyze whether the horizontal or vertical slide is inferred by the distance of the slide, assuming that the horizontal distance is greater than the vertical distance and the horizontal slide, otherwise it will slide vertically. </span></p></p><p><p></p></p><p><p></p></p><p><p>Imagine that the parent container <span style="font-family:Times New Roman">ScrollView</span> <span style="font-family:宋体">in the interception method to infer horizontal sliding. The slide event is then Intercepted. otherwise, no interception is Done. Let the child element</span> <span style="font-family:Times New Roman">listView</span> <span style="font-family:宋体">be Processed. </span></p></p><p><p>This will solve the sliding conflict problem.</p></p><p><p>We were able to rewrite the <span style="font-family:Times New Roman">ScrollView</span> <span style="font-family:宋体">on</span> <span style="font-family:Times New Roman"></span>intercepttouchevent method. Do the corresponding interception within the Internal.</p></p><p><p></p></p><p><p>Pseudo Code:</p></p><p><p></p></p><p><p>Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "><br></p></p>4 Resolving results<p><p>All of the sliding conflicts can be intercepted according to the corresponding business requirements in the above method. The only simple event conflict is given Here. But the solution to this problem is still inseparable from the above METHODS.</p></p>5 Summary<p><p><span style="color:#000000">Understand the <span style="font-family:Consolas">View</span> <span style="font-family:宋体">of the event distribution mechanism on the right to solve the problems encountered in the development of various strange conflicts. </span></span></p></p><br><span style="color:#000000"><span style="color:#000000"></span></span><br> <p><p>Event distribution mechanism for Android view</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service