View event handling for Android GUI (ii)

Source: Internet
Author: User

In the previous article, we analyzed the event-handling process of view, and of course, the view here refers to the basic view. When the view receives a touch event, the Dispachetouchevent method is called first, and in this method the Ontouchlistener and ontouchevent are called for specific event handling. The Ontouchlistener takes precedence over ontouchevent. If the Ontouch method in Ontouchlistener returns True, subsequent ontouchevent are no longer executed. In Ontouchevent, specific event handling is performed based on touch action. This is the conclusion we got in the last article, so we need to consider a problem where our basic controls are in the layout file and then on the phone screen, so how do we pass events from activity to layout (ViewGroup) to view when we touch the screen?

First, we look at a simple example, we first customize a view and ViewGroup respectively to re-dispatchtouchevent and Ontouchevent method, the specific code is as follows:

 Public classMyButtonextendsButton {Private Static FinalString tag= "MyButton";  PublicMyButton (Context context) {Super(context); }     PublicMyButton (Context context, AttributeSet attrs) {Super(context, attrs); } @Override Public Booleandispatchtouchevent (Motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_down:log.i (TAG,The dispatchtouchevent of the custom button performed: Action_down);  Break;  Casemotionevent.action_move:log.i (TAG,The dispatchtouchevent of the custom button performed: Action_move);  Break;  Casemotionevent.action_up:log.i (TAG,The dispatchtouchevent of the custom button performed: Action_up);  Break; }        return Super. Dispatchtouchevent (event); } @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_down:log.i (TAG,The ontunchevent of the custom button performed: Action_down);  Break;  Casemotionevent.action_move:log.i (TAG,The ontunchevent of the custom button performed: Action_move);  Break;  Casemotionevent.action_up:log.i (TAG,The ontunchevent of the custom button performed: Action_up);  Break; }        return Super. Ontouchevent (event); }}
 Public classMygroupviewextendsLinearLayout {Private Static FinalString tag= "Mygroupview";  PublicMygroupview (Context context) {Super(context); }     PublicMygroupview (Context context, AttributeSet attrs) {Super(context, attrs); } @Override Public Booleandispatchtouchevent (motionevent ev) {Switch(Ev.getaction ()) { Casemotionevent.action_down:log.i (TAG,"Custom ViewGroup dispatchtouchevent executed: Action_down");  Break;  Casemotionevent.action_move:log.i (TAG,"Custom ViewGroup dispatchtouchevent executed: Action_move");  Break;  Casemotionevent.action_up:log.i (TAG,"Custom ViewGroup dispatchtouchevent executed: action_up");  Break; }        return Super. dispatchtouchevent (EV); } @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_down:log.i (TAG,"Custom ViewGroup ontouchevent executed: Action_down");  Break;  Casemotionevent.action_move:log.i (TAG,"Custom ViewGroup ontouchevent executed: Action_move");  Break;  Casemotionevent.action_up:log.i (TAG,"Custom ViewGroup ontouchevent executed: action_up");  Break; }        return Super. Ontouchevent (event); }}

The same two methods are overridden in the activity, and the button-binding point-and-click event listeners are as follows:

@Override Public Booleandispatchtouchevent (motionevent ev) {Switch(Ev.getaction ()) { Casemotionevent.action_down:log.i (TAG,"The Dispatchtouchevent of activity performed: Action_down");  Break;  Casemotionevent.action_move:log.i (TAG,"The Dispatchtouchevent of activity performed: Action_move");  Break;  Casemotionevent.action_up:log.i (TAG,"The Dispatchtouchevent of activity performed: Action_up");  Break; }    return Super. dispatchtouchevent (EV);} @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_down:log.i (TAG,"The Ontunchevent of activity performed: Action_down");  Break;  Casemotionevent.action_move:log.i (TAG,"The Ontunchevent of activity performed: Action_move");  Break;  Casemotionevent.action_up:log.i (TAG,"The Ontunchevent of activity performed: Action_up");  Break; }    return Super. Ontouchevent (event);}

