在JavaFX中,您可以执行以下操作来组合两个可观察的布尔值:
BooleanProperty imagesDownloaded = new SimpleBooleanProperty(false);
BooleanProperty animationComplete = new SimpleBooleanProperty(false);
BooleanBinding isValid = imagesDownloaded.and(animationComplete);
如何使用RxJava或谷歌的Databinding API做同样的事情?
我还想听一下isValid变量的值变化.
解决方法:
您可以根据目标使用Observable.combineLatest()
,Observable.zip()
,Observable.merge()
和其他operators.
我只是添加一个简短的例子来展示RxJava中的内容:
PublishSubject<Boolean> property1 = PublishSubject.create();
PublishSubject<Boolean> property2 = PublishSubject.create();
Observable.combineLatest(property1,
property2,
(propertyOneValue, propertyTwoValue) -> propertyOneValue && propertyTwoValue)
.subscribe(isValid -> doWork(isValid));
// sometime later
property1.onNext(true);
property2.onNext(true);