Android touchevent event Transfer Mechanism

Source: Internet
Author: User
Three methods related to touch events: Public Boolean dispatchtouchevent (motionevent eV); // used to assign an event Public Boolean onintercepttouchevent (motionevent eV); // used to intercept an event Public Boolean ontouchevent (motionevent eV); // used to process the event   Classes with 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 ();


3.PartiesMethod usage:

Dispatchtouchevent () Used to dispatch events.
Onintercepttouchevent () and ontouchevent () are called. This method is generally not overwritten.
Onintercepttouchevent () Used to intercept events.
The source code implementation in the viewgroup class is {return false;}, indicating that the event is not blocked,
The event is passed down (to its subview );
If you manually override this method and make it return true, it indicates blocking, and the event will be terminated and passed down,
The event is handled by the current viewgroup class, that is, the ontouchevent () method of the class is called.
Ontouchevent () Used to process events.
If the return value is true, the view can process the event. The event is terminated and passed up (to its parent view );
If false is returned, the event cannot be processed. The event is passed to the ontouchevent () method of its parent view for processing.
[Note]: some subclasses of viewgroup (gridview, scrollview...) override the onintercepttouchevent () method. When the action_move event occurs, return true to intercept.
Four classes are rewritten for Demonstration:

President --> Governor myactivity --> Mayor myframelayout --> mylinearlayout farmer --> mytextview

[An easy-to-understand example]: The president said to the Governor: "I want to eat braised fish." The governor said to the mayor, "You are a braised fish." The county magistrate said to the farmers: make a braised fish ...... (The peasants did not do it.) The peasants said: I have tried my best, but I really don't want to do it. Sorry, the county magistrate said: You are stupid. I will not contact you next time, let me do it ...... (The County Magistrate did not do it.) The county magistrate said to the mayor, "I have tried my best. I am very sorry. I will not be the mayor." You are a waste. What is your use, I can only do it myself ...... (The mayor did it and succeeded.) The mayor told the governor that braised fish has done a good job and said, "Well, next time you have something to ask your governor to say to the President: braised fish has done a good job. The President said: yes. I will contact you next time. ---------------------------The president said to the governor, "I want to eat boiled fish. The governor said to the mayor," You are the mayor of a boiled fish. "The county magistrate cannot even cook braised fish. I will not look for him this time. I will do it myself ...... (The mayor has done it and succeeded again) The mayor told the Governor that the boiled fish is ready.The governor said: Yes. The governor will ask you next time and say to the President: "The boiled fish is done. The President said:" Yes, I will ask you again next time ." ---------------------------
  • According to common sense, leaders will distribute tasks downward. Once the following persons do not do well, they will not hand over the subsequent tasks to the following persons. They can only do the tasks themselves, if you can't do it yourself, you can only tell the superior that the task cannot be completed, and the superior will repeat his process.
  • In addition, the leaders have the right to intercept the task and conceal the task from their subordinates. If the task fails, they can only report to their superiors and cannot complete the task.

[1] The default clickable attribute of textview is false. Therefore, the ontouchevent () method of textview returns false by default. The program output is as follows:

Event transfer:

[2] manually change the clickable attribute of textview to true, or directly rewrite the ontouchevent () method to return true. The program output is as follows:
Event transfer:

[3] manually override the onintercepttouchevent () method of linearlayout to return true, intercept the event, rewrite the ontouchevent () method, return true, and output the program:
Event transfer:

