我有5个复选框,并使用RxBinding检查所有复选框的状态。
预期的逻辑(变量名以'cb'开头的是Checkboxes,以'btn'开头的是Button)
第一和第二逻辑的工作与我期望的一样,但是当所有复选框都选中时,并且取消选中cbOption1〜cbOption4中的一个,则所有复选框都被取消选中。并且4也不起作用。
我的代码如下,
Disposable cbAgreeallDisposable = RxCompoundButton.checkedChanges(binding.cbAgreeall).subscribe(check -> {
isAllAgree = check;
binding.cbOption1.setChecked(check);
binding.cbOption2.setChecked(check);
binding.cbOption3.setChecked(check);
binding.cbOption4.setChecked(check);
});
Observable<Boolean> cbOption1 = RxCompoundButton.checkedChanges(binding.cbOption1);
Observable<Boolean> cbOption2 = RxCompoundButton.checkedChanges(binding.cbOption2);
Observable<Boolean> cbOption3 = RxCompoundButton.checkedChanges(binding.cbOption3);
Observable<Boolean> cbOption4 = RxCompoundButton.checkedChanges(binding.cbOption4);
compositeDisposable.add(cbAgreeallDisposable);
compositeDisposable.add(cbOption1.subscribe(check -> isOption1Agree = check));
compositeDisposable.add(cbOption2.subscribe(check -> isOption2Agree = check));
compositeDisposable.add(cbOption3.subscribe(check -> isOption3Agree = check));
compositeDisposable.add(cbOption4.subscribe(check -> isOption4Agree = check));
compositeDisposable.add(Observable
.combineLatest(cbOption1, cbOption2,
(cb1, cb2) -> cb1 && cb2)
.subscribe(isValid -> binding.btnNext.setEnabled(isValid))
);
compositeDisposable.add(Observable
.combineLatest(cbOption1, cbOption2, cbOption3, cbOption4,
(cb1, cb2, cb3, cb4) -> cb1 && cb2 && cb3 && cb4)
.subscribe(isValid -> binding.cbAgreeall.setChecked(isValid)
));