Event distribution and processing in Android under ViewGroup

Source: Internet
Author: User

Let's write a simple demo:

A custom control in a layout file that inherits from ViewGroup Mylayout contains a button:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <com.example.viewgroupdemo.mylayout        android:id= "@+id/layout"        android:layout_width= "Match_ Parent "        android:layout_height=" match_parent ">        <button            android:id=" @+id/button "            android: Layout_width= "Wrap_content"            android:layout_height= "wrap_content"            android:text= "@string/hello_world"/ >    </com.example.viewgroupdemo.MyLayout></RelativeLayout>


Two important ways to override event distribution in a custom control: Onintercepttouchevent and Dispatchtouchevent

public class Mylayout extends LinearLayout {public Mylayout (context context, AttributeSet Attrs) {Super (context, attrs);} Whether to intercept the delivery of the event, true: Intercept @overridepublic boolean onintercepttouchevent (motionevent ev) {//false: Pass the touch event to the child control return false;} LinearLayout does not rewrite Dispatchtouchevent//viewgroup overrides the view's Dispatchtouchevent method @overridepublic Boolean Dispatchtouchevent (motionevent ev) {return super.dispatchtouchevent (EV);}}
Set two control's click events in Mainactivity:

Layout.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {LOG.I (tag, "click Layout------- -");}}); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {LOG.I (tag, "click button------- -");}});

At this point, click button, because there is no blocking event delivery in the outer mylayout, the button responds and processes the event and prints the log of the "click button"

If you return true in the onintercepttouchevent of Mylayout, the mylayout prevents the delivery of the event, which prints "click Layout" at that time

Question: When tapping the screen, how does the system determine which view is being clicked on?

In fact, each view corresponds to a rectangular area on the screen, and when the screen is clicked, the system determines which view is selected by determining which rectangular area the point belongs to.

View the source code interpretation phenomenon:

<pre name= "code" class= "Java" > Public boolean dispatchtouchevent (motionevent ev) {...//handling when pressed                if (action = = Motionevent.action_down) {//First place the View object as NULL if (mmotiontarget! = null) {            Mmotiontarget = null;             }//If event delivery is not blocked, pass in inward if (disallowintercept | |!onintercepttouchevent (EV)) {/** * Summary of the steps: *//1, find the current control child control//2, determine the current point midpoint, where (x, y) belongs to which child control in the rectangular area//3, determine whether the current child control is ViewGroup subclass object, or the sub-class object of view//3.1 VI                Ewgroup the above operation again//3.2 view tries to get the current view to handle this event (the True,dispatchtouchevent method ends, and returns TrueFalse, no return value currently) */                Reset this event's action (just to protect ourselves) ev.setaction (Motionevent.action_down); We know we want to dispatch the event down, find a child//who can handle it, start with the F                Ront-most Child.                Final view[] children = Mchildren;         Gets the number of current ViewGroup child nodes       Final int count = Mchildrencount;                    Iterates through the current ViewGroup child node by determining which child node is included in the point to determine which view is selected for (int i = count-1; I >= 0; i--) {                    Final View child = Children[i];                        if (child.mviewflags & visibility_mask) = = VISIBLE | | child.getanimation () = null) {                        Child.gethitrect (frame);                            Determines which rectangular area the point is contained within if (Frame.contains (Scrolledxint, Scrolledyint)) {... The event distribution here may be viewgroup or view, but the final call is the view's Dispatchtouchevent method//Note: ViewGroup is a dispatchtouchevent method that has a view that overrides the View//If True, this view sub-node handles the event and the event response is complete, refer to the next line Goole the engineer's                                Note ~ ~//If it is false, continue to follow the logical if (child.dispatchtouchevent (EV)) {                      Event handled, we have a target now.          Mmotiontarget = child;                            return true; }                        }                    }                }            }        }        ......        Omit extraneous code//The event wasn ' t an Action_down, dispatch it to our target if//we have one.        Final View target = Mmotiontarget;  if (target = = null) {//We have a target, this means we ' re handling the//event as a regular            View.        Invokes the event distribution rule for the parent view of the current object, noting that the current object is a ViewGroup object, so the parent view is the view return super.dispatchtouchevent (EV); }


Event distribution and processing in Android under ViewGroup

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.