Android in touch event delivery Summary

Source: Internet
Author: User

TouchEvent

TouchEvent three types of events: down, move, up.

Where the move event is in one operation (one of the actions described here is that the user interacts with the screen, that is, the action sequence from down to up) may occur more than once.
However, we think that an action sequence will contain the above three events, therefore, in the event processing is to deal with this process, and most importantly, the down event, which is the beginning of an action sequence, there is no down the back of the event.
So, we take the class that consumes the down event as the final vector of the action sequence.

If the down event is not handled by you, then the move,up of the action sequence is not yours.

Their trigger sequence would be this:

Action_down->action_move->action_move->action_move...->action_move->action_up

How touch event delivery is related

Almost all of the view subclasses in the Android system have the following three methods that are closely related to TouchEvent processing:

// This method is used to distribute TouchEvent  Public Boolean dispatchtouchevent (motionevent ev) //  // only ViewGroup contains this method, normal view does not contain method publicboolean  Onintercepttouchevent (motionevent ev)//publicboolean Ontouchevent (motionevent ev)

Ontouchevent, OnTouch

The Ontouchlistener interface is a listening interface for handling mobile screen events, and it is necessary to implement the Ontouch event/onclick event of the interface to set the triggering event, and then we can pass the Setontouchlistener () Add the listener for any of the child classes of the view that you want.

So what does the Ontouch method in the Ontouchlistener interface have to do with the transmission of the touch event?

In the source code of Ontouchevent There is such a paragraph:

if NULL NULL ) {              playsoundeffect (soundeffectconstants.click);              Li.mOnClickListener.onClick (this);
            return true ;          }

If we register the onclick listener again, the listener will be executed in the ontouchevent and return true.

If the onclick listener is not registered, the top-level view ontouchevent default returns False, and the other layer view Ontouchevent returns true by default.

Therefore, if Ontouch returns True, the event is consumed and the ontouchevent is no longer executed, let alone the onclick listener. If Ontouch returns False, the ontouchevent is resumed and the onclick listener is executed, and if True is returned in Ontouchevent, the event is consumed and no longer passed down the view.

Conclusion: The Ontouch method of Ontouchlistener has higher priority than ontouchevent and will be triggered first. If the Ontouch method in Ontouchlistener returns True, indicating that the event has been consumed, the ontouchevent is not receiving the message. If the Ontouch method returns False, the ontouchevent is then triggered, and the Ontouchevent method is not invoked. Built-in implementations such as the Click event are based on Ontouchevent, and if Ontouch returns True, these events will not be triggered.

A simple flowchart for touch event delivery:

Consider the process in conjunction with the diagram:

If the return value is super.dispatchtouchevent, it means that the event flow is determined by onintercepttouchevent.

If the super.dispatchtouchevent is called. This will continue to invoke the Onintercepttouchevent event. Therefore, if more than one control needs to be processed at the same time, you must return true and call Super.dispatchtouchevent

In other words, if you want to pass events to a child view, super.dispatchtouchevent is essential.

Any view Ontouch or Ontouchevent method returns True, indicating that the event has been consumed and is no longer passed down.

Other:

    • When the down event returns true after Onintercepttouchevent (), it is passed to ontouchevent, and when it returns true, subsequent events of the action sequence are no longer passed onintercepttouchevent. Instead, it is passed directly to Ontouchevent in Dispatchtouchevent. This means that subsequent events are distributed directly to Ontouchevent by default on the view's dispatchtouchevent.
    • The down event returns false after Ontouchevent, which causes the event not to be consumed, is passed up to the parent view's ontouchevent, and if true, the subsequent move, The up event is passed directly from the parent view's dispatchtouchevent to its own ontouchevent without passing through the onintercepttouchevent of the parent view, and the subsequent child view naturally receives no touch events.
    • Onintercepttouchevent returns false by default, the ontouchevent of the top-level view returns false by default, and the ontouchevent of other view returns true by default.
    • If we overwrite the onclick, Onlongclick and ontouchevent at the same time in a view, Ontouchevent is the first to capture Action_down and action_up events, Second, the onclick or onlongclick may be triggered.

Gesturedetector gesture Recognition class

By overriding the Ontouch (View V, motionevent Event) method of the View.ontouchlistener interface, we can handle some simple touch events, but this method does not recognize gestures, and if you need to handle some complex gestures, Using this interface can be cumbersome (because we want to determine what gestures are based on the user's touch path). Fortunately Android provides us with the Gesturedetector class, through which we can easily do gesture recognition.

The steps for adding gesture actions to view and activity are as follows:

1. Implement the Ongesturelistener interface for view or activity, and overwrite the callback method of the required gesture.

2. Create a Gesturedetector object Mygesturedetector and set its listener.

3. Overwrite the Ontouchevent method of view or activity, call or return mygesturedetector.ontouchevent (EV), and give the event to mygesturedetector processing.

Very good references:

http://www.cnblogs.com/ghj1976/archive/2012/04/13/2445561.html Knowledge Base for message distribution

http://ipjmc.iteye.com/blog/1694146 Android Event handling

The transmission mechanism of events in the ViewGroup of http://www.longdw.com/touchevent-android/Android

Android in touch event delivery Summary

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.