Android Touch event distribution mechanism: androidtouch

Source: Internet
Author: User

Android Touch event distribution mechanism: androidtouch

Preface

The delivery of touch events in Android is worth studying. I have never seen you introduce a ListView slide function, so that ListView will not follow your finger's call to scroll. I also don't know why the Button has set onClick and onTouch. Who will first respond; maybe you will ask what is the difference between onTouch and onTouchEvent and how to use it? Everything here, as long as you understand the event distribution mechanism, you will find that it is not a problem to explain it!

Methods for related Touch events

1. public boolean dispatchTouchEvent (MotionEvent ev) ---- Event distribution method, which is called by Event Distribution

2. public boolean onInterceptTouchEvent (MotionEvent ev) ---- the method used to intercept the Event.

3. public boolean onTouchEvent (MotionEvent event) ---- Event response method, which processes the calls of the event

Classes with the above events

1. Activity Class (Activity and its inheritance subclass)

DispatchTouchEvent (), onTouchEvent ()

2. ViewGroup class (LinearLayout, FrameLayout, ListView, etc .....)

DispatchTouchEvent (), onInterceptTouchEvent (), onTouchEvent ()

3. View class (such as Button and TextView .....)

DispatchTouchEvent (), onTouchEvent ()

PS: The ViewGroup has the onInterceptTouchEvent () method. The other two methods share the onInterceptTouchEvent () method.

Analysis of simple usage of the Method

We can find that the return values of these three methods are of the boolean type. In fact, they determine the next transmission processing direction through the return value.

1. dispatchTouchEvent () -- used to distribute events

This method distributes root element events from top to bottom to the Inner sub-element until they are terminated or reach the innermost element. This method also uses a Tunnel Method for distribution. OnInterceptTouchEvent () and onTouchEvent () will be called, and will not be overwritten in general.

If the return value is false, the system does not intercept and continues to distribute the event. If the return value is true, the system blocks the event and does not distribute the event to lower-level elements. By default, the system returns false in the dispatchTouchEvent () method.

2. onInterceptTouchEvent () -- used to intercept events

In the ViewGroup source code, this method returns false and does not intercept events. The Touch event is passed down to its subview.

If we override this method and return true, the event will be intercepted and processed by the current ViewGroup, And the onTouchEvent () method of this class will be called.

3. onTouchEvent () -- used to process events

If true is returned, the View can process the event, and the event is terminated and passed up (to its parent View)

If false is returned, the event cannot be processed. The event is passed to the onTouchEvent () method of its parent View for processing.

Practical drills

Well, it doesn't matter if the above things don't understand. Now you need to open your eyes and see it, because the following uses an example to explain the event distribution mechanism of Touch, we believe that this distribution mechanism can be better understood.

In this example, we need to rewrite four classes:

1. Boss --> MyActivity

2. manager --> FrameLayout

3. Leader --> LineaLayout

4. employee --> TextView

 The software interface diagram is as follows:

Let's start with an easy-to-understand example.]

The boss said to the Minister: I want to eat braised fish. The Minister said to the manager, "You are a braised fish manager." The manager said to the employee: "You are a braised fish ...... (The employee did it and did not burn the fish out.) The employee said: I have worked overtime all night, but I still haven't done it yet. Don't you deduct my salary? The team lead said: You stupid, white hair, your salary, I will not contact you next time ...... (The team leader did it or didn't do it.) The team leader said to the manager, "I did my best, but I still didn't do it. Never fire my squid." The manager said, "You are a waste, I can only do this myself ...... (The manager did it and succeeded.) The manager said to the Minister: braised fish has done a good job. The Minister replied: "Yes, I will ask your minister to say something to the boss next time: the boss responded: Yes, I will contact you next time. ---------------------------The boss said to the Minister, "I want to eat boiled fish. The Minister said to the manager," You are a boiled fish manager. Think about it yourself. The team lead cannot even cook braised fish. I will not look for him this time. I will do it myself ...... (The manager did it and succeeded again.) The manager said to the Minister: braised fish has done a good job. The Minister replied: "Yes, I will ask your minister to say something to the boss next time: the boss responded: Yes, I will contact you next time. ---------------------------

1. According to common sense, leaders will distribute tasks down. Once the following persons do not do well, they will not hand over the subsequent tasks to the following persons. They can only do the tasks themselves, if you can't do it yourself, you can only tell the superior that the task cannot be completed, and the superior will repeat his process.

2. In addition, the leaders have the right to intercept the task and conceal the task from their subordinates. If the task fails, they can only report to their superiors that the task cannot be completed.

Process demonstration

[1]. Assume that the employee's ability is insufficient, that is, if the onTouchEvent () method of TextView is set to false, the event cannot be consumed.

Event Transfer Process:

[2]. Assume that the employee can handle the event, that is, the onTouchEvent () method of TextView is set to true, indicating that the employee can process the event.

Event Transfer Process:

[3] assume that the staff and team lead are insufficient in ability, that is, the onTouch () of TextView and LinearLayout returns false, but the manager solves this problem, that is, the onTouch () of FrameLayout returns true.

 

Event Transfer Process:

[4] Suppose our team lead decides to intercept the event and will not send it to the following employees when the event is distributed to him, that is, onInterceptTouchEvent () returns true, the onTouchEvent () returns true.

Event Transfer Process:

Make a summary

1. Obviously, these processes are the processing results of dispatchTouchEvent (), but the premise is that we do not completely implement this method again, that is, to ensure that return super is required. dispatchTouchEvent (ev); to determine whether the method of the parent class is called. These events will be passed from top to bottom layer until they are passed to the bottom-layer View element. At this time, the onTouchEvent () method of the View will be called to process the event; returns true to indicate that the event has been successfully processed. If false is returned, the event is not processed successfully. The event is passed up layer by layer and handed over to the onTouchEvent () method of the upper View for processing, similarly, if a View successfully processes the event or returns false to the top-level View, the event is discarded and disappears.

