如何在Android中为Flowable创建观察器

时间:2018-07-10 06:46:39

标签: android rx-java2

我可以成功创建Flowable并订阅它们,但是我不明白如何为其创建Observer。

以下是我的可流动代码

 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<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 !!!!");
            }

        };
    }
}

如何为flowable创建类似的观察器,以便可以在UI中进行更改?

所以在可观察的情况下,我曾经做过

observable
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            //.toFlowable(BackpressureStrategy.BUFFER)
            .subscribe(getMyObserver());

那么我如何为flowable创建类似的观察者?

0 个答案:

没有答案