如何在android中为flowable创建观察者
我可以成功地创建Flowable并订阅它们,但是我不知道如何为它创建Observer 下面是我的flowable代码如何在android中为flowable创建观察者,android,rx-java2,Android,Rx Java2,我可以成功地创建Flowable并订阅它们,但是我不知道如何为它创建Observer 下面是我的flowable代码 FlowableOnSubscribe<String> flowableOnSubscribe = new FlowableOnSubscribe<String>() { @Override public void subscribe(final FlowableEmitter<String> emitter)
FlowableOnSubscribe<String> flowableOnSubscribe = new FlowableOnSubscribe<String>() {
@Override
public void subscribe(final FlowableEmitter<String> emitter) throws Exception {
t = new Timer();
t.scheduleAtFixedRate(
new TimerTask() {
public void run() {
if (listObj.size() > 0) {
varToBeAddedInObservable = listObj.remove(listObj.size() - 1);
// Log.d("&&&&&&&&&&",""+varToBeAddedInObservable);
emitter.onNext(varToBeAddedInObservable);
} else {
emitter.onComplete();
t.cancel();
}
}
},
0, // run first occurrence immediatetly
1000);
}
};
那么,我如何才能为flowable创建一个类似的观察者呢?请阅读“是的,我正在关注该站点本身”,我在示例中看到了这一点。subscribe(System.out::println,Throwable::printStackTrace);但我需要根据observer中的onNext事件进行一些UI更改。那怎么做呢?
Flowable<String> mFlowable = Flowable.create(flowableOnSubscribe, BackpressureStrategy.BUFFER);
mFlowable
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.single()).subscribe();
private Observer<String> getMyObserver() {
return new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe");
}
@Override
public void onNext(String s) {
Log.d(TAG, "Name: " + s);
int colorIndex = random.nextInt(arrayColors.length);
//Color color = (arrayColors[colorIndex]);
textView.setTextColor(arrayColors[colorIndex]);
textView.setText(varToBeAddedInObservable);
AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f);
// AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ;
textView.startAnimation(fadeIn);
// textView.startAnimation(fadeOut);
fadeIn.setDuration(1500);
fadeIn.setFillAfter(true);
/* fadeOut.setDuration(1200);
fadeOut.setFillAfter(true);
fadeOut.setStartOffset(1200 - fadeIn.getStartOffset());
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
textView.setTextColor(Color.BLACK);
textView.setText("Khallas !!!!");
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});*/
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.d(TAG, "All items are emitted!");
textView.setTextColor(Color.BLACK);
textView.setText("Finished !!!!");
}
};
}
}
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
//.toFlowable(BackpressureStrategy.BUFFER)
.subscribe(getMyObserver());