引言
RxBus实际上是EventBus的另一种实现模式,使用了RxJava方式。
--
效果预览
RxBus.gif用法
第一步:添加依赖(app下build.gradle中)
//RxJava,RxBus(被内包)
implementation 'io.reactivex:rxjava:1.2.2'
implementation 'io.reactivex:rxandroid:1.2.1'
第二步:布局文件(ActivityA)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".blog.ActivityA">
<Button
android:id="@+id/btnIntentC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到ActivityC"
android:layout_margin="10dp"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/tvMessageC"
app:layout_constraintVertical_chainStyle="packed"
android:textAllCaps="false" />
<TextView
android:id="@+id/tvMessageC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_margin="10dp"
app:layout_constraintTop_toBottomOf="@id/btnIntentC"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="接受ActivityC返回结果" />
</androidx.constraintlayout.widget.ConstraintLayout>
布局文件(ActivityC)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".blog.ActivityC">
<Button
android:id="@+id/btnSendMes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送事件"
android:textSize="20sp"
android:layout_margin="10dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textAllCaps="false" />
</androidx.constraintlayout.widget.ConstraintLayout>
第三步:创建MessageEvent定义消息事件类
package com.example.mydemo.entity;
/**
* @data on 2020/10/15 1:43 PM
* @auther armstrong
* @describe RxBus实战->定义消息事件类
*/
public class MessageEvent {
private String message;
public MessageEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
第四步:创建RxBus工具类
/**
* @data on 2020/10/15 2:35 PM
* @auther armstrong
* @describe RxBus操作类
*/
public class RxBus {
private static volatile RxBus instance;
private Subject<Object, Object> bus;
private RxBus() {
bus = new SerializedSubject<>(PublishSubject.create());
}
public static RxBus getDefault() {
if (instance == null) {
synchronized (RxBus.class) {
instance = new RxBus();
}
}
return instance;
}
/**
* 发送事件
* @param object
*/
public void post(Object object) {
bus.onNext(object);
}
/**
* 根据类型接收相应类型事件
* @param eventType
* @param <T>
* @return
*/
public <T> Observable toObservable(Class<T> eventType) {
return bus.ofType(eventType);
}
}
第五步:在Activity中书写业务逻辑(ActivityA中)
Tips:点击事件中,我用到了java8中的特性:Lambda表达式,简化了匿名内部类,你可以采用传统的写法new 完OnClickListener重写onClick方法,也可以看我的这篇文章完成Lambda配置,从而简化你的代码。
//RxBus--EventBus的升级版,可替代EventBus,进行事件发布订阅。
public class ActivityA extends AppCompatActivity {
private Button btnIntentC;
private TextView tvMessageC;
private ArrayList<Subscription> rxBusList = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
initView();
}
private void initView(){
btnIntentC = findViewById(R.id.btnIntentC);
tvMessageC = findViewById(R.id.tvMessageC);
tvMessageC.setText("千夜零一");
btnIntentC.setOnClickListener((View)->{
startActivity(new Intent(this,ActivityC.class));
});
}
@Override
protected void onResume() {
super.onResume();
receive();
}
private void receive(){
Subscription subscription = RxBus.getDefault().toObservable(MessageEvent.class)
.subscribe(new Action1<MessageEvent>() {
@Override
public void call(MessageEvent messageEvent) {
tvMessageC.setText(messageEvent.getMessage());
}
});
rxBusList.add(subscription);
}
@Override
protected void onDestroy() {
super.onDestroy();
clearSubscription();
}
/**
* 取消该页面所有订阅
*/
private void clearSubscription() {
for (Subscription subscription : rxBusList) { //增强型for循环,遍历rxBusList
if (subscription != null && subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
}
}
在Activity中书写业务逻辑(ActivityB中)
//用于RxBus实现发布-订阅事件总线,替代EventBus
public class ActivityC extends AppCompatActivity {
private Button btnSendMes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
initView();
}
private void initView(){
btnSendMes = findViewById(R.id.btnSendMes);
btnSendMes.setOnClickListener((View)->{
RxBus.getDefault().post(new MessageEvent("接收到了RxBus发布的事件"));
finish();
});
}
}
网友评论