View event distribution mechanism and View event distribution mechanism

Source: Internet
Author: User

View event distribution mechanism and View event distribution mechanism

 

The View event distribution mechanism is completed by three methods:

Public boolean dispatchTouchEvent (MotionEvent ev );

Public boolean onInterceptTouchEvent (MotionEvent ev );

Public boolean onTouchEvent (MotionEvent ev );

You can use the followingPseudocodeIndicates:

public boolean dispatchTouchEvent(MotionEvent ev){    boolean consume=false;    
if(onInterceptTouchEvent(ev)) { consume=onTouchEvent(ev); } else { consume=child.dispatchTouchEvent(ev); } return consume;}

From the pseudocode above, we can see that the transfer rule for click events is as follows:
The click event is passed from the external to the internal, and the ViewGroup that receives the event is the outermost layer. In this case, the dispatchTouchEvent method is called (if the event is received, this method will be called). If the onInterceptTouchEvent method returns true, it indicates that it wants to intercept the event, and the event is handed over to the ViewGroup for processing, that is, the onTouchEvent method is called.

On the contrary, if the onInterceptTouchEvent method returns false, it means that the ViewGroup does not intercept the event, the event will be passed to its child element, and the dispatchTouchEvent method of the child element will be called, and so on until the event is finally handled.

If the event has been handed over to the subview for processing, but the onTouchEvent method of the subview returns false (no event is processed, or the event fails to be processed ), then the onTouchEvent of the parent container will be called (we can think that the lower-level fails to process the event, so the upper-level will handle it), and so on, if all elements do not process the event, the event will be passed to the activity, and the onTouchEvent method of the activity will be called.

 

When the event is distributed, the transfer order is Activity-> Window-> View.

From the above analysis process, we can know the functions of the above three methods:

DispatchTouchEvent: used to distribute events. If an event can be passed to the view, the dispatchTouchEvent method of the view will be called.

OnInterceptTouchEvent: used to determine whether to intercept an event. If true is returned, it indicates interception. If false is returned, it indicates no interception. Once an event is intercepted by this view, this method will not be called in the same event sequence.

OnTouchEvent: this event is used to process events. If it is returned, true indicates consumption, and false indicates no consumption. If it is not consumed, the current view in the same event sequence cannot receive the event again.

 

What is the same event sequence?: A series of events that occur when your fingers touch the screen to the moment your fingers exit the screen.

(For example, when you are sliding: ACTION_DOWN-> ACTION_MOVE...-> ACTION_UP)

 

Priority of onTouchEvent, OnTouchListener, and OnClickListener:

OnTouchListener> onTouchEvent> OnClickListener

How to say: if OnTouchListener is set when the view processes the event, the onTouch in the OnTouchListener method will be called back. If onTouch returns true, the onTouchEvent will not be called, otherwise, onTouchEvent will be called. If OnClickListener is set, The onClick method will be called in onTouchEvent.

 

There are several notes:

1. Once a view starts to process events, if it does not consume ACTION_DOWN events (onTouchEvent returns false), other events in the same event sequence will not be handed over to it for processing, the event will be handed over to the parent element for processing (the onTouchEvent of the parent element will be called ). If this view consumes ACTION_DOWN events but does not consume other events, the events will disappear, and these click events will be passed to the Activity for processing.

2. ViewGroup does not intercept all events by default. Its onInterceptTouchEvent returns false by default.

3. view does not have the onInterceptTouchEvent method. Once an event is passed to it, its onTouchEvent method will be called.

4. The onTouchEvent method of the view returns true by default, which means that the view consumes events by default, unless it cannot be clicked, that is, its clickable and longClickable are set to false at the same time. In fact, the longClickable attribute of view is false by default, and clickable is case-sensitive. For example, the clickable attribute of Button is true by default, and the clickable attribute of TextView is false by default.

5. The enable attribute of view does not affect the default return value of onTouchEvent. Even if it is disable, as long as one of its clickable or longClickable is true, its onTouchEvent returns true.

6. onClick occurs only when the current View is clickable and receives down and up events.

 

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.