Onintercepttouchevent and Ontouchevent

Source: Internet
Author: User

Before practicing custom viewgroup, using the Onintercepttouchevent method, this method and ontouchevent easy to confuse, hereby share the use of these two methods.

1, Onintercepttouchevent () is used to handle the event, and its return value determines whether the touch event is allowed to continue down (child control) delivery. Returns True (representing that the event will be processed in the current viewgroup), the path to the down pass is truncated (all child controls will have no chance to participate in the touch event), and the event is passed to the current control's ontouchevent () processing; returns false. The event is given to the child control's onintercepttouchevent (), and so on.

2, Ontouchevent () is used to handle the event, and its return value determines whether the current control consumes the event, and whether the touch event continues to pass up (the parent control). Returns false, which is passed up to the parent control. Returns true if the child control returns True, then its touch event is handled here, and the parent control cannot handle it because it does not receive the touch that the child control passes to him, and the quilt control is intercepted.

PS: Does consumption have an impact on processing? Some, such as action_move or action_up occurrence of the premise is must have occurred action_down, if you do not consume Action_down, then the system will think Action_down did not happen, so Action_ Move or action_up cannot be captured.

From this concept, we can probably tell the difference, the event that Onintercepttouchevent handles is passed from the parent control to the child control until it is blocked or to a view that does not have the event, and then goes back from the child control to the parent control. From child controls to parent controls, the ontouchevent is responsible, and the relationship between the two is closely linked. From this we can also know that onintercepttouchevent is aimed at Childview ViewGroup only way, Ontouchevent is the method of all view.

Let's use a small example to illustrate how the two work:

1     <Com.example.LayoutView12         xmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "vertical" >6 7         <Com.example.LayoutView28             Android:layout_width= "Fill_parent"9 Android:layout_height= "Fill_parent"Ten android:gravity= "Center" One android:orientation= "vertical" > A  -             <Com.example.MyTextView -                 Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" /> -         </Com.example.LayoutView2> -     </Com.example.LayoutView1>

This simple layout is probably a hierarchical relationship on the phone:

Usually the periphery of the layoutview1,layoutview2 is just the layout of the container does not need to respond to touch-screen click events, just mytextview need to click accordingly. But this is only the general case, some special layout may also have to respond to the peripheral container (for example, pull-down refresh viewgroup), not even let the inside of the Mytextview to respond. In a more special case, the response object is dynamically replaced. So first look at the pass-through process between the two functions of the default touch-screen event. Such as:

We describe the most common down events

    1. The down event is first passed to the Onintercepttouchevent () method

    2. If the ViewGroup onintercepttouchevent () return false after receiving the down event processing, then the subsequent move, up, and so on will continue to be passed to the ViewGroup first. It is then passed to the ontouchevent () processing of the final target view like the down event.

    3. If the ViewGroup onintercepttouchevent () return true after receiving the down event processing, subsequent moves, up, and so on will no longer be passed to Onintercepttouchevent (), Instead, the ontouchevent () process passed to the viewgroup like the down event, noting that the target view will not receive any events.

    4. If the ontouchevent () of the view that eventually needs to handle the event returns false, the event is passed to the ontouchevent () processing of the view at its previous level.

    5. If the ontouchevent () of the view that eventually needs to handle the event returns TRUE, then subsequent events will continue to be passed to the view's ontouchevent () processing.

Let's take a look at Layoutview1 's onintercepttouchevent. If the process returns true:

Onintercepttouchevent and Ontouchevent

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.