Objective
The distribution of touch events in Android is a worthwhile thing to study. I didn't see you. introduced a ListView slide function, the ListView does not listen to the fingers of your finger to scroll, do not know why the button set the onclick and Ontouch, who will first respond to Perhaps 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 explaining this is not a thing!
How to Relate touch events
1. Publicboolean dispatchtouchevent (motionevent ev) ———— Event distribution method, which is called by the
2. Publicboolean onintercepttouchevent (motionevent ev) ———— event interception method to intercept the call
3, Publicboolean ontouchevent (motionevent event) ———— Event response method, handling the call
Classes that have the above events
1. Activity class (activity and its various inheriting subclasses)
Dispatchtouchevent (), Ontouchevent ()
2, ViewGroup class (LinearLayout, Framelayout, ListView, etc..... )
Dispatchtouchevent (), Onintercepttouchevent (), Ontouchevent ()
3. View Class (Button, TextView, etc...) )
Dispatchtouchevent (), Ontouchevent ()
PS: One thing that needs special attention is that ViewGroup has an extra onintercepttouchevent () method, and the other two methods are common to these three species.
Simple use resolution of the method
We can see that the return values of these three methods are all Boolean types, but they are the return values that determine the direction of the next pass processing.
1,dispatchtouchevent () --Used to distribute the event
This method distributes the events of the root element from top to bottom into the inner child element until it is terminated or arrives at the innermost element, which is also distributed using a tunneling method. in which onintercepttouchevent () and ontouchevent () are called, they are not generally rewritten.
Returns false to not intercept the continuation of the distribution, if true to intercept the event is not distributed in the downward layer element, in the Dispatchtouchevent () method by default returns False.
2,onintercepttouchevent () --Used to intercept the event
The implementation of this method in the ViewGroup source code is to return false to not intercept the event, and the touch event is passed down to its child view.
If we override the method and return it to true, the event will be intercepted and processed by the current viewgroup, calling the class's Ontouchevent () method.
3,ontouchevent () --For handling events
Returns true to indicate that the view can handle 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
Practical Walkthrough
OK, the basic knowledge is finished, the above things do not understand it does not matter, now to open big eyes to look good, because the following began to use examples to explain the touch of the event distribution mechanism, I believe you can better understand this distribution mechanism.
In this example, we need to rewrite four classes:
1, Boss--myactivity
2, manager--Framelayout
3. Group Leader---Linealayout
4, employee--TextView
The software interface diagram is as follows:
" start with an easy-to-understand example. "
the boss said to the Minister:I'm going to eat braised fish .the minister said to the manager:you make a braised fish.the manager said to the team leader:you make a braised fish.The team leader said to the staff:you make a braised fish.... (Employees do, do not burn the fish out)employees say:I've been working all night, and I'm not doing it, okay?The team leader said:you idiot, white hair your salary, next time not to find you, I do... (The team leader did it or did not do it)The team leader said to the manager:I tried, I didn't do it, don't fire me.manager said:you're a loser, you can't do this, but I'll do it myself.... (The manager did it and did it successfully)the manager said to the Minister:braised fish is ready.The minister responded that:Yes, I'll see you next time.the minister said to the boss:braised fish is ready.The boss responded by saying:Yes, I'll see you next time.---------------------------the boss said to the Minister:I want boiled fish in the draught.the minister said to the manager:you make a poached fish.The manager thinks for himself .:Group leader even braised fish can not make, this time will not find him, I personally to do... (The manager did it, he succeeded again.)the manager said to the Minister:braised fish is ready.The minister responded that:Yes, I'll see you next time.the minister said to the boss:braised fish is ready.The boss responded by saying:Yes, I'll see you next time.---------------------------
1, according to common sense, the leadership will be assigned to the task downward, once the following people do not do well, will not be the follow-up task to the following people to do, can only do their own, if they can not do, can only tell the superior can not complete the task, the superior will repeat his process.
2, in addition, the leadership has the right to intercept the task, the subordinate to conceal the task, and directly to do, if not done, can only report to the superior can not complete the task.
Process Demo
"1", we assume that the staff capacity is insufficient, that is, the TextView ontouchevent () method setting returns False, indicating that it cannot consume the event.
Process for event Delivery:
"2", we assume that the employee can handle the event, that is, the TextView's Ontouchevent () method setting returns True, indicating that it can handle the event.
Process for event Delivery:
"3", we assume that the staff and team leader ability is insufficient, namely TextView and LinearLayout's Ontouch () return false, but the manager resolved the problem, that is, Framelayout Ontouch () returns True
Process for event Delivery:
"4" Suppose our leader, when the event was distributed to him, decided to intercept it and not give it to the following employee, that is, Onintercepttouchevent () returned to true, and he completed the task successfully, that is, the ontouchevent () return value is true.
Process for event Delivery:
Make a summary.
1, it is clear that these processes are the result of dispatchtouchevent (), but only if we do not fully re-implement this method, that is, to ensure that the return super.dispatchtouchevent (EV); To determine if a method of the parent class has been called. These events will be passed from top to bottom, until passed to the bottommost view element, which will call the view's Ontouchevent () method to handle the event, and true to indicate that the event has been successfully processed and that if False is returned, the event is not successfully processed. The event will be passed up and down to the upper view's Ontouchevent () method, and so on, until a view successfully processes the event, or the top view processing still returns false to discard the event processing, and the event disappears.
2, if the event is passed down in the process, is intercepted, that is, the view of the Onintercepttouchevent () method returns True, then the event will stop passing down and handed over to the layer's ontouchevent () method processing, regardless of the processing success or not, The underlying view will no longer receive the event. PS: If processing fails, it will be processed by the Ontouchevent () method of the upper view.
3, Dispatchtouchevent () has the function of memory, if the first event is passed down to a view, it continues to pass the event to its child view, it will record whether the event is processed by the view below it successfully, (how can you know?) 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 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 by the following, then this event will not be passed down, the view directly call their own ontouchevent () method to handle the event.
4, Memory function information is only valid before a series of events, such as starting from the Action_down event, until the end of the subsequent event action_move,action_up, "Memory" information will be cleared. 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.
Included code
1, MyActivity
Public classMyActivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.I ("Test", "Boss" Release task: "+ util.actiontostring (ev.getaction ()) +", find someone to help me complete, the task is distributed down. "); return Super. dispatchtouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {BooleanRelust =false; LOG.I ("Test", "Boss" to complete the task: "+ util.actiontostring (event.getaction ()) +", "manager" is too bad, no longer looking for you to work, I came to take care of! Whether to resolve: "+util.candotasktop (relust)); returnRelust; }}
2, Myframelayout:
Public classMyframelayoutextendsFramelayout { PublicMyframelayout (Context context, AttributeSet attrs) {Super(context, attrs); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.I ("Test", "manager" to release the task: "+ util.actiontostring (ev.getaction ()) +", find someone to help me complete, the task is distributed down. "); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {BooleanRelust =false; LOG.I ("Test", "Manager" interception task: "+ util.actiontostring (ev.getaction ()) +", stop it? " +relust); returnRelust; } @Override Public Booleanontouchevent (Motionevent event) {BooleanRelust =true; LOG.I ("Test", "manager" to complete the task: "+ util.actiontostring (event.getaction ()) +", "team Leader" is too bad, no longer looking for you to work, I came out! Whether to resolve: "+Util.candotask (relust)); returnRelust; }}
3, Mylinearlayout
Public classMylinearlayoutextendsLinearLayout { PublicMylinearlayout (Context context, AttributeSet attrs) {Super(context, attrs); } @Override Public Booleandispatchtouchevent (motionevent ev) {LOG.I ("Test", "Team Leader" Release task: "+ util.actiontostring (ev.getaction ()) +", find someone to help me complete, the task is distributed down. "); return Super. dispatchtouchevent (EV); } @Override Public Booleanonintercepttouchevent (motionevent ev) {BooleanRelust =true; LOG.I ("Test", "leader" whether intercept task: "+ util.actiontostring (ev.getaction ()) +", stop it? " +relust); returnRelust; } @Override Public Booleanontouchevent (Motionevent event) {BooleanRelust =true; LOG.I ("Test", "team Leader" to complete the task: "+ util.actiontostring (event.getaction ()) +", "staff" is too bad, no longer looking for you to work, I came through! Whether to resolve: "+Util.candotask (relust)); returnRelust; }}
4, Mytextview
Public classMytextviewextendsTextView { PublicMytextview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub} @Override Public Booleandispatchtouchevent (Motionevent event) {log.i ("Test", "Employee" Release task: "+ util.actiontostring (event.getaction ()) +", I have no men, alas ~ do it yourself); return Super. Dispatchtouchevent (event); } @Override Public Booleanontouchevent (Motionevent event) {BooleanRelust =false; LOG.I ("Test", "staff" to complete the task: "+ util.actiontostring (event.getaction ()) +", "Employees" now only on their own! Whether to resolve: "+Util.candotask (relust)); returnRelust; }}
5. Util (Tool Class)
Public classUtil { Public StaticString actiontostring (intaction) {String result=NULL; Switch(action) { CaseMotionEvent.ACTION_DOWN:result= "Action_down"; Break; CaseMotionEvent.ACTION_MOVE:result= "Action_move"; Break; CaseMotionEvent.ACTION_UP:result= "Action_up"; Break; } returnresult; } Public StaticString Candotask (Booleancan) {String result=NULL; if(CAN) {result= "Perfect solution to the task!" "; } Else{result= "This is not going to work, give the boss to finish it." "; } returnresult; } Public StaticString Candotasktop (Booleancan) {String result=NULL; if(CAN) {result= "Perfect solution to the task!" "; } Else{result= "This is not going to work, give up the task." "; } returnresult; }}
Enjoy wind chimes
Source: http://www.cnblogs.com/net168/
This article is copyright to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original connection, or next time not to you reproduced
Simple analysis of Android's touch event distribution mechanism