RxJava

RxJava日常使用总结(三)过滤操作

2020-10-25  本文已影响0人  h2coder

过滤操作符,对数据流做条件过滤操作,就像流水间上的质检员,对不符合规则的零件剔除。

Distinct操作符。抑制(过滤掉)重复的数据项。简单来说就是去重。

image.png
Observable.fromIterable(mItems)
                .distinct()
                .toList()
                .toObservable()
                .as(RxLifecycleUtil.bindLifecycle(this))
                .subscribe(new Consumer<List<Object>>() {
                    @Override
                    public void accept(List<Object> objects) throws Exception {
                        //...
                    }
                });

ElementAt操作符。只发射第N项数据。类似集合的ArrayList.get(index)。取出对应位置的数据。在RxJava事件流中就是只发送指定位置的数据事件。

image.png
int index = 0;
        Observable.fromIterable(mItems)
                .elementAt(index)
                .cast(SingleSelectOptionModel.class)
                .as(RxLifecycleUtil.bindLifecycle(this))
                .subscribe(new Consumer<SingleSelectOptionModel>() {
                    @Override
                    public void accept(SingleSelectOptionModel optionModel) throws Exception {
                        optionModel.setSelect(true);
                        mAdapter.notifyItemChanged(index);
                    }
                });

firstElement操作符。

Observable.fromIterable(mItems)
                .filter(new Predicate<Object>() {
                    @Override
                    public boolean test(Object itemModel) throws Exception {
                        return itemModel instanceof SingleSelectOptionModel;
                    }
                })
                .cast(SingleSelectOptionModel.class)
                .firstElement()
                .toObservable()
                .as(RxLifecycleUtil.bindLifecycle(this))
                .subscribe(new Consumer<SingleSelectOptionModel>() {
                    @Override
                    public void accept(SingleSelectOptionModel singleSelectOptionModel) throws Exception {
                        //...
                    }
                });

filter操作符。只发射通过了谓词测试的数据项。其实就是一个过滤器,传入一个Predicate谓词接口实现参数,复写test方法,返回true代表通过,返回false则代表需要过滤掉。

image.png
RxUtil.click(vSearch)
                .map(new Function<Object, String>() {
                    @Override
                    public String apply(Object o) throws Exception {
                        return vSearchEdit.getText().toString().trim();
                    }
                })
                .filter(new Predicate<String>() {
                    @Override
                    public boolean test(String inputText) throws Exception {
                        boolean isEmpty = TextUtils.isEmpty(inputText);
                        if (isEmpty) {
                            showToast(R.string.recommend_search_input_empty_tip);
                        }
                        return !isEmpty;
                    }
                })
                .as(RxLifecycleUtil.bindLifecycle(this))
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String inputText) throws Exception {
                        search(inputText);
                    }
                });

first和last操作符。first代表取出第一个事件发送,last则是取出最后一个事件发送,它们需要传一个defaultItem的参数,作为如果找不到时发送的默认值。

image.png image.png
Observable.fromIterable(mItems)
                .filter(new Predicate<Object>() {
                    @Override
                    public boolean test(Object itemModel) throws Exception {
                        return itemModel instanceof SingleSelectOptionModel;
                    }
                })
                .cast(SingleSelectOptionModel.class)
                .filter(new Predicate<SingleSelectOptionModel>() {
                    @Override
                    public boolean test(SingleSelectOptionModel model) throws Exception {
                        return "我们会怎么样".equals(model.getTitle());
                    }
                })
                .first(new SingleSelectOptionModel("找不到"))
                .toObservable()
                .as(RxLifecycleUtil.bindLifecycle(this))
                .subscribe(new Consumer<SingleSelectOptionModel>() {
                    @Override
                    public void accept(SingleSelectOptionModel singleSelectOptionModel) throws Exception {
                        //...
                    }
                });

skip操作符,抑制Observable发射的前N项数据。就是跳过发送的事件的前多少个。

image.png
RxLunarDataTimePicker.pickChange(mDataTimePopupDialog)
                //忽略掉一开始发送的初始化默认值
                .skip(1)
                .as(RxLifecycleUtil.bindLifecycle(this))
                .subscribe(new Consumer<LunarDataTimePickInfo>() {
                    @Override
                    public void accept(LunarDataTimePickInfo info) throws Exception {
                        int type = info.getType();
                        CalendarTypeEnum calendarTypeEnum;
                        if (DatePickerView.DATE_TYPE_SOLAR == type) {
                            calendarTypeEnum = CalendarTypeEnum.SOLAR;
                        } else if (DatePickerView.DATE_TYPE_LUNAR == type) {
                            calendarTypeEnum = CalendarTypeEnum.LUNAR;
                        } else {
                            calendarTypeEnum = CalendarTypeEnum.SOLAR;
                        }
                        mCalendar.set(Calendar.YEAR, info.getYear());
                        mCalendar.set(Calendar.MONTH, info.getMonthOfYear() - 1);
                        mCalendar.set(Calendar.DAY_OF_MONTH, info.getDayOfMonth());
                        mCalendar.set(Calendar.HOUR_OF_DAY, info.getHour());
                        mCalendar.set(Calendar.MINUTE, 0);
                        mCalendar.set(Calendar.MILLISECOND, 0);
                        updateBirthday(mCalendar.getTime().getTime(), calendarTypeEnum);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });

skipLast操作符。和skip操作符一样,同样是跳过指定数量的事件,只是skip是跳过前面开始指定数量的事件,skipLast则是从后面开始,跳过指定数量的事件。

image.png

take操作符。只发射前面的N项数据。就是从前面开始,保留指定数量的事件。

image.png
//指定数量的,没有设置图片的占位符
        Observable<PressQuestionUploadImageModel> listItemObservable = Observable.fromIterable(mItems)
                .filter(new Predicate<Object>() {
                    @Override
                    public boolean test(Object target) throws Exception {
                        return target instanceof PressQuestionUploadImageModel
                                && ((PressQuestionUploadImageModel) target).getPicFilePath() == null;
                    }
                })
                .cast(PressQuestionUploadImageModel.class)
                //限制只发送未设置的图片的指定数量
                .take(imgPaths.size());
        return Observable.zip(listItemObservable, Observable.fromIterable(imgPaths), new BiFunction<PressQuestionUploadImageModel, String, PressQuestionUploadImageModel>() {
            @Override
            public PressQuestionUploadImageModel apply(PressQuestionUploadImageModel model, String path) throws Exception {
                model.setPicFilePath(path);
                return model;
            }
        })

takeLast操作符,和take一样,take是从前面开始保留指定数量的事件,而takeLast则是从后面开始,如果有需求是从后面开始填充的话,就可以使takeLast操作符。

image.png
上一篇 下一篇

猜你喜欢

热点阅读