EventBus是一个第三方开源框架。它的作用主要是解决各组件、组件与后台线程间的信息沟通问题。
比如,如何在Activity_B中修改Activity_A中的TextView显示信息?
之前的做法是:接口回调或者通过广播。现在只需要通过EventBus就可以轻松解决,主要是为了项目的解耦。
EventBus项目地址:https://github.com/greenrobot/EventBus
EventBus和广播有点类似,需要Event事件、事件订阅者、事件发布者三项。
EventBus有四种线程模型,分别是:
基本使用步骤如下:
(1)添加远程依赖:
implementation 'org.greenrobot:eventbus:3.1.1'
(2)自定义一个事件类(泛型)
/** * 事件类(泛型) * @param*/ public class MessageEvent { private T message; public MessageEvent(T message) { this.message = message; } public T getMessage() { return message; } public void setMessage(T message) { this.message = message; } }
(3)在OneActivity.java中注册事件以及解除注册事件,代码如下:
public class OneActivity extends BaseActivity implements View.OnClickListener { private Context mContext; private TextView tvInfo; private Button btnJump; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_eventbus_one); baseDataInit(); bindViews(); viewsAddListener(); viewsDataInit(); EventBus.getDefault().register(this); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void alertInfo(MessageEvent messageEvent) { tvInfo.setText(((Tb_MessageInfo)messageEvent.getMessage()).getName()); } @Override public void baseDataInit() { mContext = this; } @Override public void bindViews() { tvInfo = findViewById(R.id.EventBus_One_tvInfo); btnJump = findViewById(R.id.EventBus_One_btnSwitch); } @Override public void viewsAddListener() { btnJump.setOnClickListener(this::onClick); } @Override public void viewsDataInit() { tvInfo.setText("信息显示"); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.EventBus_One_btnSwitch: { //EventBus.getDefault().postSticky(new MessageEvent("发送粘性事件内容用于EditText显示")); startActivity(new Intent(mContext, TwoActivity.class)); break; } default:break; } } }
备注:主要是那个处理事件代码如下:
@Subscribe(threadMode = ThreadMode.MAIN) public void alertInfo(MessageEvent messageEvent) { tvInfo.setText(((Tb_MessageInfo)messageEvent.getMessage()).getName()); }
(4)在TwoActivity.java中发布事件,代码如下:
public class TwoActivity extends BaseActivity implements View.OnClickListener { private Context mContext; private EditText etContent; private Button btnSubmit; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_eventbus_two); baseDataInit(); bindViews(); viewsAddListener(); viewsDataInit(); //EventBus.getDefault().register(this); } @Override protected void onDestroy() { super.onDestroy(); //EventBus.getDefault().unregister(this); } /*@Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void editTextShow(MessageEvent messageEvent) { etContent.setText(messageEvent.getMessage()); }*/ @Override public void baseDataInit() { mContext = this; } @Override public void bindViews() { etContent = findViewById(R.id.EventBus_Two_etContent); btnSubmit = findViewById(R.id.EventBus_Two_btnSubmit); } @Override public void viewsAddListener() { btnSubmit.setOnClickListener(this::onClick); } @Override public void viewsDataInit() { } @Override public void onClick(View v) { switch (v.getId()) { case R.id.EventBus_Two_btnSubmit: { String name = etContent.getText().toString().trim(); EventBus.getDefault().post(new MessageEvent(new Tb_MessageInfo(name, "17"))); finish(); break; } default:break; } } }
备注:主要是那个发布事件代码如下:
EventBus.getDefault().post(new MessageEvent(new Tb_MessageInfo(name, "17")));
PS:还有一种是粘性事件,它主要是A界面事件已发布,进入B界面,但是B界面还想收到该事件的情况下进行处理的。(说白了,就是消息已发布,但是还没有人注册,没人收得到。)和普通事件有两点区别:
(1)处理事件的方法加了,sticky=true,代码如下:
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void editTextShow(MessageEvent messageEvent) { etContent.setText(((Tb_MessageInfo)messageEvent.getMessage()).getName()); }
(2)发布事件的方法也变了,代码如下:
EventBus.getDefault().postSticky(new MessageEvent(new Tb_MessageInfo("张三", "17")));