A man asked me how to use the callback, callback how to understand? How to say, imaginative achievement here inexpressible na, not also, callback in the actual development of the use of frequency is actually very high, just as I was a child was also a callback function bullying, unexpectedly asked, then certainly to share some of my experience.
comprehension of the net-pass callback
The so-called callback function is: Defines a method in Class A, this method uses an interface and an abstract method in the interface, but the abstract method does not have a concrete implementation, requires class B to implement, B class implementation of the method, it does not call the method itself, but passed to class A, for Class A to call, This mechanism is called a callback.
Estimated after reading has fainted in the toilet, can temporarily ignore ...
Then from now on can use my three elements to understand, the previous can be temporarily forgotten.
Callback My side summed up there are three elements:
- Interface:
哪里需要被监听 The interface is written there.
- Reference: It is possible to have a reference
对被监听者进行设置监听 .
- Implementation:
哪里需要数据/结果 , where it is implemented
Examples of selling machines
A sale to the workers ordered 100 machines ready to sell to foreigners, let workers in the production of 100 machines after the notice him.
For this need we can use回调监听To handle
Let's start with a wisp of thought:
In this demand, we use just three elements to locate.
- Where to be monitored worker (monitor worker's work progress)
- Where to find results--sales (results of worker production)
- Set up monitoring and sales for listeners (because the results are eventually obtained in the sales class, so set the listener to be here)
Let's take a look at the following code:
Workers need to monitor the production machine in.
/** * Worker class * 1. Only responsible for production * 2. Tell the manager when the production is finished * * * @author relicemxd * */ Public class Worker { PrivateWorklistner Listner;/** * Manufacturing Machines: * Worker-specific methods */ Public void Makemachine() {intnum = -; for(inti =0; i < num; i++) {if(Num >= -) {Continue; } }//Meet 100 machines to notify you of the next stepsListner.onworklistner (num); }/ * * Monitor worker Production Interface */ Public interface worklistner { //The method for event operation after the condition is met voidOnworklistner (intNUM); }/** * Set up a way to monitor workers ' production * @param Listner is implemented on demand side * * Public void Setonworklistner(Worklistner Listner) { This. Listner = Listner; }}
The above is to observe the work of the workers, so the WorkListner interface and setOnWorkListner(WorkListner listner) the setting of the monitoring method is written in the Worker worker class.
Continue looking at the following code:
Sales need to set up monitoring, only to know when the workers finished the machine.
/** * Sales as long as the responsibility to sell can be * * @author relicemxd * * * Public class Seller { Public Static void Main(string[] args) {//1. Sales to sell machines first, machines, who produce machines? Find WorkersWorker work =NewWorker ();//2. How many machines are sold for sale? Tell the WorkersWork.setonworklistner (NewWorklistner () {@Override Public void Onworklistner(intnum) {System.out.println ("Now the production is complete, total"+num +"Taiwan Machine");//When the workers are finished, they can sell the machines.Sell (); } });//3. Workers are clear about how many machines they will have to produce to dry. Let the workers produce 100 machinesWork.makemachine (); }/** * Selling machines: * Only sales can be the method * * Private Static void Sell() {System.out.println ("Sales start selling machines to foreigners ..."); }}
To know the progress of the machine, then there must be a new Worker() worker object, at this time with the worker's reference, and then what needs to command the workers (to do 100 machines) and tell the workers to finish giving feedback work.setOnWorkListner(listner) , the workers began to do the machine on demand work.makeMachine() , after the completion of the feedback callback, The sales can be sold.onWorkListner(int num) { TODO }
If you still have doubts, continue to see common callbacks in Android点击事件
The callbacks I've seen at the moment are all method callbacks that occur under a specific condition (for example, the onclick () of a button; the most common)
I first analyze the need to do the real thing is not the corresponding in activity/fragment或者adapter , need to be monitored to see if there is a click on the state of whether Button .
Okay, I know. After the ins and outs, with three elements Su Tong under:
- Interface: Button
As you can see in the source code, the method of setting up the listener is written in the Button class, but OnClickListener in Button the parent class View .
publicvoidsetOnClickListener(@Nullable OnClickListener l) {? if (!isClickable()) {? setClickable(true);? }? getListenerInfo().mOnClickListener = l;?}
- Implementations and references
In the activity , when clicked button , I need to open a page MainActivity , so button the application and setOnClickListener(listner) methods are written in this activity .
button.setOnClickListener(new OnClickListener() {?@Override?publicvoidonClick(View v) {? mActivity.startActivity(new Intent(mActivity, MainActivity.class));?}});
Java callback function So to speak, you should understand!