Transferred from: http://hunankeda110.iteye.com/blog/1944311
Events in Android are divided into keystroke events and touch events, where touch events are described. The touch event consists of a action_down,n action_move, a action_up composed of onclick,onlongclick,onscroll and other events. Controls in Android inherit view's base class, and controls fall into two categories: one that inherits the control that the view cannot contain, and one that inherits the control that ViewGroup can contain other controls, such as a container control, such as Listview,gridview, LinearLayout and so on.
Here are some of the functions explained below.
Ø Public Boolean dispatchtouchevent (Motioneventev) This method distributes TouchEvent
Ø public booleanonintercepttouchevent (motionevent ev) This method intercepts TouchEvent
Ø Public Boolean ontouchevent (motionevent ev) This method handles TouchEvent
The view class has Dispatchtouchevent and Ontouchevent two methods, viewgroup inherit view, and a new Onintercepttouchevent method is added. There are no onintercepttouchevent methods in activity, but there are two other methods. We can see that all 3 of the above methods return a Boolean, what does that mean?
? public boolean dispatchtouchevent (motionevent ev)
The Activity explains:
Called to process touch screens events. You can override this to intercept all touch screens events before they aredispatched to the window. Be sure to the implementation for touch screenevents that should be handled normally.
Parameters
|
Ev |
The touch screen event. |
Returns
· Boolean Return True if this event is consumed.
It will be called to handle touch screen events and can override this method to intercept all touch screen events before these events are distributed to the window. You should usually handle touch screen events, so be sure to call this implementation. When the return value is true, it indicates that the event has been consumed. For example, in Textactivity, Dispatchtouchevent returns True in Action_move, and the result is as follows:
That is, it did not distribute the action_move.
public boolean onintercepttouchevent (motionevent ev)
Implementthis method to intercept all touch screen motion events. This allows Towatch events as they is dispatched to your children, and take ownership of thecurrent gesture at any PO Int.
Usingthis function takes some care, as it had a fairly complicated interaction with view.ontouchevent (motionevent), and USI Ng it requires implementing that method as well as the this one in Thecorrect. Events would be received in the following order:
1. You'll receive the down event here.
2. The down event is handled either by a child of this viewgroup, or given to your own ontouchevent () method to Ha Ndle; This means youshould implement Ontouchevent () to return true, so you'll continue to see therest of the gesture (instead of looking for a, parent view to handle it). Also,by returning True from Ontouchevent (), you won't receive any followingevents in Onintercepttouchevent () and all to Uch processing must happen inontouchevent () like normal.
3. As long as you return false from this function, eachfollowing event (up to and including the final up) would be D Elivered first HEREand then to the target ' s ontouchevent ().
4. If you return true from this, you won't receive any followingevents:the target view would receive the same event But with the action Action_cancel, and all further events would be delivered to Yourontouchevent () method and no longer app Ear here.
Parameters
|
Ev |
The motion event being dispatched down the hierarchy. |
Returns
· Return true to steal motionevents from the children and has them dispatched to this ViewGroup throughontouchevent (). The current target would receive an Action_cancel event, and nofurther messages would be a delivered here.
The basic meaning is:
1. Action_down is first passed to the Onintercepttouchevent () method
2. If the ViewGroup onintercepttouchevent () return false after receiving the down event processing, subsequent moves, up, and so on will continue to be passed to the ViewGroup first, It is then passed to the ontouchevent () processing of the final target view like the down event.
3. If the ViewGroup onintercepttouchevent () return true after receiving the down event processing, subsequent moves, up, and so on will no longer be passed to Onintercepttouchevent (), Instead, the ontouchevent () process passed to the viewgroup like the down event, noting that the target view will not receive any events.
4. If the Ontouchevent () of the view that eventually needs to handle the event returns false, the event is passed to the ontouchevent () processing of the view at its previous level.
5. If the ontouchevent () of the view that eventually needs to handle the event returns TRUE, then subsequent events will continue to be passed to the view's ontouchevent () processing.
Android Touch Event Delivery mechanism:
We can look at the Android source code:
In Activity.java
Ignore the Onuserinteraction method because it's just an empty method if you don't realize it. GetWindow (). superdispatchtouchevent (EV). where GetWindow () returns the Phonewindow.
Phonewindow.java:
This function calls Super.dispatchtouchevent (event), and the activity's Rootview is Phonewindow.decorview, which inherits Framelayout. By Super.dispatchtouchevent, the touch event is distributed to each activity by a sub view. At the same time I can see that if the child view intercepts the event, the Ontouchevent function is not executed.
Dispatchtouchevent method in Viewgroup.java:
Because the code is too long to be posted here, but also know that it is returning
return target.dispatchtouchevent (EV);
Here, target refers to the object being distributed, either by itself or by its child view.
The Onintercepttouchevent method in Viewgroup.java:
False is returned by default. That is, the touch event is not intercepted.
The Dispatchtouchevent method in View.java
Here we know very well that if the IF condition is not true, the return value of Dispatchtouchevent is the return value of Ontouchevent
The Ontouchevent method in View.java
So it's easy to get the touch event default processing flow (take the Action_down event as an example):
When the touch event Action_down occurs, the Dispatchtouchevent function in activity is called first, and then the Action_down event is passed to the Dispatchtouchevent function in ViewGroup. The Action_down event in Dispatchtouchevent in ViewGroup is then passed to the Onintercepttouchevent function in the call ViewGroup, which is responsible for intercepting the Action_down event. Because ViewGroup also contains child view, the default return value is False, which means that this action_down event is not intercepted. If False is returned, the Action_down event continues to be passed to its child view. Because child view is not a ViewGroup control, the Action_down event is then passed to ontouchevent for processing events. At this point, the delivery of the message basically ends. From the above can be analyzed, the Motionevent event is passed by tunnel mode. Tunnel mode, which is passed from the root element down to the most inner child element or in an intermediate element because a condition stops passing.
The next step is to analyze and handle the event. Just now the Action_down event was passed to the view's Ontouchevent function, which returns true by default, and then the dispatchtouchevent of the view returns TRUE. Then the ViewGroup dispatchtouchevent returns True, and the last activity Dispatchtouchevent returns true. We found that the Motionevent event was handled in a bubbling fashion. The bubbling way, which is passed from the most inner child element to the root element, or to an element in the middle, in order to stop passing by a certain condition.
Debug the results for the program:
Action_down Event Output:
Action_move Event Output:
Now let's make some changes, and then take Action_down as an example.
Situation One:
We action_down return falsein the view ontouchevent, the output is as follows:
You can find that the Action_down event is passed to the upper ViewGroup ontouchevent, while returning true, indicating that the event was consumed by the viewgroup. The next Touch event (Action_move, etc.) is no longer passed to view, only passed to ViewGroup, and the touch event is handled by ViewGroup's ontouchevent function. At the same time Onintercepttouchevent is no longer called.
Situation Two:
We action_move return falsein the view ontouchevent, the output is as follows:
Because view does not consume this action_move event, it should be the principle to bubble event processing to ViewGroup, but the result is activity processing. We know that the first thing that happens in touch events is the Action_down event, which we explain in onintercepttouchevent to find differences between Action_down and Action_move events, Action_ The down event, as the starting event, is more important than Action_move and action_up, and if Action_move or action_up occurs, then there must have been action_down. That is, the Action_down event was consumed by the view, and the Action_move event was not consumed, passed to ViewGroup, because before ViewGroup did not deal with the Action_down event, so it does not handle action_move. But the activity is not the same, it can accept all events.
Situation Three:
This time in Onintercepttouchevent in ViewGroup, Action_down returns true .
The results are as follows:
It sends events directly to ViewGroup's ontouchevent processing, and thereafter no longer intercepts the ontouchevent processing of events directly to ViewGroup.
Situation Four:
Action_move returns true in Onintercepttouchevent in ViewGroup
The results are as follows:
Action_move by ViewGroup Intercept, the last processing Action_down view will receive Action_cancel event, then ViewGroup no longer intercept subsequent events, Events are handled directly in the ViewGroup ontouchevent.
There are many other things that are not listed here.
At the beginning of writing this demo, I found that overriding the Ontouchevent function would not get the onclick and Onlongclick events. The next step is to discuss how the Ontouchevent,android distinguishes the Onclick,onlongclick event when overridden. It is important to understand how to respond to various events in the UI, such as apps icons like Android Desktop, you can click, and then press and hold to drag.
Android Onclick,onlongclick are all made up of action_down,action_up. If ontouchevent, onclick, Onlongclick are overridden in the same view. Ontouchevent first captures unit events such as Action_down, ACTION_UP, and so on. The onclick, Onlongclick event may occur next. An onclick event is made up of Action_down and action_up. A Onlongclick event has at least one action_down. How is the Android specific implementation, you can look at the source code:
In View.java:
I've shown the Ontouchevent method above, but since I've folded a part of the code for too long, now expand
Execution in this if condition is the Click event Handling and Longclick event handling. First look at the Action_down event
We see a postdelayed method, which means that the thread is inserted into the message queue for a delay. A postdelayed method is triggered after Action_down. Mpendingcheckfortap belongs to the Checkfortap instance.
In the inside open a line Chengdang for long_clickable, call the Postcheckforlongclick method.
Look at mpendingcheckforlongpress this thread again.
The Performlongclick method is called when all the above conditions are met.
This method invokes the Onlongclick function that we are familiar with.
At this point the Onlongclick event has been parsed out. And then watch the Action_up event.
Direct attention to the PerformClick function:
Here we also see the OnClick method we are familiar with.
So android this mechanism is guaranteed that this onclick and onlongclick can coexist with ontouchevent. Next consider whether the onclick and onlongclick coexist, in fact, the question has been elaborated before. As long as this event is not consumed, it will continue to pass. It is known from above that Onlongclick is executed on a separate thread, which occurs before action_up. The onclick occurs after action_up, that is, if Onlongclick returns False,onclick, and Onlongclick returns True, this event is already consumed. OnClick no longer occurs.
return false:
Returns True
If the onclick event is set more than once, the topmost onclick overrides the underlying OnClick event, and the Onlongclick event is set more than once, only the Onlongclick method of the underlying view is executed. Returns false after the Action_down call.
You can see that the action_down is consumed, so the upper layer will not be processed.
Android Event distribution mechanism