Android Combat Interface Callback

Source: Internet
Author: User
Tags throwable

Because I have been trapped in the abstract, interface and other "scary" things, the lack of on-line in the project of the actual use of the explanation, the following I would venture to their understanding and everyone to exchange next.

I am also self-taught "talent", hehe, although the University of computer, but not interested in software development. After graduating to see the students or training Android or training iOS 4 months soon found a good job, I feel very surprised, but also very jealous! So I made an important decision in life, began to learn Android, because I am relatively poor, so choose to self-study. The process of learning is not easy, from the beginning of the ignorance of the struggle, to learn to have a strategy after the response, all rely on their own diligence good ask.

People who learn Java should all know the three main features of object-oriented: encapsulation, inheritance, polymorphism, two-thirds of which involves abstraction (inheritance and polymorphism), thus the importance of abstraction can be seen. Abstract this word sounds very abstract, learning is also very abstract, and interface is a special abstract class (the method is all abstract method), it is more abstract. I have always known the existence of abstraction, but I have never known how to use it and what it is good for, and do not know how to ask others. So I wrote the code to do the project until once. Someone has to ask, do not use abstract, interface and other things can not write projects? I would like to say that the quality of the code is different, or why the brick family to invent it?!

in the process of doing the project, one time with the server colleagues to discuss points, he looked at my code, and then found a more serious problem, that is the request network operation. I am referring to Asynchttpclient.jar, each time the network request is:

Requestparams params = new Requestparams ();

New Asynchttpclient (). Post (Requesturl, Params,new Asynchttpresponsehandler () {


public void onsuccess (int statusCode, header[] headers,byte[] responsebody) {}

public void onfailure (int statusCode, header[] headers,byte[] responsebody, throwable error) {}
}

So in a live activity fragment to new Asynchttpclient (), many times, this will also produce a lot of repetitive code, and unclear, not in line with the principle of single responsibility. So he helped me get the network request operation out of a single class, and then through the interface callback and fragment or activity interaction. I feel so great that I can understand the interface:

public class Netrequest {
Private Netrequestiterface Netrequestiterface;
Private context context;
Public Netrequest (Netrequestiterface netrequestiterface,context Context) {
This.netrequestiterface= Netrequestiterface;
This.context=context;
}

Requestparams params = new Requestparams ();
if (Null!=map&&!map.isempty ())
For (String Key:map.keySet ()) {
Params.put (Key, Map.get (key));
}

New Asynchttpclient (). Post (Requesturl, Params,new Asynchttpresponsehandler () {
public void onsuccess (int statusCode, header[] headers,byte[] responsebody) {

Here is the result of the asynchronous parsing of the network passed to the subclass, Requesturl is a sign to determine which request. Subclass Implementation ChangeView ()

Netrequestiterface.changeview (result, requesturl);

}

Public void OnFailure (int statusCode, header[] headers,byte[] responsebody, throwable error) {}
}

 } 

This is a small demo of a network operation interface callback that I wrote, code: http://download.csdn.net/detail/gaolei1201/8936209


the condition of an interface callback is an interface, two classes, two classes of each other, which conforms to the six principles of Java design Pattern dependency inversion principle. the six principles of design patterns can be consulted in my previous blog: http://blog.csdn.net/gaolei1201/article/details/47082783. For more information on interface callbacks, refer to:http://www.2cto.com/kf/201412/365788.html


Here's an example that fragment is a common component in a project, but how do you do it with the interaction between them? It is difficult for the person to be brought up, like the former me. If Fragmenta wants to change Fragmentb's UI, you might first think of writing a method like Changefragmentui () in FRAGMENTB to change Fragmentb ui,1, the first thing you think about is new FRAGMENTB (). Changefragmentui (), but you try to find it is not good newspaper: NullPointerException. This is because you have re-fragmentb a new one, and the component that needs to be changed is initialized in the original FRAGMENTB. 2, the Changefragmentui is set to static, then you can Fragmentb.chanefragmentui (). This way, although the null pointer exception is not reported, but the static method inside the variable must be static, that is, you want to change the UI components to declare is static, think, if there is too much static is certainly not good. At this point, if the interface callback will be able to solve all your troubles, the following example implementation of the interaction between the two fragment to change the UI of each other example.


The following leftfragment code:

public class Leftfragment extends Fragment implements Onfragmentchangelistener {
Private TextView Show_change_text;
Private Button CHANGE_ACTIVITY_BT;

public static Onfragmentchangelistener Onfragmentchangelistener;

public static void Setonfragmentchangelistener (Onfragmentchangelistener onfragmentchangelistener) {
Rightfragment.onfragmentchangelistener=onfragmentchangelistener;
}

Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
Super.oncreateview (Inflater, container, savedinstancestate);

View view = Inflater.inflate (r.layout.right_fragment, NULL);
CHANGE_ACTIVITY_BT = (Button) view
. Findviewbyid (R.ID.CHANGE_ACTIVITY_BT);
Show_change_text = (TextView) View.findviewbyid (R.id.show_change_text);

/* new  RightFragment (). Setonfragmentchangelistener (this);   such a set is not possible, because this again creates a mainactivity, and the original initialization of the one is not     a so  mainactivity.setonfragmentchangelistener (this); * *
rightfragment.setonfragmentchangelist Ener (this);

Change_activity_bt.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Execute the interface method here
Onfragmentchangelistener.onfragmentchange ();
}
});
return view;
}
@Override
public void Onfragmentchange () {
Methods of implementing interfaces in subclasses
Show_change_text.settext ("I am leftfragment,and I have changed");
}
}


The following is the Rightfragment code, which is almost the same as leftfrgment because of their logic:

public class Rightfragment extends Fragment implements Onfragmentchangelistener {
Private TextView Show_change_text;
Private Button CHANGE_ACTIVITY_BT;

public static Onfragmentchangelistener Onfragmentchangelistener;

public static void Setonfragmentchangelistener (Onfragmentchangelistener onfragmentchangelistener) {
Rightfragment.onfragmentchangelistener=onfragmentchangelistener;
}

Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
Super.oncreateview (Inflater, container, savedinstancestate);
View view = Inflater.inflate (r.layout.right_fragment, NULL);
CHANGE_ACTIVITY_BT = (Button) view
. Findviewbyid (R.ID.CHANGE_ACTIVITY_BT);
Show_change_text = (TextView) View.findviewbyid (R.id.show_change_text);

/* New Leftfragment (). Setonfragmentchangelistener (this); This set is not possible, because it re-creates a mainactivity, and the original initialization is not

a a null pointer, because the activity's listener is not set (instantiated). So it should be mainactivity.setonfragmentchangelistener (this); */


Leftfragment.setonfragmentchangelistener (this);
Change_activity_bt.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Execute the interface method here
Onfragmentchangelistener.onfragmentchange ();
}
});
return view;
}
@Override
public void Onfragmentchange () {
Methods of implementing interfaces in subclasses
Show_change_text.settext ("I am rightfragment,and I have changed");
}


this is demo:http://download.csdn.net/detail/gaolei1201/8936215.


That's not what Daniel said. Abstract, interface is really more abstract this needs to learn, summarize, and more importantly in the project to use it, then you will understand its role, found its advantages, Oh yeah!




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Interface Callback

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.