Android TouchEvent Event Delivery mechanism

Source: Internet
Author: User

3 methods related to touch events: Public Boolean dispatchtouchevent (motionevent ev); Used to assign an event Public Boolean onintercepttouchevent (motionevent ev);//To intercept event Public Boolean ontouchevent (motionevent ev); Used to handle event classes that have these three methods:
Activity class: Activity Dispatchtouchevent ();
Ontouchevent ();
View container (subclass of ViewGroup): Framelayout, LinearLayout ...
ListView, ScrollVIew ...
Dispatchtouchevent ();
Onintercepttouchevent ();
Ontouchevent ();
View Control (non-viewgroup subclass): Button, TextView, EditText ... Dispatchtouchevent ();
Ontouchevent ();

three a party method of Use:
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
"Note": Some subclasses of ViewGroup (GridView, ScrollView ... ) overrides the Onintercepttouchevent () method, which returns true to intercept when the Action_move event occurs.   to demonstrate, rewrite the 4 classes:President-to-myactivity---myframelayout mayor--and mylinearlayout farmers--and Mytextview
"To give an easy-to-understand example": the president said to the governor: "I want to eat braised fish." The governor said to the mayor: "You make a braised fish the mayor said to the county magistrate:" You do a braised fish County magistrate said to the peasants: you make a braised fish ... The farmer said: "I try my best, but I really won't do it, spare me." The county magistrate said: "You idiot, next time not to find you, see me to do ..." The county magistrate said to the mayor: "I did my best, I am very sorry, I will not do the mayor said:" You a waste, what to use, I can only do it myself ... (The mayor did it, he did it.) The mayor said to the governor: Braised fish do the governor said: "Good, next time you have to find your governor said to the President:" Braised fish do the president said: "Good, next time you need to find you ---------------------------The president said to the governor: "I want the water boiled fish governor to the mayor said:" You make a boiled fish the mayor said: the county magistrate even braised fish can not make, this time will not find him, I personally to do ... (The mayor did it, he succeeded again.) the mayor said to the governor: Boiled fish is readyThe governor said: "Good, next time you have to find your governor said to the President: Boiled fish do the president said:" Good, next time you have something to look for ---------------------------
    • According to common sense, the leadership will assign 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.
    • 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.
The clickable property of "1" TextView defaults to False, so TextView's Ontouchevent () method returns false by default, and the program output is as follows:Event Delivery:
  "2" Change the TextView clickable property to true manually, or simply rewrite the Ontouchevent () method to return True, the program output is as follows:
Event Delivery:
  the "3" manually overrides the LinearLayout onintercepttouchevent () method, which returns true, intercepts the event, and then overrides the Ontouchevent () method, returning True to the program output:
