(三)RxJava2常用操作符merge、flatmap、zip--结合MVP架构讲解

效果图如下:


demo3.gif

Demo3
1.merge
应用场景:复杂页面一次性要调用多个接口使用
2.flatMap
应用场景:一个接口调用时依赖另一个接口的返回值,如一个接口调用成功后才调用第二个接口或者第二个接口需要第一个接口的返回值作为参数调用
3.zip
应用场景:两个接口的数据相互结合才能显示UI

结合MVP代码实现

连接类contact

public interface IDemo3Contract {
    interface View extends IView {
        void setData1(String rate);
        void setData2(List dataList);
        void setData3(String data3);
        void setData4(List dataList);

    }

    interface Presenter extends IPresenter {
        void getMergeData();
        void getFlatMapData(String type);
        void getZipData();

    }

    interface Model extends IModel {
        void getMergeData(Observer observer);
        void getFlatMapData(String type,Observer observer);
        void getZipData(Observer> observer);

    }
}
 
 

数据层Model

public class Demo3Model extends BaseModel implements IDemo3Contract.Model {

    @Override
    public void getMergeData(Observer observer) {
        NetApi netApi= NetClient.getInstance().getNetApi();
        Observable.merge(netApi.getInsureRate(),netApi.getGoodsType())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void getFlatMapData(String type,Observer observer) {
        final NetApi netApi= NetClient.getInstance().getNetApi();
        netApi.getAgreementList(type)
                .flatMap(new Function>() {
                    @Override
                    public ObservableSource apply(TestBean4 testBean4)  throws Exception {
                        return netApi.getAgreementDetail(testBean4.getDate().get(0).getId());
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void getZipData(Observer> observer) {
        NetApi netApi= NetClient.getInstance().getNetApi();
        Observable.zip(netApi.getAgreementList("员工须知"), netApi.getGoodsType(), new BiFunction>() {
            @Override
            public List apply(TestBean4 testBean4, TestBean2 testBean2) throws Exception {
                //结合数据为一体
                List list=new ArrayList<>();
                List data1=testBean4.getDate();
                List data2=testBean2.getDate();
                for (int i = 0; i < data1.size(); i++) {
                    list.add(new TestBean5(data2.get(i).getName(),data1.get(i).getTitle(),data1.get(i).getAddtime()));
                }
                return list;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
 
 

业务类 presenter

public class Demo3Presenter extends BasePresenter implements IDemo3Contract.Presenter {

    @Override
    protected IDemo3Contract.Model createModel() {
        return new Demo3Model();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void getMergeData() {
        mView.showDialogLoading();
        mModel.getMergeData(new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object o) {
                mView.dismissDialogLoading();
                if (o instanceof TestBean1) {
                    if (((TestBean1) o).getMsg().equals(Params.SUCCESS_MSG)) {
                        mView.setData1(((TestBean1) o).getDate().getProportion());
                    } else {
                        mView.showMessage(((TestBean1) o).getDescribe());
                    }
                } else if (o instanceof TestBean2) {
                    if (((TestBean2) o).getMsg().equals(Params.SUCCESS_MSG)) {

                        mView.setData2(((TestBean2) o).getDate());
                    } else {
                        mView.showMessage(((TestBean2) o).getDescribe());
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissDialogLoading();
                mView.errorDialog();
            }

            @Override
            public void onComplete() {
                mView.dismissDialogLoading();
            }
        });
    }

    @Override
    public void getFlatMapData(String type) {
        mView.showDialogLoading();
        mModel.getFlatMapData(type, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TestBean3 testBean3) {
                mView.dismissDialogLoading();
                if (testBean3.getMsg().equals(Params.SUCCESS_MSG)) {
                    mView.setData3(testBean3.getData().toString());
                }else {
                    mView.showMessage(testBean3.getDescribe());
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissDialogLoading();
                mView.errorDialog();
            }

            @Override
            public void onComplete() {
                mView.dismissDialogLoading();
            }
        });
    }

    @Override
    public void getZipData() {
        mView.showDialogLoading();
        mModel.getZipData(new Observer>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List testBean5s) {
                mView.dismissDialogLoading();
               mView.setData4(testBean5s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissDialogLoading();
                mView.errorDialog();
            }

            @Override
            public void onComplete() {
                mView.dismissDialogLoading();
            }
        });
    }
}

 
 

(一)高斯模糊实现毛玻璃效果丶共享元素动画 丶地址选择器
(二)仿京东顶部伸缩渐变丶自定义viewpager指示器丶viewpager3D回廊丶recyclerview瀑布流
(四)仿支付宝首页顶部伸缩滑动/中间层下拉刷新
(五)TabLayout+ViewPager悬浮吸顶及刷新数量动画显示
(六)仿QQ首页drawer/侧滑删除/浮动imgaeView/角标拖拽
(七)仿微信发布朋友圈拖拽删除

将持续更新.. 不喜勿喷,仅个人分享,希望能帮助到你

源码地址:Github传送门

你可能感兴趣的:((三)RxJava2常用操作符merge、flatmap、zip--结合MVP架构讲解)