Whether in Java or Android callback is used a lot of, such as Custom ListView drop-down Refresh, ListView This class does not know what to implement in the dropdown, and only know when the user pull down to refresh, this is not the role of interface, interface only defines the appearance, Specific implementation to the subclass to achieve, this is one, and the other, since the ListView do not know what the function of the drop-down, but also to call his class to implement, so it is necessary to use the callback,
Definition of callback:
In my personal understanding of the words: is a function, in the middle of a paragraph does not know what to achieve, to wait for external callers to know, and in a function only know what to implement, but the specific implementation is actually implemented in the calling class, this is the interface callback, now the code to explain more clearly
First, define an interface for the callback
/** * Interface for callback */public interface Onclicklistener {void OnClick ();}
In defining a button to implement certain functions,
public class Button {private Onclicklistener listener;private myinterface interfacer;public void Setonclicklistener ( Final Onclicklistener listener) {This.listener = Listener;}}
This is a click event that simulates a button in Android.
Mainclass.java
public class MainClass implements Onclicklistener {public static void Main (string[] args) { System.out.println ("This is Main"); New MainClass (). CallBack ();} public void CallBack () { Button btn = new Button (); Btn.setonclicklistener (this); } @Overridepublic void OnClick () {System.out.println ("interface callback");}}
Printing results:
This is main interface callback
Now parse the callback,
1: First define an interface for callbacks,
2: In the class to be called, in this Code the button is the class to invoke, in which the Setonclicklistener () method does not implement any code, just call the OnClick () method in the interface, equivalent to declaring the method body, and there is no specific implementation, In the MainClass class we find that the onclick () method prints a word System.out.println ("interface Callback"), which can be seen as the specific implementation of the OnClick () method, so the callback method is declared and implemented separate,
3: Register listener: public void Setonclicklistener (Onclicklistener listener); This method is to register the listener, and then define the variables to accept the interface variables passed by the outside, passed through the interface of the implementation class, The concept of polymorphism is used here.
4: Define a method in the Button class to implement a certain logic, but this logic is also a part of the Do not know how to implement,
Part of the code changes:
Button.java
public class Button {private Onclicklistener listener;public void Setonclicklistener (Onclicklistener listener) { This.listener = listener;} public void Move () {System.out.println ("------Before Move"), Listener.onclick ();//For example, to handle a lot of business System.out.println on the Move (" ------") after the move;}}
Mainclass.java
public class MainClass implements Onclicklistener {public static void Main (string[] args) { new MainClass () . CallBack ();} public void CallBack () { Button btn = new Button (); Btn.setonclicklistener (this); Btn.move (); } @Overridepublic void OnClick () {System.out.println ("moving ....... ...");}}
What to do with the calling class:
1:new an object of a called class
2: Then register the Listener
Specific code to mention now:
Button btn = New button (); Btn.setonclicklistener (this);
The called class also has a notation:
Button.java<pre class= "java" name= "code" >public class Button {public void Setonclicklistener (Onclicklistener Listener) {System.out.println ("------Before moving"), Listener.onclick ()///For example, a lot of business System.out.println to be handled in the move ("------after Moving");}}
The caller also has a notation:
public class MainClass {public static void Main (string[] args) { new MainClass (). CallBack (); public void CallBack () { Button btn = new Button (); Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick () {System.out.println ("moving ................");}}); }}
You can compare some of the view of Android Click events, that is, understanding the interface to learn the design module has a great help! Hope that we all learn together, there is no place to speak of the wrong, please reply!
Callback in Java