2. If the event is intercepted midway down, that is, the onInterceptTouchEvent () method of the View returns true, the event will stop being passed down, the onTouchEvent () method at the layer is used for processing. no matter whether the processing is successful or not, the underlying View will no longer receive the event. PS: If the processing fails, it will be handled by the onTouchEvent () method of the upper View.

3. dispatchTouchEvent () has the memory function. If the first event is passed down to a View, it will pass the event to its subview, it records whether the event is processed successfully by the View below it (how can we know? If the event will be passed up to me again and handled by my onTouchEvent (), it means that none of the following views can process the event successfully ); when the second event is passed down to the View, the dispatchTouchEvent () method of the View is used to determine whether the previous event is successfully handled by the following view, then the event will be handed over to the following for processing. If the previous event is not successfully handled by the following, the event will not be passed down, the View directly calls its onTouchEvent () method to process the event.

4. The information of the memory function is valid only before a series of events are completed. For example, the information of the memory function is cleared from the ACTION_DOWN event until the ACTION_MOVE and ACTION_UP events are completed. That is to say, if a View fails to process the ACTION_DOWN event (onTouchEvent () returns false), the subsequent ACTION_MOVE, ACTION_UP and other events will not be passed to the View, and the parent View will handle it by itself. The next ACTION_DOWN event will still be passed to this View.

Additional code

1. MyActivity

Public class MyActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Override public boolean dispatchTouchEvent (MotionEvent ev) {Log. I ("test", "[Boss]:" + Util. actionToString (ev. getAction () + ", ask someone to help me complete the task and distribute it down. "); Return super. dispatchTouchEvent (ev) ;}@ Override public boolean onTouchEvent (MotionEvent event) {boolean relust = false; Log. I ("test", "[Boss] to complete the task:" + Util. actionToString (event. getAction () + ", [Manager] is too bad. I will not work for you anymore. I will do it myself! Solved: "+ Util. canDoTaskTop (relust); return relust ;}}

2. MyFrameLayout:

Public class MyFrameLayout extends FrameLayout {public MyFrameLayout (Context context, AttributeSet attrs) {super (context, attrs);} @ Override public boolean dispatchTouchEvent (MotionEvent ev) {Log. I ("test", "[Manager]:" + Util. actionToString (ev. getAction () + ", ask someone to help me complete the task and distribute it down. "); Return super. dispatchTouchEvent (ev) ;}@ Override public boolean onInterceptTouchEvent (MotionEvent ev) {boolean relust = false; Log. I ("test", "[Manager] whether to intercept tasks:" + Util. actionToString (ev. getAction () + ", stop? "+ Relust); return relust ;}@ Override public boolean onTouchEvent (MotionEvent event) {boolean relust = true; Log. I ("test", "[Manager] to complete the task:" + Util. actionToString (event. getAction () + ", [team lead] is too bad. I will not work for you anymore. I will do it myself! Solved: "+ Util. canDoTask (relust); return relust ;}}

3. MyLinearLayout

Public class MyLinearLayout extends LinearLayout {public MyLinearLayout (Context context, AttributeSet attrs) {super (context, attrs);} @ Override public boolean dispatchTouchEvent (MotionEvent ev) {Log. I ("test", "[team lead]:" + Util. actionToString (ev. getAction () + ", ask someone to help me complete the task and distribute it down. "); Return super. dispatchTouchEvent (ev) ;}@ Override public boolean onInterceptTouchEvent (MotionEvent ev) {boolean relust = true; Log. I ("test", "[team lead] whether to intercept tasks:" + Util. actionToString (ev. getAction () + ", stop? "+ Relust); return relust ;}@ Override public boolean onTouchEvent (MotionEvent event) {boolean relust = true; Log. I ("test", "[team lead] to complete the task:" + Util. actionToString (event. getAction () + ", [employee] is too bad. I will not work for you anymore. I will do it myself! Solved: "+ Util. canDoTask (relust); return relust ;}}

4. MyTextView

Public class MyTextView extends TextView {public MyTextView (Context context, AttributeSet attrs) {super (context, attrs ); // TODO Auto-generated constructor stub} @ Override public boolean dispatchTouchEvent (MotionEvent event) {Log. I ("test", "[employee]:" + Util. actionToString (event. getAction () + ", no more, alas ~ "); Return super. dispatchTouchEvent (event);} @ Override public boolean onTouchEvent (MotionEvent event) {boolean relust = false; Log. I ("test", "[employee] to complete the task:" + Util. actionToString (event. getAction () + ", [employee] Now only depends on oneself! Solved: "+ Util. canDoTask (relust); return relust ;}}

5. Util (Tool class)

Public class Util {public static String actionToString (int action) {String result = null; switch (action) {case MotionEvent. ACTION_DOWN: result = "ACTION_DOWN"; break; case MotionEvent. ACTION_MOVE: result = "ACTION_MOVE"; break; case MotionEvent. ACTION_UP: result = "ACTION_UP"; break;} return result;} public static String canDoTask (boolean can) {String result = null; if (can) {result = "perfect solution for this task! ";} Else {result =" This cannot be done, but it should be handed over to the boss. ";}Return result;} public static String canDoTaskTop (boolean can) {String result = null; if (can) {result =" perfect solution for this task! ";} Else {result =" This cannot be done. Give up the task. ";}Return result ;}}

 

By enjoy wind chimes
Source:Http://www.cnblogs.com/net168/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article. However, you must keep this statement without the author's consent and provide a connection to the original article on the article page. Otherwise, you will not be reposted next time.

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.