时间:2021-07-01 10:21:17 帮助过:17人阅读
对其进行订阅
- private Observable
> bufferObserver() {
- return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).buffer(2, 3);
- }
- private Observable
> bufferTimeObserver() {
- return Observable.interval(1, TimeUnit.SECONDS).buffer(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread());
- }
- mLButton.setText("buffer");
- mLButton.setOnClickListener(e -> bufferObserver().subscribe(i -> log("buffer:" + i)));
- mRButton.setText("bufferTime");
- mRButton.setOnClickListener(e -> bufferTimeObserver().subscribe(i -> log("bufferTime:" + i)));
分别对其进行订阅
- private Observable
flatMapObserver() { - return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).flatMap(integer -> Observable.just("flat map:" + integer));
- }
- private Observable extends Integer> flatMapIterableObserver() {
- return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
- .flatMapIterable(
- integer -> {
- ArrayList
s = new ArrayList<>(); - for (int i = 0; i < integer; i++) {
- s.add(integer);
- }
- return s;
- }
- );
- }
- mLButton.setText("flatMap");
- mLButton.setOnClickListener(e -> flatMapObserver().subscribe(i -> log(i)));
- mRButton.setText("flatMapIterable");
- mRButton.setOnClickListener(e -> flatMapIterableObserver().subscribe(i -> log("flatMapIterable:" + i)));
- mLButton.setText("groupBy");
- mLButton.setOnClickListener(e -> groupByObserver().subscribe(new Subscriber
>() { - @Override
- public void onCompleted() {
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onNext(GroupedObservable
groupedObservable) { - groupedObservable.count().subscribe(integer -> log("key" + groupedObservable.getKey() + " contains:" + integer + " numbers"));
- }
- }));
- mRButton.setText("groupByKeyValue");
- mRButton.setOnClickListener(e -> groupByKeyValueObserver().subscribe(new Subscriber
>() { - @Override
- public void onCompleted() {
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onNext(GroupedObservable
integerIntegerGroupedObservable) { - if (integerIntegerGroupedObservable.getKey() == 0) {
- integerIntegerGroupedObservable.subscribe(integer -> log(integer));
- }
- }
- }));
- }
对其进行注册
- private Observable
mapObserver() { - return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).map(integer -> integer * 10);
- }
- private Observable
castObserver() { - return Observable.just(getAnimal())
- .cast(Dog.class);
- }
- Animal getAnimal() {
- return new Dog();
- }
- class Animal {
- protected String name = "Animal";
- Animal() {
- log("create " + name);
- }
- String getName() {
- return name;
- }
- }
- class Dog extends Animal {
- Dog() {
- name = getClass().getSimpleName();
- log("create " + name);
- }
- }
运行后得到结果如下。可以看到,map操作符将数据都乘以10后再发射出来,cast操作符将Animal类型的对象强制转化为Dog类型的对象。另外我们还可以验证一下一个知识点,有继承的情况下创建对象会首先调用父类的构造方法哦。
- mLButton.setText("Map");
- mLButton.setOnClickListener(e -> mapObserver().subscribe(i -> log("Map:" + i)));
- mRButton.setText("Cast");
- mRButton.setOnClickListener(e -> castObserver().subscribe(i -> log("Cast:" + i.getName())));
对其进行订阅
- private Observable
scanObserver() { - return Observable.from(list).scan((x, y) -> x * y).observeOn(AndroidSchedulers.mainThread());
- }
得到结果如下,可以看到,我们输出了2的n次方。
- mLButton.setText("scan");
- mLButton.setOnClickListener(e -> scanObserver().subscribe(i -> log("scan:" + i)));
分别对其订阅
- private Observable
> windowCountObserver() { - return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).window(3);
- }
- private Observable
> wondowTimeObserver() { - return Observable.interval(1000, TimeUnit.MILLISECONDS)
- .window(3000, TimeUnit.MILLISECONDS)
- .observeOn(AndroidSchedulers.mainThread());
- }
运行结果如下,可以看到第一个Observable对象没次发射出一个包含3个数据的小Observable,第二个Observable对象每隔3秒钟发射出一个包含2~4个数据的Observable对象
- mLButton.setText("window");
- mLButton.setOnClickListener(e -> windowCountObserver().subscribe(i -> {
- log(i);
- i.subscribe((j -> log("window:" + j)));
- }));
- mRButton.setText("Time");
- mRButton.setOnClickListener(e -> wondowTimeObserver().subscribe(i -> {
- log(i);
- i.observeOn(AndroidSchedulers.mainThread()).subscribe((j -> log("wondowTime:" + j)));
- }));
http://www.bkjia.com/PHPjc/1077811.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1077811.htmlTechArticleRxJava操作符(二)Transforming Observables 在上一篇文章中,我们了解了如何创建Observable,仅仅创建一个Observable可能无法满足一些复杂的场景,...