Event Delivery:
 (1) This series of delivery processes are controlled by the Dispatchtouchevent () method, if not artificially intervened, the event will be passed from top to bottom (because the default is to return false will not intercept), passed to the lowest view, by its ontouchevent ( ) method to handle the event, if the processing succeeds returns TRUE, if the processing fails returns false, the event is passed up sequentially, and each view calls its own ontouchevent () method to handle the event, and if the processing succeeds, it terminates the pass, and if the processing fails, it continues to pass upward. (2) After human intervention, if it is intercepted in the process of passing down, that is, 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 is passed up. (3) In addition, the Dispatchtouchevent () method also has the function of "memory", if the first event is passed down to a view, it will continue to pass the event to its child view, it would record whether the event is under its view to deal with the success, (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) The information of the "memory" function is only valid until a series of events have been completed, such as from the Action_down event, until the end of the subsequent event action_move,action_up, the "Memory" information 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. Demo Source Download:http://download.csdn.net/detail/morgan_xww/5781199 [Java]View Plaincopyprint?
  1. Public class MyActivity extends Activity {
  2. @Override
  3. public void OnCreate (Bundle savedinstancestate) {
  4. super.oncreate (savedinstancestate);
  5. Setcontentview (R.layout.main);
  6. }
  7. @Override
  8. Public Boolean dispatchtouchevent (motionevent ev) {
  9. LOG.D ("D", " President" task < "+ util.actiontostring (ev.getaction ()) + " >: Need to assign ");
  10. return super.dispatchtouchevent (EV);
  11. }
  12. @Override
  13. Public Boolean ontouchevent (motionevent ev) {
  14. Boolean bo = false;
  15. LOG.D ("D", " President" task < "+ util.actiontostring (ev.getaction ()) + " >: Below all can not solve, next time can not rely on you, hum ... You can only try it yourself. Can solve?  "+ bo);
  16. return bo;
  17. }
  18. }

[Java]View Plaincopyprint?
  1. Public class Myframelayout extends Framelayout
  2. {
  3. Public myframelayout (context context, AttributeSet attrs) {
  4. Super (context, attrs);
  5. }
  6. @Override
  7. Public Boolean dispatchtouchevent (motionevent ev) {
  8. LOG.D ("D", " Governor" task < "+ util.actiontostring (ev.getaction ()) + " >: Need to assign ");
  9. return super.dispatchtouchevent (EV);
  10. }
  11. @Override
  12. Public Boolean onintercepttouchevent (motionevent ev) {
  13. Boolean bo = false;
  14. LOG.D ("D", " Governor" task < "+ util.actiontostring (ev.getaction ()) + " >: Intercept?  "+ bo);
  15. return bo;
  16. }
  17. @Override
  18. Public Boolean ontouchevent (motionevent ev) {
  19. Boolean bo = false;
  20. LOG.D ("D", " Governor" task < "+ util.actiontostring (ev.getaction ()) + " >: The mayor is a waste, I will not find you again next time, I will try it myself. Can solve?  "+ bo);
  21. return bo;
  22. }
  23. }

[Java]View Plaincopyprint?
  1. Public class Mylinearlayout extends linearlayout{
  2. Public mylinearlayout (context context, AttributeSet attrs) {
  3. Super (context, attrs);
  4. }
  5. @Override
  6. Public Boolean dispatchtouchevent (motionevent ev) {
  7. LOG.D ("D", " Mayor" task < "+ util.actiontostring (ev.getaction ()) + " >: Need to assign ");
  8. return super.dispatchtouchevent (EV);
  9. }
  10. @Override
  11. Public Boolean onintercepttouchevent (motionevent ev) {
  12. Boolean bo = false;
  13. LOG.D ("D", " Mayor" task < "+ util.actiontostring (ev.getaction ()) + " >: Intercept?  "+ bo);
  14. return bo;
  15. }
  16. @Override
  17. Public Boolean ontouchevent (motionevent ev) {
  18. Boolean bo = false;
  19. LOG.D ("D", " Mayor" task < "+ util.actiontostring (ev.getaction ()) + " >: Farmers really useless, next time will not find you, I have to try. Can solve?  "+ bo);
  20. return bo;
  21. }
  22. }

[Java]View Plaincopyprint?
  1. Public class Mytextview extends TextView
  2. {
  3. Public Mytextview (context context, AttributeSet attrs) {
  4. Super (context, attrs);
  5. }
  6. @Override
  7. Public Boolean dispatchtouchevent (motionevent ev) {
  8. LOG.D ("D", " Farmer" Task < "+ util.actiontostring (ev.getaction ()) + " >: Need to assign, I have no one below, how to do?  Do it Yourself ");
  9. return super.dispatchtouchevent (EV);
  10. }
  11. @Override
  12. Public Boolean ontouchevent (motionevent ev) {
  13. Boolean bo = true;
  14. LOG.D ("D", " Farmer" Task < "+ util.actiontostring (ev.getaction ()) + " >: Do it yourself. Can solve?  "+ bo);
  15. return bo;
  16. }
  17. }

Android TouchEvent Event Delivery mechanism

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.