Event distribution mechanism

Source: Internet
Author: User

Event distribution mechanism 1, event distribution in a single view

Touch event handling for a single view triggers two methods:

Dispatchtouchevent (Motionevent event);

Ontouchevent (Motionevent event);

When a touch event occurs:

The down event of the Dispatchtouchevent event is executed first, and then the down event of the Ontouchevent event is executed, and when the down event of the Ontouchevent event returns a value of true, The Dispatchtouchevent method also returns true and passes the next move event, up event, to Ontouch. After the move event and the return value of the Up event, Ontouch returns what Dispatchtouchevent also returns.
Conversely, if the first down event is passed to Ontouch, Ontouch returns false, and the event is no longer transmitted, that is, dispatchtouchevent. Not even go ontouchevent.

Return true means that the view will continue to handle the lift event without passing the time to the parent view---------This also means that only one view can continue to listen on the touch event.

2. Event distribution of view in activity

The touch event handling of view in activity triggers two methods of activity and view:

Dispatchtouchevent (motioneventevent);

Ontouchevent (Motionevent event);

The Dispatchtouchevent method is passed from the parent view to the child view, and the Ontouchevent method is passed from the child view to the parent view.

Event Execution Order:

In the case of no processing, first trigger the Dispatchtouchevent down event in activity, and then trigger the Dispatchtouchevent down event in the child view. The Ontouch event in the setontouchlistener of the child view is then triggered, then the down event of the ontouchevent in the child view is triggered, and the ontouchevent down event that triggers the activity is finally passed up. And then, because the child view does not handle the down event, as the gesture moves, it triggers the dispatchtouchevent move event in the activity and then triggers the ontouchevent move event in the activity. The up event is then set to dispatchtouchevent in activity and then ontouchevent in the activity.

Special cases:

  1. When the down event in Dispatchtouchevent in activity returns a value of true, it means that the activity is handling the event, so the event is not passed down. Instead, the move or up event in dispatchtouchevent in the activity is executed directly, and then the ontouchevent move or up event is executed.
  2. When the down event in Dispatchtouchevent in activity returns a value of false, it means that the activity does not handle the event, the event is not passed down, and the Ontouchevent down event in the activity is not executed , the move or up event in Dispatchtouchevent is executed directly before the move or up event of the ontouchevent is executed.
  3. When the down event in Dispatchtouchevent in activity does not do any processing, the event is passed to the child view, triggering the down event in the dispatchtouchevent in the child view, and when this method returns a value of true, Represents the method to handle this event, so the event is no longer passed down, triggering the move or up event of the disaptchtouchevent in activity, then passing down again, Triggers a move or up event from the dispatchtouchevent in view and then triggers the Ontouch event in the view's Setontouchlistener. The ontouchevent move or up event in the child view is then triggered, and the move or up event of the ontouchevent that triggered the activity is finally passed up.
    1. When the down event in Dispatchtouchevent in activity does not do any processing, the event is passed to the child view, triggering the down event in the dispatchtouchevent in the child view, and when this method returns a value of false, Represents a child view that does not handle this event, and then passes up the down event of the ontouchevent that triggered the activity, and then triggers the move or up event of the dispatchtouchevent in the activity, Because the child view has declared that the event will not be processed, the event is no longer passed down, but instead directly triggers the ontouchevent move or up event in the activity.
    2. When the down event in Dispatchtouchevent in activity does not do any processing, the event is passed to the child view, triggering the down event in the dispatchtouchevent in the child view, and when the method does not do any processing, Triggering the Ontouch event in the view's setontouchlistener and then triggering the view's ontouchevent down event, when the method returns a value of true, means that the view is handling this event, so the event no longer passes up, Instead, it triggers the dispatchtouchevent move or up event in the activity, because the child view is handling the event, so the event is passed down again, triggering the dispatchtouchevent move or up event in view. Then trigger the Ontouch event in the view's setontouchlistener and then trigger the view's ontouchevent move or up event, The event is then passed up to trigger the ontouchevent move or up event in the activity.
    3. When the down event in Dispatchtouchevent in activity does not do any processing, the event is passed to the child view, triggering the down event in the dispatchtouchevent in the child view, and when the method does not do any processing, Triggering the Ontouch event in the view's setontouchlistener and then triggering the view's ontouchevent down event, when the method returns a value of false, means that the view does not handle this event, so the event is passed up again, The down event that triggers the activity's ontouchevent, then triggers the move or up of the dispatchtouchevent in the activity, because the child view does not handle the event, so the event is no longer passed down. Instead, it triggers a move or up event that triggers the ontouchevent in the activity.
3 ViewGroup Event Distribution mechanism

When the Ontouch event is triggered, Android ViewGroup calls the following three functions:

public boolean dispatchtouchevent (motionevent ev)//For distribution of events

public boolean onintercepttouchevent (motionevent ev)//For event interception

public boolean ontouchevent (motionevent ev)//handling events

For a control of the ViewGroup class, there is an important way to do this is onintercepttouchevent (), which handles the event and alters the passing direction of the event, whose return value is a Boolean value that determines whether the touch event will continue to be passed to the child view it contains. This method is passed from the parent view to the child view. The method Ontouchevent () is used to receive and process the event, and its return value is also a Boolean value that determines whether the event and subsequent events continue to pass upward, which is passed from the child view to the parent view. The transfer mechanism of the touch event between Onintercepttouchevent () and ontouchevent and each childview depends entirely on the return value of Onintercepttouchevent () and Ontouchevent (). A return value of TRUE indicates that the event was received and processed correctly, and a return value of FALSE indicates that the event was not processed and will continue to be passed.

The Action_down event is passed to the onintercepttouchevent of a ViewGroup class, and if False is returned, the down event continues to pass Onintercepttouchevent to the child ViewGroup class. If the child view is not a control of the ViewGroup class, it is passed to its ontouchevent.

If Onintercepttouchevent returns True, the down event is passed to its ontouchevent, no longer passed, and subsequent events are passed to its ontouchevent.
If the ontouchevent of a view returns false, the down event continues to pass to the ontouchevent of its parent ViewGroup class, and if true is returned, subsequent events are passed directly to their ontouchevent to continue processing. (subsequent events are only passed to ontouchevent that return true for the necessary event Action_down.)

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).

Remember: Ontouchevent () is used to handle events, 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.

Event distribution mechanism

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.