将两个可观察的布尔值与 ObservableBoolean 或 Observable<Boolean>
Combine two obserable booleans with either ObservableBoolean or Observable<Boolean>
在 JavaFX 中,您可以执行以下操作来组合两个可观察的布尔值:
BooleanProperty imagesDownloaded = new SimpleBooleanProperty(false);
BooleanProperty animationComplete = new SimpleBooleanProperty(false);
BooleanBinding isValid = imagesDownloaded.and(animationComplete);
如何使用 RxJava 或 google 的数据绑定 API 做同样的事情?
我还想听听 isValid
变量的值变化。
您可以根据自己的目标使用 Observable.combineLatest()
, Observable.zip()
, Observable.merge()
, and other 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);
在 JavaFX 中,您可以执行以下操作来组合两个可观察的布尔值:
BooleanProperty imagesDownloaded = new SimpleBooleanProperty(false);
BooleanProperty animationComplete = new SimpleBooleanProperty(false);
BooleanBinding isValid = imagesDownloaded.and(animationComplete);
如何使用 RxJava 或 google 的数据绑定 API 做同样的事情?
我还想听听 isValid
变量的值变化。
您可以根据自己的目标使用 Observable.combineLatest()
, Observable.zip()
, Observable.merge()
, and other 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);