(1) This series of transfer processes are controlled by the dispatchtouchevent () method. If no human intervention is performed, the events will be transmitted from top to bottom (because false is returned by default and will not be intercepted ), the ontouchevent () method is used to process the event. If the event is processed successfully, true is returned. If the event fails to be processed, false is returned, each view calls its own ontouchevent () method to process the event. If the process is successful, the transfer is terminated. If the process fails, the transfer continues. (2) After human intervention, if the onintercepttouchevent () method returns true, the event stops being passed down, it is handled directly by the current ontouchevent () method. If the processing is successful, OK. If the processing fails, the event is passed up. (3) In addition, the dispatchtouchevent () method also has the "Memory" function. If the first event is passed down to a view, it will pass the event to its subview, it records whether the event is processed successfully by the view below it (how can we know? If the event will be passed up to me again and handled by my ontouchevent (), it means that none of the following views can process the event successfully ); when the second event is passed down to the view, the dispatchtouchevent () method of the view is used to determine whether the previous event is successfully handled by the following view, then the event will be handed over to the following for processing. If the previous event is not successfully handled by the following, the event will not be passed down, the view directly calls its ontouchevent () method to process the event. (4) The information of the "Memory" function is valid only before the completion of a series of events, such as starting from the action_down event until the completion of the action_move and action_up events, the "Memory" information will be cleared. That is to say, if a view fails to process the action_down event (ontouchevent () returns false), the subsequent action_move, action_up and other events will not be passed to the view, and the parent view will handle it by itself. The next action_down event will still be passed to this view. Download demo source code:Http://download.csdn.net/detail/morgan_xww/5781199
Public class myactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main) ;}@ overridepublic Boolean dispatchtouchevent (motionevent eV) {log. D ("D", "[President] task <" + util. actiontostring (ev. getaction () + ">: need to assign"); return Super. dispatchtouchevent (EV) ;}@ overridepublic Boolean ontouchevent (motionevent eV) {Boole An BO = false; log. D ("D", "[President] task <" + util. actiontostring (ev. getaction () + ">: The following cannot be solved. You can no longer rely on it next time... You can only try it yourself. Yes? "+ Bo); Return Bo ;}}

Public class myframelayout extends framelayout {public myframelayout (context, attributeset attrs) {super (context, attrs);} @ overridepublic Boolean dispatchtouchevent (motionevent eV) {log. D ("D", "[Governor] task <" + util. actiontostring (ev. getaction () + ">: need to assign"); return Super. dispatchtouchevent (EV) ;}@ overridepublic Boolean onintercepttouchevent (motionevent eV) {Boolean BO = false; log. D ("D", "[Governor] task <" + Util. actiontostring (EV. getaction () + ">: intercept? "+ Bo); Return Bo ;}@ overridepublic Boolean ontouchevent (motionevent eV) {Boolean BO = false; log. D ("D", "[Governor] task <" + util. actiontostring (ev. getaction () + ">: The mayor is a waste and will never look for you again. I will try it myself. Yes? "+ Bo); Return Bo ;}}

Public class mylinearlayout extends linearlayout {public mylinearlayout (context, attributeset attrs) {super (context, attrs);} @ overridepublic Boolean dispatchtouchevent (motionevent eV) {log. D ("D", "Mayor" task <"+ util. actiontostring (ev. getaction () + ">: need to assign"); return Super. dispatchtouchevent (EV) ;}@ overridepublic Boolean onintercepttouchevent (motionevent eV) {Boolean BO = false; log. D ("D", "Mayor" <"+ Util. actiontostring (EV. getaction () +">: intercept? "+ Bo); Return Bo ;}@ overridepublic Boolean ontouchevent (motionevent eV) {Boolean BO = false; log. D ("D", "Mayor" task <"+ util. actiontostring (ev. getaction () + ">: It's useless for a farmer. I will try it again next time. Yes? "+ Bo); Return Bo ;}}

Public class mytextview extends textview {public mytextview (context, attributeset attrs) {super (context, attrs) ;}@ overridepublic Boolean dispatchtouchevent (motionevent eV) {log. D ("D", "[Farmer] task <" + util. actiontostring (ev. getaction () + ">: The task needs to be assigned. I don't have any of them. What should I do? "); Return Super. dispatchtouchevent (EV) ;}@ overridepublic Boolean ontouchevent (motionevent eV) {Boolean BO = true; log. D ("D", "[Farmer] task <" + util. actiontostring (ev. getaction () + ">: do it by yourself. Yes? "+ Bo); Return Bo ;}}
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.