Some small understandings about event delivery and distribution in Android

Source: Internet
Author: User

Android when our fingers touch the screen will produce an event,

(assuming that the process is not shown to intercept the event)
This event is passed to the lowest level of the view, even though some views in the middle respond to the event (this view does not consume the event).

is still passed to the bottom (the underlying does not respond to the event), and then back to the top layer from the bottom, in the process of returning to the top layer,
The view that originally responds to this event will not consume this event

For example, in the left image, A
B (will respond to the event)
C
D

(assuming that no special treatment is done during this whole process)

The event is passed from a to D, and at this time B does not consume this event
Only when the event is passed from D to a, B goes back to consuming the event.


However, assuming D also responds to this event, then the event will be consumed by D to stop the event and then B will not have the opportunity to consume the event.

This means that the B at this time is tantamount to ignoring the touch (click) event.


Then why is the event consumed when it reaches the bottom d? (assuming that the underlying D responds to the event)


Because, ViewGroup has the Onintercepttouchevent () method, and the view is not, that is to say
When a view responds to an event, it is bound to consume the event because it has no choice but to consume the event.
ViewGroup, by default, does not intercept events but is distributed to sub-view.

Here are three ways to intercept and distribute:

The Dispatchtouchevent () method is used for the distribution of events, and all events in Android must be distributed through this method,
Then decide whether to consume the current event yourself or continue to distribute it to child control processing. Returns true to indicate that no continuation of the distribution
The event was not consumed. Returns false to continue to distribute downward,
If it is viewgroup, it is distributed to onintercepttouchevent to determine whether to intercept the event.

Onintercepttouchevent () is a method in ViewGroup that is not in view, (that is, the method of interception in ViewGroup)
It is responsible for intercepting the event, returning true to intercept the current event,
Do not continue to distribute, handing over to your own ontouchevent (that is, triggering the Ontouchevent () method). Returns false to Not intercept,
Continue to preach. This is a unique method for ViewGroup, because there may be sub-view in the ViewGroup, and the view in Android can no longer contain child view.

The Ontouchevent () method is used for the handling of the event, which returns true for consumption processing of the current event,
Returns False if it is not processed and handed over to the child control for continued distribution.

Additions and summaries: the passing of an event contains a forward pass (the process that the parent view passes to the child view) and a postback pass (the child view passes to the parent view)
In a forward pass, the parent view can intercept the event so that the event is not passed to the child view, and, similarly, in the postback pass, the child view can also intercept the event so that it is no longer passed back to the parent view

The distance between the x-axis and the y-axis sliding
float DX = Math.Abs (ENDX-DOWNX);
float DY = Math.Abs (Endy-downy);
if (DX > Dy&&dx>8) {
Slide horizontally
Response skid
Anti-interception-event to Slidelayout
GetParent (). Requestdisallowintercepttouchevent (True); (actually the parent view called the anti-interception method)
}

The issue of conflicts over the delivery and distribution of various events is focused on analyzing the dependencies between the various views
Then 1. Determine (reverse) intercept or not, 2. Determine if the horizontal slide is greater than the vertical slide

The core idea is : let the different operations do their own events do not interfere with other people's affairs

    For example, the relationship of the view in the slide-in menu is assumed to be this
    activity--"relatilayout--" listview--" Slidelayout (custom ViewGroup)--"1.content (TextView)
                                                                                                                   --"2.menu (TextView)

Side-by-side menus (1. Conflict resolution when handling slippage with the ListView: an anti-intercept event (actually a conflict between Slidelayout and Slidelayout
That is, this event slidelayout not responding and the ListView responds)
2. Dealing with a ListView entry when clicking Events and skidding (by analyzing the dependencies, it is actually a conflict between content and Slidelayout.
It is the content that consumes the event, slidelayout the event so that it cannot be slid) Resolve: Intercept event)

Just to show that I opinion, if there is a mistake, please correct me.

-----love life, love Android.

Some small understandings about event delivery and distribution in Android

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.