[Learning summary] 5, Android ViewGroup in the event of the transmission mechanism (ii)

Source: Internet
Author: User

here is the connection for the first articleThe transfer mechanism of events in Android ViewGroup (i)

A detailed explanation of onintercepttouchevent and ontouchevent.

1  Public classMainactivityextendsActivity {2 Group1 group1;3 Group2 group2;4 Mytextview myTv;5 6     /**Called when the activity is first created.*/7 @Override8      Public voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten  One         //--group1 A         //----| -         //-------group2 -         //---------| the         //------------myTv -  -Group1 =NewGroup1 ( This); -Group2 =NewGroup2 ( This); +MyTv =NewMytextview ( This); -Group2.addview (MyTv,Newlayoutparams (layoutparams.fill_parent, + layoutparams.fill_parent)); AGroup1.addview (Group2,Newlayoutparams (layoutparams.fill_parent, at layoutparams.fill_parent)); - Setcontentview (group1); -     } -}

Override the Onintercepttouchevent and Ontouchevent methods of Group1 and Group2, override Mytextview's Ontouchevent method, and finally get the following hierarchy of controls:

1. In the case of the default return value, the Logcat output is as follows:

After testing, it is known that by default and all methods return a value of false the result is consistent, the capture order of the down event onintercepttouchevent precedes Ontouchevent, because the Ontouchevent return value is False, The down event is not digested, the subsequent move and up events do not appear, and the reverse order is returned to the parent control's Ontouchevent method to capture, as shown in:

2. If all Ontouchevent return value is true, the Logcat output is as follows

The output shows that the child controls Mytextview digest the down event, and the subsequent move and up events are properly captured, as the down event is digested and the upper-level ontouchevent method is not executed, as shown in: (three arrows refer to down, move, up events, respectively)

In this case, if the Ontouchevent method in Mytextview returns to false, and the Ontouchevent method of Group1 and Group2 returns True, the result is naturally the order:

The test output proves the sequence of guesses,

Note: Some people may be more puzzled about this situation, action_down good understanding, but action_move why not experience MyTV, and Action_ Move only experienced group1 onintercepttouchevent and group2 ontouchevent without experience group2 onintercepttouchevent, I began to understand, but also, Everybody compares 1th, because Ontouchevent returns false and does not consume the down event cause the subsequent move and up do not appear, this is also the same as the MyTV in the ontouchevent returned false that is not consumed by the down event, Then the move and up will not appear in this view, but Group2 intercepted the down event, but then the move why Group2 in the onintercepttouchevent did not do it, Why don't you forget about Onintercepttouchevent's original intention, and return false is to let its child view or ViewGroup class handle, While Group2 's child controls are obviously MyTV and MyTV's ontouchevent returns false, which means that no subsequent move and up events are received. There is no need to go through the onintercepttouchevent to continue distributing (because the distribution is still not received), after the group2 ontouchevent because it returns true, intercepts the event and consumes the event.

3. When the Onintercepttouchevent method in a Groupview returns a value of true, the Logcat output is as follows (for example, group2):

If True is returned in the return value of the method, the child control will not get any click events, instead forwarding to its own Ontouchevent method, as shown in:

If the return value of the Ontouchevent method is true, then the rule results are triggered as sequential:

The final logcat results confirm this speculation,

According to this sequence, we can replicate the onintercepttouchevent in the Groupview to control the responder of the event.

Test Demo: Download demo

[Learning summary] 4, Android ViewGroup in the event of the transfer mechanism (a)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.