Android Event distribution detailed and sample code _android

Source: Internet
Author: User

Event distribution is a very important mechanism in Android and is the basis for user interaction with the interface. This article will create a flowchart of event distribution by using the example print log, making it easier for everyone to understand the Android event distribution mechanism.

First, the necessary basic knowledge

1. Related methods

The methods associated with event distribution in Android include Dispatchtouchevent, Onintercepttouchevent, ontouchevent three methods, and event distribution typically passes through three containers, which are activity, ViewGroup, View. The following table collates the related methods of event distribution for each of the three types of containers.

Activity
event-related methods Method Features ViewGroup View
public Boolean Dispatchtouchevent Event distribution Yes Yes Yes
public Boolean Onintercepttouchevent Event interception No Yes No
public Boolean Ontouchevent Event consumption Yes Yes Yes

Distribution: Dispatchtouchevent If True, represents the current view or its child view (child ...). View), the view that handles the event is found, otherwise it means that no find

Interception: Onintercepttouchevent If True, the event is handled by the current view, no matter how the result is processed, no longer passes the event to the child view, or the current view does not actively handle the event. Unless his child view returns the event distribution result to False

Consumption: Ontouchevent If True, indicates that the current view is the endpoint of the event delivery, and conversely, that the current view is not the endpoint of the event pass

2. Related events

In this article we only consider 4 kinds of touch events: Action_down, Action_up, Action_move, action_canal. Sequence of events: a sequence of events that starts with touching the screen from the finger to the end of the finger leaving the screen, resulting in a series of events. A sequence of events begins with the Action_down event, and the middle may pass through several move to action_up the event to an end. Next we will use the previous article to customize the view--elastic sliding examples As an example of this article, simply add some code, the modified code, please click to view.

Second, the example defaults

We can see from the XML of the sample code that the pictures are clickable.

<?xml version= "1.0" encoding= "Utf-8"?> <com.idtk.customscroll.parentview xmlns:android= "http://" Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:padding=" 10DP "tools:context="
    Com.idtk.customscroll.MainActivity "> <com.idtk.customscroll.childview android:layout_width=" match_parent " android:layout_height= "Match_parent" android:src= "@drawable/zhiqinchun" android:clickable= "true"/> <c Om.idtk.customscroll.ChildView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Androi d:src= "@drawable/hanzhan" android:clickable= "true"/> <com.idtk.customscroll.childview Android:layout_widt H= "Match_parent" android:layout_height= "match_parent" android:src= "@drawable/shengui" android:clickable= "true" /> <com.idtk.customscroll.childview android:layout_width= "match_pArent "android:layout_height=" match_parent android:src= "@drawable/dayu" android:clickable= "true"/> </c

 Om.idtk.customscroll.parentview>

Let's click here to check out the printed log.

Draw an event-passing flowchart based on the printed log

Now let's take a look at the process of the sequence of events:

    1. The Action_down event starts with the Activity#dispatchtouchevent method
    2. The Action_down event is passed to the Viewgroup#dispatchtouchevent method, and Viewgroup#onintercepttouchevent returns false, indicating that it does not intercept Action_down
    3. The Action_down event is passed to the View#dispatchtouchevent method, executed at View#ontouchevent, and returns True, indicating that the event has been consumed
    4. The result returned is true, is passed back to View#dispatchtouchevent, and then uploaded back to the Action_down event's starting point Activity#dispatchtouchevent method
    5. The Action_up event passes the same process as Action_down, which is not repeated here.

Here, use the situation in the work to simulate: Boss (activity), Project Manager (ViewGroup), software engineer (View)

