Android Touch Event delivery mechanism popular explanation

Source: Internet
Author: User
Tags terminates

Before we talk, we'll talk about a little story about mission delivery, throwing bricks under the jade:

In the words of a software company, a task is assigned to the development manager to complete:

The development manager got it, looked at it, felt so simple, so

Development Manager: Assigned to the development team leader

Development team Leader: assigned to a member (programmer)

Programmer: Assigned to an intern of his own.

Intern: Good bitter force, unable to assign, how to do? You can do it yourself.

But there are two things that interns can do.

Situation One:

Intern: After a period of research, pondering, stay up, struggle, death knock, Emperor not bear Ah, completed.

Then came a similar task, which was passed down in this way (development manager, developer, programmer-based intern), and the intern was completed.

Situation Two:

Intern: After a period of research, pondering, is no clue, can not be completed, can only ask Master (programmer).

Programmer: Ah, I did not pay attention to the interns, the task is very difficult ah, their own research, there is no clue, no way can only request the leader.

Development team Leader: This task is not difficult ah, how I under the people will not, no way, can only own, after, a period of time, completed, feel, if again have with this very similar to the task, I got it, do not get them.

And then came a similar task, passed on like this

Development Manager: Assigned to development team leader

Development team Leader: Ah, again follow a very similar task, I do it, not too long has been completed!

PS: The above is a task delivery process. The boss always wants to assign the task to his subordinates at first. Then the task is passed down one step at a time, finally by a person to complete him, of course, pass, the bottom of the people will not, also will step by step rollback, and his superiors to complete, if there is a similar task, the node will not be passed to him, Not for him either. Of course, the delivery of these tasks may also be assigned to intercept, for example, the development team leader intended to assign to the programmer, suddenly he did not want to pass, on the way to intercept down.

And in our Android Touch event delivery mechanism is similar to this, the idea is almost, there is a saying, design comes from life. There are three main ways to deal with touch events:

Dispatch Event Public boolean dispatchtouchevent (motionevent ev)//Intercept Event public boolean onintercepttouchevent (motionevent ev)// Handling Event Public boolean ontouchevent (Motionevent event)

When we use them, we just need to rewrite, we can operate, these three methods are mainly called in three kinds, that three kinds?

Base class Example Class The method of owning
Inheriting activity (activity Class) Mainactivity (varies by project)
Dispatchtouchevent,ontouchevent
Inherit ViewGroup (View container) Relativelayout,framelayout,linearlayout,absolutelayout,listview,scrollview ...
Dispatchtouchevent,onintercepttouchevent,ontouchevent
Inherit view (View control) Button,edittext,textview,imageview ....
Dispatchtouchevent,ontouchevent

And what is the meaning of these three methods?

Event Meaning
Dispatchtouchevent
Used to dispatch events.
where Onintercepttouchevent () and ontouchevent () are called, the method is not generally overridden
Onintercepttouchevent
Used to intercept events.
The source code implementation in the ViewGroup class is {return false;} Indicates that the event is not intercepted,
The event is passed down (passed to its child view);
If you manually override the method so that it returns true to indicate interception, the event will stop passing down,
Event is handled by the current ViewGroup class, which is called the Ontouchevent () method of the class
Ontouchevent
Used to handle events.
Returns true to indicate that the view handles the event, and the event terminates the pass-up (passed to its parent view);
Returns false to indicate that it cannot be processed, then the event is passed to its parent view's Ontouchevent () method to handle

According to the small story above, we use the code to achieve:

I've created four new classes: mainactivity (development manager), Firstframelayout (development team leader), Secondrelativelayout (programmer), Thirdtextview (intern)

The hierarchical relationships in the interface are as follows:

I'm like the story in front of us. Situation one, situation two so simulated under:

Situation One

First of all, we follow the story of the above, the task-level pass down (the onintercepttouchevent of the ViewGroup class will return false), and then the event will be passed to, Thirdtextview, finally, We return its ontouchevent to ture (meaning that the intern has successfully handled it). We click on the blue area. Let's look at the log.

Situation Two

First of all, we're going to pass down the class one (returning the onintercepttouchevent of ViewGroup classes to false), and then to Thirdtextview, We return its ontouchevent to false (meaning that the intern has failed), then Secondrelativelayout's ontouchevent returns False (meaning that the programmer has failed), and finally says Firstframelayout Ontouchevent Returns True (meaning that the team leader has successfully processed). We click on the blue area. Let's look at the logs, like.

Note: The above two cases of the log diagram, the red box represents the first task, the yellow box represents a second similar task. According to the log, we draw the pass diagram:

Scenario One event pass diagram:

Scenario Two event pass diagram:

From the above two event pass diagram we come out with some conclusions:

1. The event is preceded by a dispatchtouchevent assignment to the next level

2. To pass the Onintercepttouchevent whether it is necessary to intercept, not intercept the next level, and ultimately pass to the view control,

In the 3.onTouchEvent method, if True is returned in the processing event, the pass is signaled and the delivery terminates. No, no, if not, this will return to the previous level of the Ontouchevent method, and if it is false, it will go up to the Ontouchevent method in the previous layer.

Curious we will find a problem: The Yellow line in Figure two, not like the red line, first passed to the bottom, and then rolled back back, this is why?

A: As in our story, he knows, the bottom person will not go to do, then why he also assigned to him, he will do it himself, directly to Ontouchevent, this is the event transmission of "memory" function. We are finger-clicked in the blue area, the touch event has two, the first Action_down, the second action_up, the first Action_down event is passed down to a view, it continues to pass the event to its child view, It will record whether the event was successfully processed by the view below it, (how can you tell?). If the event is passed up again to me here for my ontouchevent, it means that the view below is not able to handle the event successfully, and when the second ACTION_UP event is passed down to the view, The view of the Dispatchtouchevent method of the opportunity to judge, if the last event was successfully processed by the following view, then the event will continue to the following to deal with, if the last event has not been processed by the following success, then this event will not be passed down, The view directly calls its own Ontouchevent method to handle the event.

PS: Information about this "memory" function is only valid until a series of events have been completed, that is, from the Action_down event, until the end of the subsequent event action_move,action_up, the "memory" message will be erased. That is, if a view processing Action_down event fails (Ontouchevent () returns false), subsequent events such as ACTION_MOVE,ACTION_UP are no longer passed to the view, which is handled by the parent view itself. The next time the Action_down event occurs, it will still be delivered to the view.

Additional notes:

-If it is intercepted in the process of passing down, that is, if the Onintercepttouchevent method returns True, then the event will stop passing down, directly by the current Ontouchevent method, if the processing succeeds OK, if the processing is unsuccessful, the event will be passed up.

Android Touch Event delivery mechanism popular explanation

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.