如何在Android rxJava 2.0中订阅Flowable

时间:2018-07-09 10:51:17

标签: android rx-java2

我是android中的rxJava的新手。我已经成功创建了可观察的对象。现在,我想创建flowable。我不太清楚flowable是否以相同的方式工作。以下是我的可观察代码,效果很好。

 Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
    @Override
    public void subscribe(final ObservableEmitter<String> e) throws Exception {
        //Use onNext to emit each item in the stream//
        t = new Timer();                                                                 
        t.scheduleAtFixedRate(
            new TimerTask() {
              public void run() {
                if (listObj.size() > 0) {
                  varToBeAddedInObservable = listObj.remove(listObj.size() - 1);
                  e.onNext(varToBeAddedInObservable);
                } else {
                  e.onComplete();
                  t.cancel();
                }
              }
            },
            0,      // run first occurrence immediatetly
            1000);
    }
  }
    );

/////////

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

//////////////

     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进行相同操作,但是不允许我订阅,那么该怎么做?我读到,需要有一些类似一次性的东西才能订阅可流动货币,但我并没有获得适当的榜样。谢谢:)

 private Flowable<String> getMyflowable() {
    return new Flowable<String>() {
        @Override
        protected void subscribeActual(Subscriber<? super String> s) {

        }


    };
}

0 个答案:

没有答案