How to distribute and intercept Android-touchevent

Source: Internet
Author: User

Each ViewGroup subclass in the Android system has the following methods that are closely related to the next three and touchevent processing:

 Public boolean dispatchtouchevent (motionevent ev)         //  This method is used to distribute TouchEvent Public boolean onintercepttouchevent (motionevent ev)         // This method is used to intercept toucheventpublic  boolean ontouchevent (motionevent ev)                 // This method is used to process touchevent

Note: Not all of the view's subclasses, many of the tutorials are talking about all of the view's subclasses, only the control that can add view to it needs to be distributed, such as TextView itself is the smallest view, so no longer to its sub-view distribution, it also has no sub-view, So it has no dispatch and intercept, only touchevent.

The onintercepttouchevent (motionevent ev) method is a unique method in the Groupview control, and we can implement this method to intercept all the window click events to trigger the distribution of events to the child control or itself based on the difference in the event. It is important to be careful when overriding this method because it has a complex relationship with View.ontouchevent (motionevent), and we use it to achieve the right results with view.ontouchevent (motionevent). The event firing sequence is as follows:

1. You will first get a down event in this method.

2. This down event is handled either in the Ontouchevent () method of the Groupview neutron control or in the Ontouchevent () method of Groupview itself. When we return true in the Ontouchevent () method return value, we will continue to see the subsequent triggering events (move, up, etc.), noting that when we return false in the Groupview Ontouchevent () event return value, The method will no longer capture subsequent (move up) events.

3. If the return False,down event in the method return value is distributed from the root node to the Ontouchevent () method of the target child control based on the control tree structure.

4. If true is returned in the method return value, then your child control will not get any click events.

To demonstrate the order characteristics described above, the following test code is observed:

 Public classMainactivityextendsActivity {Group1 group1;      Group2 group2;        Mytextview MyTv; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //--group1//----| //-------group2//---------| //------------myTvgroup1=NewGroup1 ( This); Group2=NewGroup2 ( This); MyTv=NewMytextview ( This); Group2.addview (MyTv,Newlayoutparams (layoutparams.fill_parent, layoutparams.fill_parent)); Group1.addview (group2,Newlayoutparams (layoutparams.fill_parent, 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. for all ontouchevent return values of 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 Ontouchevent method is not executed as shown in: (three arrows refer to down, move , up events)

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,

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.

I'm the dividing line of the king of the Land Tiger.

Reference: http://blog.csdn.net/alexander_xfl/article/details/11056679

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.