Callback回调事件介绍
Java或Android中创建异步回调最普遍的做法就是使用listener监听器或者observer观察者模式来解决,listener回调事件通常用于实现一个代码去监听另一个类或方法发生的事情,Android中的监听器listener最流行的莫过于Button按钮的OnClickListener了,下面来开始学习吧!
先来看看Button的onClick事件的代码吧!
Button btnExample = (Button) findViewById(R.id.btnExample); btnExample.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do something here } });
如何自定义Android回调事件
我们要做一个类似于Button按钮的回调事件,该如何做呢?先来看自定义listener监听事件的步骤:
1:定义一个接口
2:父类中设置一个成员变量,以及setter方法
3:实现监听事件内部的接口
4:触发Java监听事件
一:定义一个监听事件接口:
public class MyCustomObject { // Step 1 - This interface defines the type of messages I want to communicate to my owner public interface MyCustomObjectListener { public void onObjectReady(String title); public void onDataLoaded(SomeData data); } }
二:创建Listener成员变量,代码如下:
public class MyCustomObject { // ... // Step 2 - This variable represents the listener passed in by the owning object // The listener must implement the events interface and passes messages up to the parent. private MyCustomObjectListener listener; }
在父类中添加setter方法,代码如下:
public class MyCustomObject { // Constructor where listener events are ignored public MyCustomObject() { // set null or default listener or accept as argument to constructor this.listener = null; } // Assign the listener implementing events interface that will receive the events public void setCustomObjectListener(MyCustomObjectListener listener) { this.listener = listener; } }
三:在别的类中实现监听事件内部的接口
public class MyParentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { object.setCustomObjectListener(new MyCustomObject.MyCustomObjectListener() { @Override public void onObjectReady(String title) { // Code to handle object ready } @Override public void onDataLoaded(SomeData data) { // Code to handle data loaded from network // Use the data here! } }); } }
四:触发Callback监听事件:
public class MyCustomObject { // Listener defined earlier public interface MyCustomObjectListener { public void onObjectReady(String title); public void onDataLoaded(SomeData data); } // Member variable was defined earlier private MyCustomObjectListener listener; public MyCustomObject() { // set null or default listener or accept as argument to constructor this.listener = null; loadDataAsync(); } // ... setter defined here as shown earlier //假如这是一个网络加载事件 public void loadDataAsync() { AsyncHttpClient client = new AsyncHttpClient(); client.get("https://mycustomapi.com/data/get.json", new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { SomeData data = SomeData.processData(response.get("data")); // 触发Java监听事件 if (listener != null) listener.onDataLoaded(data); // <---- fire listener here } }); } }
总结
会“Android自定义监听器Listener(自定义Java Callback回调事件)”是很有必要的,java监听事件通常用于异步网络加载,Android activity或fragment之间的参数传递,事件回调等,很有必要!
Java接口回调的几种方式
1:通过方法来回调监听事件
2:通过构造器来回调监听事件
3:通过lifecycle生命周期来回调参数
一:通过方法来回调监听事件
下方演示的是通过接口方法来回调listener监听事件,代码如下:
// 在父类方法中 childObject.setCustomObjectListener(new MyCustomObject.MyCustomObjectListener() { @Override public void onObjectReady(String title) { // Code to handle object ready } });
我们在MyCustomObject父类中是这样定义的,代码:
// Inside the child object private MyCustomObjectListener listener; // 将接口作为参数传入方法中 public void setCustomObjectListener(MyCustomObjectListener listener) { this.listener = listener; }
二:通过构造器来回调监听事件
将接口作为参数放在MyCustomObject父类的构造方法中,代码如下:
// Inside the child object private MyCustomObjectListener listener; // 将接口放在构造方法中 public MyCustomObject(MyCustomObjectListener listener) { this.listener = listener; }
我们可以在创建父类对象时,这样使用构造器回调,代码如下:
// Inside the parent object MyCustomObject object = new MyCustomObject(new MyCustomObject.MyCustomObjectListener() { @Override public void onObjectReady(String title) { // Code to handle object ready }); });
三:通过lifecycle生命周期来回调接口,一般是在另外一个类中直接实现接口类就行了
public class MyListFragment extends Fragment { private MyCustomObjectListener listener; @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof MyCustomObjectListener) { listener = (MyCustomObjectListener) context; } else { throw new ClassCastException(context.toString() + " must implement MyListFragment.MyCustomObjectListener"); } } }
具体怎么使用生命周期来回调接口,后续小编会制作完整教程供大家使用。