The execution of click events for parent view and child view in Android

Source: Internet
Author: User

The types of events in Android are key events and screen touch events, and touch events are the underlying events of screen touch events, and it is necessary to understand them in depth.
One of the simplest screen touch actions triggered a series of touch events:action_down->action_move->action_move->action_move...->action_move-> Action_up

When the screen contains a viewgroup, and the ViewGroup contains a sub-view, how does the Android system handle the touch event? In the end
ViewGroup to handle touch events or sub-view to handle touch events? I can only be sure to say to you not necessarily. Oh, why? Look at my findings below.
I understand.

Each view subclass in the Android system has the following three methods that are closely related to TouchEvent processing:

1) Public boolean dispatchtouchevent (motionevent ev) This method is used to distribute TouchEvent

2) Public boolean onintercepttouchevent (motionevent ev) This method is used to intercept touchevent
3) Public boolean ontouchevent (motionevent ev) This method is used to process touchevent

When touchevent occurs, the activity first passes the touchevent to the topmost view,
The TouchEvent first arrives at the topmost view of the dispatchtouchevent and is then distributed by the Dispatchtouchevent method,

If Dispatchtouchevent returns True, it is given to this view's ontouchevent processing,

if Intercepttouchevent returns True, that is, it is intercepted, it is given to its ontouchevent to handle,
if Intercepttouchevent returns FALSE, it is passed to the child view, and the dispatchtouchevent of the sub view begins the distribution of the event.

If the event is passed to the ontouchevent of a child view of a layer, this method returns false, and the event is passed from this view to the ontouchevent to receive.
If the event is passed to the ontouchevent of a sub-view of a layer, this method returns false, then the event will not be passed up, and this view interception process.
If the ontouchevent passed to the top also returns false, the event "disappears" and cannot receive the next event

Onintercepttouchevent () is used to handle events and change the direction in which events are passed. Handling events This needless to say, you can write code processing inside the function. Instead of passing to the child control, the return value is returned as false when the event is passed to the child control's onintercepttouchevent (), and the return value is true when the event is passed to the ontouchevent () of the current control. This is called intercept (truncation).

Ontouchevent () is used to handle the event, and the return value determines whether the current control consumes (consume) the event. Maybe you want to ask if there is a difference between consumption, anyway, I have written the processing code for the event? The answer is a difference! For example, 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.

This article source address: http://www.cnblogs.com/rocky_yi/archive/2011/01/21/1941522.html#, reprint please indicate the source!

<?XML version= "1.0" encoding= "Utf-8"?>
<Com.touchstudy.LayoutView1xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:orientation= "vertical"
Android:layout_width= "Fill_parent"
Android:layout_height= "Fill_parent" >
<Com.touchstudy.LayoutView2
android:orientation= "vertical"
Android:layout_width= "Fill_parent"
Android:layout_height= "Fill_parent"
Android:gravity= "Center">
<Com.touchstudy.MyTextView
Android:layout_width= "Wrap_content"
Android:layout_height= "Wrap_content"
Android:id= "@+id/tv"
Android:text= "AB"
Android:textsize= "40SP"
Android:textstyle= "Bold"
Android:background= "#FFFFFF"
Android:textcolor= "#0000FF"/>
</Com.touchstudy.LayoutView2>
</Com.touchstudy.LayoutView1>

In cases where onintercepttouchevent () and ontouchevent () are not overridden (their return values are false), the order of the Motionevent events is passed as follows:

When a control's onintercepttouchevent () return value is true, truncation occurs, and the event is passed to the current control's ontouchevent (). If we return the LayoutView2 onintercepttouchevent () value to True, then the delivery process becomes:

If we set LayoutView2 's Onintercepttouchevent () and Ontouchevent () to true at the same time, then LAYOUTVIEW2 will consume the event that is passed, while subsequent events (such as following Action_ Down Action_move or ACTION_UP) is passed directly to LayoutView2 's ontouchevent () and does not pass any function to any other control. A Action_cancel event is passed to the subspace at the same time. The transfer process becomes (no Action_cancel event is drawn in the diagram):

The instructions that accompany the SDK:

· You'll receive the down event here.

· The down event is handled either by a child of this view group, or given to your own ontouchevent () method to handle; This means your should implement ontouchevent () to return true, so you'll continue to see the rest of the gesture (inste Ad of looking for a, parent view to handle it). Also, by returning True from Ontouchevent (), you'll not receive any following events in Onintercepttouchevent () and all Touch processing must happen in ontouchevent () like normal.

· For as long as you return false from this function, each following event (up to and including the final up) would be delive Red first here and then to the target ' s ontouchevent ().

· If you return true from this, you won't receive any following events:the target view would receive the same event but W ith the action Action_cancel, and all further events would be delivered to your ontouchevent () method and no longer appear Here.


The execution of click events for parent view and child view in Android

Related Article

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.