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.
Scenario 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:
// Assigning Events Public Boolean dispatchtouchevent (motionevent ev) // publicboolean onintercepttouchevent (motionevent ev)// handling Events Publicboolean ontouchevent (Motionevent event)
In their time, we just need to rewrite, we can operate, these three methods are mainly called in three kinds, which 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 (return the onintercepttouchevent of the ViewGroup class to false), and the event will be passed to , Thirdtextview, finally, we put its ontouchevent back to ture(meaning that the intern handled it successfully). We click on the blue area. Let's look at the log.
Situation Two
First of all, we're going to pass it down (ViewGroup class's onintercepttouchevent all back to false), and then it's passed to Thirdtextview, and we'll ontouchevent return false(meaning that intern processing failed), then secondrelativelayout ontouchevent return false (meaning that the programmer failed to handle it), and finally, Firstframelayout's ontouchevent returns true(meaning that the team leader handled it successfully). 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,
3. In theontouchevent method, if True is returned in the processing event, the delivery will be terminated. 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, which records 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; when the second action_up event is passed down to the view, the view's Dispatchtouchevent Method 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 is not successfully processed below, 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 the onintercepttouchevent method returns trueif it is intercepted in the process of passing down, the event will stop passing down and be processed directly by the current ontouchevent method. If the processing succeeds then OK, if the processing is unsuccessful, the event will be passed up.
Thanks for watching.
Instance code: http://download.csdn.net/detail/chenjie_920/8398413
Android Touch event delivery mechanism parsing