android 监听多个 MutableLiveData 属性变化?
视图代码如下:
<Button
// ...
android:text="@{vm.getText()}"
/>
java代码如下:
class TestVM extends ViewModel {
private final MutableLiveData<Boolean> isRequest = new MutableLiveData<>();
private final MutableLiveData<Integer> total = new MutableLiveData<>();
public TestVM() {
this.isReqeust.setValue(false);
this.total.setValue(10);
}
public String getText() {
if (this.isRequest.getValue()) {
return "请求中";
}
int total = this.total.getValue();
if (total >= 1000) {
return '999+';
}
return String.valueOf(total);
}
}
上述代码在 isRequest
或 total
变化的时候,界面上显示的文本不更新,请问该怎么解决?
用 MediatorLiveData:
或者