Run the program, click on the button, you can see the log information as follows, from the running log, we see that the order of events to be passed Activityàviewgroupàview.

In the activity, we see the Dispatchtouchevent method finally called the Super.dispatchtouchevent method, tracking code, you can see in the activity of the source, its method is implemented as follows:

 Public Boolean dispatchtouchevent (motionevent ev) {    if (ev.getaction () = = Motionevent.action_down) {        Onuserinteraction ();    }     if (GetWindow (). superdispatchtouchevent (EV)) {        returntrue;    }     return ontouchevent (EV);}

From the source, it can be seen that whether ontouchevent is executed in activity depends entirely on the return value of the Superdispatchtounchevent method of the Window object, as mentioned in the previous article. Activity of the window implementation class for Phonewindow, find the source of this class, see this method, the content is as follows:

     Public Boolean   superdispatchtouchevent (motionevent event) {    return  mdecor.superdispatchtouchevent (event); }

It is obvious that the Superdispatchtouchevent method of Decorview is called, and the parent class is called the Dispatchtouchevent method of framelayout in its concrete implementation. And Framelayout does not override this method, so we need to find the method implementation in ViewGroup, this method is a bit long, here is not all posted out, interested can self-view, in this method key place called the following two methods:

Private Boolean Boolean Cancel,         int desiredpointeridbits)  Public boolean onintercepttouchevent (motionevent ev)

Simply speaking, the effect of these two methods is to dispatchtransfromedtouchevent the event to the child view, and the method onintercepttouchevent determines whether the distribution of the event will be intercepted. At this point, we have a basic understanding of the delivery process of the view event, which is not enough, we continue to analyze.

As mentioned before, a complete event is composed of several movements of touch (Action_down, Action_move, action_up), and the beginning of the event is always started by Action_down and action_up ends. Make a slight change to the above example and rewrite the Onintercepttouchevent method in Myviewgroup.

@Override  Public Boolean onintercepttouchevent (motionevent ev) {    log.i (TAG,"Onintercepttouchevent of Custom ViewGroup performed");     return Super . onintercepttouchevent (EV);}

After running, the output log is as follows, and you can see that the method returns False (the default return value in ViewGroup) and does not affect the pass-through process of the event.

Modify the method again, return the value, modify to True, the log output is as follows, from the results to see that the event is not passed to the MyButton, but was intercepted, and was intercepted, after the execution of the Myviewgroup ontouchevent method.

Restore the Onintercepttouchevent and modify the dispatchtouchevent, change its return value to True, run the log results as follows, it is clear that the event was canceled and no longer distributed downward.

Restore the Myviewgroup dispatchtouchevent method, modify the MyButton, change the return value of Ontouchevent to true, and run the log result as:

Modify the return value to false and the log result is:

By comparing the logs, we can find that when True is returned, the ontouchevent of the view containing the mybutton is not executed, which means that the event is mybutton processed and consumed.

Through the above analysis, we can get the following conclusions:

1. After the event is received from the activity, it is finally passed to the view by ViewGroup. There are three methods involved in this process, as follows:

Dispatchtouchevent

Ontouchevent

Onintercepttouchevent

Where Onintercepttouchevent is the method in ViewGroup.

2. If the return value in the Dispatchtouchevent method is true, the distribution is no longer resumed when the event is canceled.

3. If the Ontouchevent method of the view returns a value of true, it means that the event has been processed, and if the return value is false, the ontouchevent of the parent container is invoked in turn to handle the event.

3. If the Onintercepttouchevent return value is true, the event is intercepted, no longer distributed downward, and its own Ontouchevent method is called for event handling

To learn more about the small partners, you can click to view the source code , run the test yourself,

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright notice: The copyright of this article is Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:

View event handling for Android GUI (ii)

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.