The boss assigns a task to the project Manager (Activity#dispatchtouchevent→viewgroup#dispatchtouchevent), and the project manager chooses not to do the task (viewgroup# Dispatchtouchevent returns False), to the software engineer to handle the task (<view#dispatchtouchevent) (we ignore the director and team leader), the software engineer completed the task (view# Ontouchevent returns True)

Tell the project manager (return the result true,view#dispatchtouchevent→viewgroup#dispatchtouchevent) that the project manager tells the boss the result (returns the result true,viewgroup# Dispatchtouchevent→activity#dispatchtouchevent)

The project manager finished well, the boss decided to the project two, three and so on to the project manager, the same project manager also felt that the software engineer completed a good, so also the two, three and so on to the engineer to do
Through the above process, we can draw some conclusions:

    1. A viewgroup if Onintercepttouchevent returns false, it means that ViewGroup does not intercept the event, but instead passes it to the View#dispatchtouchevent method
    2. Events are always distributed by parent elements to child elements
    3. A view if Ontouchevent returns True, indicating that the event is consumed, the result is passed directly back to the activity by the Dispatchtouchevent method
    4. If a view consumes Action_down events, subsequent events in the sequence of events will also be processed (except in special cases, such as after events in the sequence).

Third, do not consume events in view

We now modify the XML portion of the sample code, android:clickable= "true" to all android:clickable= "false"

<?xml version= "1.0" encoding= "Utf-8"?> <com.idtk.customscroll.parentview xmlns:android= "http://" Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:padding=" 10DP "tools:context="
    Com.idtk.customscroll.MainActivity "> <com.idtk.customscroll.childview android:layout_width=" match_parent " android:layout_height= "Match_parent" android:src= "@drawable/zhiqinchun" android:clickable= "false"/> < Com.idtk.customscroll.ChildView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andro id:src= "@drawable/hanzhan" android:clickable= "false"/> <com.idtk.customscroll.childview Android:layout_wi Dth= "Match_parent" android:layout_height= "match_parent" android:src= "@drawable/shengui" android:clickable= "FAL Se "/> <com.idtk.customscroll.childview android:layout_width=" MATCH_parent "android:layout_height=" match_parent "android:src=" @drawable/dayu "android:clickable=" false "/> &L

 T;/com.idtk.customscroll.parentview>

Then click again to view the newly printed log

Now, according to the logic shown in log, the flowchart of Action_down event and ACTION_UP event is plotted separately.

Let's sort out the process of this sequence of events:

    1. The Action_down event is passed the same as before, except that the return value is passed
    2. Because it is not clickable, View#ontouchevent returns a value of false, passing it to its own Dispatchtouchevent method, and then passing it to the Viewgroup#dispatchtouchevent method, and pass it to the Viewgroup#ontouchevent method.
    3. ViewGroup returns FALSE, the Action_down event is handled by the Activity#ontouchevent method, but it still returns false, and the last Action_down event returns the result is False
    4. The Action_up event is passed directly to the Activity#ontouch method processing after discovering view, ViewGroup does not handle the Action_down event, and the return result of the processing return FALSE,ACTION_UP event is false

This is done using the job scenario: it's still the Boss (activity), the project Manager (ViewGroup), the software engineer (View) handing over the task to the project manager, the project manager handing over the task to the engineer, and this process is the same as the previous example. The difference is that the software engineer did not complete the task (View#ontouchevent returned false), told the project manager I did not finish, and then the project manager himself tried, likewise not completed (Viewgroup#ontouchevent return false), The project manager told the boss that I didn't finish it, and then the boss tried it and didn't finish the task (Activity#ontouchevent returned false), but then there were two and three of the project, but the boss knew you couldn't do it, so he tried it himself. But it was so miserable that it didn't finish. (This is a bit different from normal, but just for example)

By combining the above two examples, we can draw some conclusions:

    1. A view if Ontouchevent returns False, indicating that the event was not consumed, the event is passed to the ontouchevent of its parent view for processing
    2. A view if it does not consume Action_down events, then the subsequent events of this sequence will not be handled by it.
    3. If the event does not have a view to process it, then the activity is finally processed
    4. View default ontouchevent will consume events, return True when view is clickable, do not consume events, return False (longclickable, readers can test themselves, and the result is the same as clickable )

Iv. interception of events in ViewGroup

Events in the distribution of interception, where I divided it into 2 kinds, one is in the Action_down event, the interception, the other is in the sequence of events after Action_down, the event was intercepted.

1. Intercept at the beginning of the event

In order to achieve the effect of intercepting the touch event at the outset in the viewgroup, we need to modify the intercept=true that I commented out in the last part of the Parentview#onintercepttouchevent method; Then increase the android:clickable= "true" attribute for the Parentview in Activity_main.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <com.idtk.customscroll.parentview xmlns:android= "http://" Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:padding=" 10DP "tools:context="
    Com.idtk.customscroll.MainActivity "> <com.idtk.customscroll.childview android:layout_width=" match_parent " android:layout_height= "Match_parent" android:src= "@drawable/zhiqinchun" android:clickable= "true"/> <c Om.idtk.customscroll.ChildView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Androi d:src= "@drawable/hanzhan" android:clickable= "true"/> <com.idtk.customscroll.childview Android:layout_widt H= "Match_parent" android:layout_height= "match_parent" android:src= "@drawable/shengui" android:clickable= "true" /> <com.idtk.customscroll.childview android:layout_width= "match_pArent "android:layout_height=" match_parent android:src= "@drawable/dayu" android:clickable= "true"/> </c

 Om.idtk.customscroll.parentview>

Now let's take a look at the event flow chart of the interception case

Most of this is the same as the previous example, the main difference is that in the Viewgroup#onintercepttouchevent method, the passing event is intercepted, the return True,action_down event is passed to the viewgroup# Ontouchevent, the Action_down after the event is the same as the previous example. Another important difference is that after the event is intercepted by ViewGroup, the remainder of this sequence of events, after entering the Viewgroup#dispatchtouchevent method, does not need to be judged on whether to intercept the event, But directly into the Ontouchevent method.

Use the situation in the work to simulate: the Boss (activity), Project Manager (ViewGroup), software engineer (View) boss of the task to the project manager, the project manager that the project is more difficult, so decided to deal with themselves (viewgroup# Onintercepttouchevent,return true), the project manager was more powerful he finished the task (Viewgroup#ontouchevent,return True), and then he told the boss that he had finished (return true, viewgroup#dispatchtouchevent→activity#dispatchtouchevent). The boss will still hand over the task to the project manager, who knows how difficult the task is, so don't think for themselves (that is, the rest of the events in this sequence is not viewgroup#onintercepttouchevent).

From the above example, we can draw some conclusions:

A viewgroup if Onintercepttouchevent returns True, ViewGroup intercepts the event and passes the event to its Ontouchevent method for processing

A viewgroup if its onintercepttouchevent returns True, the subsequent events in this sequence of events are not onintercepttouchevent judged, Instead, it is passed directly to the Ontouchevent method for processing by its Dispatchtouchevent method.

2. Intercept in the event sequence

Here The example used is restored to the initial state, and then I intercept = true of the two annotations in the Parentview#onintercepttouchevent method, switch, and the last part Intercept = True , comment out again.

<?xml version= "1.0" encoding= "Utf-8"?> <com.idtk.customscroll.parentview xmlns:android= "http://" Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ Parent "android:layout_height=" match_parent "android:padding=" 10DP "tools:context="
    Com.idtk.customscroll.MainActivity "> <com.idtk.customscroll.childview android:layout_width=" match_parent " android:layout_height= "Match_parent" android:src= "@drawable/zhiqinchun" android:clickable= "true"/> <c Om.idtk.customscroll.ChildView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Androi d:src= "@drawable/hanzhan" android:clickable= "true"/> <com.idtk.customscroll.childview Android:layout_widt H= "Match_parent" android:layout_height= "match_parent" android:src= "@drawable/shengui" android:clickable= "true" /> <com.idtk.customscroll.childview android:layout_width= "match_pArent "android:layout_height=" match_parent android:src= "@drawable/dayu" android:clickable= "true"/> </c

 Om.idtk.customscroll.parentview>

After running again, slide a picture and look at the log

This is divided into two pictures, because there are a lot of action_move, in order to facilitate observation, so only intercept the log's beginning and end. The key part here, is the red box in the Action_cancel, you can see the Action_down event passed Onintercepttouchevent did not intercept, return false, after the event Action_ When move enters Onintercepttouchevent again, ViewGroup intercepts the event, which passes a Action_cancel event to the view, and the Action_move event is no longer passed to view.

Use the situation in the work to simulate: Boss (activity), Project Manager (ViewGroup), software Engineer (View) The situation here is that the first phase of the task as in the case of the same, by the software engineer to complete, but suddenly the project manager felt that the task of two is a little difficult, Then decide to do it yourself. At this point to the engineer said that the follow-up task of this project, do not you to complete (action_cancel).

A conclusion can also be drawn from here:

After a view has received the Action_down, in subsequent events of this sequence, if it is intercepted at some point by the parent view, the view receives a Action_cancel event and no subsequent events in the sequence of events are received.

V. Summary

This paper draws the following conclusions by using the various logs that are printed in the example to carry out the Android event distribution mechanism.

    1. An event sequence is a series of events that begin with touching the screen from the finger to the end of the finger leaving the screen. A sequence of events begins with the Action_down event, and the middle may pass through several move to action_up the event to an end.
    2. The process of passing an event is an extrovert, that is, the event is always distributed by the parent element to the child element
    3. If a view consumes Action_down events, then normally, subsequent events in this sequence of events will also be processed, but can intercept subsequent events by calling the Onintercepttouchevent method of their parent view
    4. If a view does not consume Action_down events, subsequent events of this sequence will not be handled by it.
    5. If the event does not have a view to process it, then the activity is finally processed
    6. If the result of the event is true, the result of the return is passed directly to the activity by calling the parent View#dispatchtouchevent method continuously, and if the result of the event is false, the result of the return is constantly invoked by the parent view# Ontouchevent method to get the return result.
    7. View default ontouchevent will consume events, return True when view is clickable, do not consume events, return False (longclickable, readers can test themselves, and the result is the same as clickable )
    8. If a viewgroup onintercepttouchevent returns true, subsequent events in this sequence of events are not judged onintercepttouchevent. Instead, it is passed directly to the Ontouchevent method for processing by its Dispatchtouchevent method.
    9. If a view receives a action_down, the subsequent events of this sequence are intercepted at a certain moment by the parent view, the view receives a Action_cancel event and no further events in the sequence of events are received.

Activity
event-related methods Method Features ViewGroup View
public Boolean Dispatchtouchevent Event distribution Yes Yes Yes
public Boolean Onintercepttouchevent Event interception No Yes No
public Boolean Ontouchevent Event consumption Yes Yes Yes

The above is the distribution of information on the Android event collation, follow-up to continue to supplement the relevant information, thank you for your support for this site!

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.