Distribution and Consumption of Touch events in Android programming

Source: Internet
Author: User

Distribution and Consumption of Touch events in Android programming

Methods related to Touch events in Android include dispatchTouchEvent (MotionEvent ev), onInterceptTouchEvent (MotionEvent ev), and onTouchEvent (MotionEvent ev). controls that can respond to these methods include: viewGroup, View, and Activity. The relationship between methods and controls is shown in the following table:

Touch event related methods Method Functions
ViewGroup
View
Activity
Public boolean dispatchTouchEvent (MotionEvent ev) Event Distribution
Yes Yes Yes
Public boolean onInterceptTouchEvent (MotionEvent ev)
Event Interception
Yes Yes No
Public boolean onTouchEvent (MotionEvent ev) Event Response
Yes Yes Yes

  • If return true, the event is distributed to the current View and consumed by the dispatchTouchEvent method. At the same time, the event stops being passed down;
    • If return false, event distribution is divided into two situations:
      1. If the event retrieved by the current View is directly from the Activity, the event is returned to the onTouchEvent of the Activity for consumption;
        1. If the event retrieved by the current View comes from the outer parent control, the event is returned to the onTouchEvent of the parent View for consumption.
          • If the system returns the default super. dispatchTouchEvent (ev), the event is automatically distributed to the onInterceptTouchEvent method of the current View.
            • If onInterceptTouchEvent returns true, it intercepts the event and submits the intercepted event to the onTouchEvent of the current View for processing;
              • If onInterceptTouchEvent returns false, the event is released. The event on the current View is passed to the subview, And the dispatchTouchEvent of the subview is used to distribute the event;
                • If onInterceptTouchEvent returns super. onInterceptTouchEvent (ev), the event is intercepted by default, and the intercepted event is handed over to the onTouchEvent of the current View for processing.
                  • If the event is passed to the onTouchEvent method of the current View, and the method returns false, the event will be passed up from the current View and received by the onTouchEvent of the upper View, if the onTouchEvent passed to the above method also returns false, the event will "disappear" and the next event will not be received.
                    • If true is returned, the event is received and consumed.
                      • If super. onTouchEvent (ev) is returned, the default event processing logic is the same as when false is returned.

                        From this table, we can see that ViewGroup and View can respond to all three methods related to Touch events, while Activity does not respond to onInterceptTouchEvent (MotionEvent ev), that is, event interception. In addition, you must note that the View responds to dispatchTouchEvent (MotionEvent ev) and onInterceptTouchEvent (MotionEvent ev) only if you can add a subview to the View, if the current View is already a smallest unit View (such as TextView), you cannot add a subview to the smallest View, and you cannot distribute or intercept events to the subview, therefore, it does not have dispatchTouchEvent (MotionEvent ev) and onInterceptTouchEvent (MotionEvent ev), and only onTouchEvent (MotionEvent ev ).

                        I. Touch Event Analysis

                        ? Event distribution: public boolean dispatchTouchEvent (MotionEvent ev)

                        DispatchTouchEvent (MotionEvent ev) of the Activity when the Touch event occurs) the method will be in the tunnel mode (pass down from the root element until the inmost layer sub-element or stop passing because of a certain condition in the middle of a mona1 element) the event is passed to the dispatchTouchEvent (MotionEvent ev) method of the outermost View, and the dispatchTouchEvent (MotionEvent ev) method of the View distributes the event. The event distribution logic of dispatchTouchEvent is as follows:

                        ? Event interception: public boolean onInterceptTouchEvent (MotionEvent ev)

                        When the dispatchTouchEvent (MotionEvent ev) method of the outer View returns the system's default super. dispatchTouchEvent (ev), the event is automatically distributed to the onInterceptTouchEvent method of the current View. The event interception logic of onInterceptTouchEvent is as follows:

                        ? Event Response: public boolean onTouchEvent (MotionEvent ev)

                        When dispatchTouchEvent returns super. dispatchTouchEvent (ev) and onInterceptTouchEvent returns true or super. onInterceptTouchEvent (ev), onTouchEvent is called. The Event Response logic of onTouchEvent is as follows:

                        The following figure shows the entire event Transfer Process (layout: viewGroup1 is in the outermost layer, with a nested viewGroup2 and a Button added to viewGroup2 ). It can also be seen that the event is first transmitted to the corresponding layout (viewGroup1) of the outermost layer, and then to the inner layer (viewGroup2) in turn. When it is passed to the inner layer of View (Button ), if the View is already a minimum unit, it will be handed over to the onTouchEvent of the View to process the event. If the event is not consumed, the event will be returned up until no one processes the event, events will be lost.



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.