com.jess.arms.utils.RxLifecycleUtils Java Examples

The following examples show how to use com.jess.arms.utils.RxLifecycleUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZhihuHomePresenter.java    From lifecycle-component with Apache License 2.0 6 votes vote down vote up
public void requestDailyList() {
    mModel.getDailyList()
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
            .doOnSubscribe(disposable -> {
                mRootView.showLoading();//显示下拉刷新的进度条
            }).subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(() -> {
                mRootView.hideLoading();//隐藏下拉刷新的进度条
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
            .subscribe(new ErrorHandleSubscriber<DailyListBean>(mErrorHandler) {
                @Override
                public void onNext(DailyListBean dailyListBean) {
                    mDatas.clear();
                    mDatas.addAll(dailyListBean.getStories());
                    mAdapter.notifyDataSetChanged();
                }
            });
}
 
Example #2
Source File: DetailPresenter.java    From lifecycle-component with Apache License 2.0 6 votes vote down vote up
public void requestDetailInfo(int id){
    mModel.getDetailInfo(id)
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
            .doOnSubscribe(disposable -> {
                mRootView.showLoading();//显示下拉刷新的进度条
            }).subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(() -> {
                mRootView.hideLoading();//隐藏下拉刷新的进度条
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
            .subscribe(new ErrorHandleSubscriber<ZhihuDetailBean>(mErrorHandler) {
                @Override
                public void onNext(ZhihuDetailBean zhihuDetailBean) {
                    mRootView.shonContent(zhihuDetailBean);
                }
            });
}
 
Example #3
Source File: RxUtil.java    From lifecycle-component with Apache License 2.0 6 votes vote down vote up
public static <T> ObservableTransformer<T, T> applySchedulers(final IView view) {
    return new ObservableTransformer<T, T>() {
        @Override
        public Observable<T> apply(Observable<T> observable) {
            return observable.subscribeOn(Schedulers.io())
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            view.showLoading();//显示进度条
                        }
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(new Action() {
                        @Override
                        public void run() {
                            view.hideLoading();//隐藏进度条
                        }
                    }).compose(RxLifecycleUtils.bindToLifecycle(view));
        }
    };
}
 
Example #4
Source File: RxUtils.java    From TikTok with Apache License 2.0 6 votes vote down vote up
public static <T> ObservableTransformer<T, T> applySchedulers(final IView view) {
    return new ObservableTransformer<T, T>() {
        @Override
        public Observable<T> apply(Observable<T> observable) {
            return observable.subscribeOn(Schedulers.io())
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            view.showLoading();//显示进度条
                        }
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(new Action() {
                        @Override
                        public void run() {
                            view.hideLoading();//隐藏进度条
                        }
                    }).compose(RxLifecycleUtils.bindToLifecycle(view));
        }
    };
}
 
Example #5
Source File: RxUtils.java    From Aurora with Apache License 2.0 6 votes vote down vote up
public static <T> ObservableTransformer<T, T> applySchedulersWithLifeCycle(IView view) {
    return new ObservableTransformer<T, T>() {
        @Override
        public Observable<T> apply(Observable<T> observable) {
            return observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doAfterTerminate(new Action() {
                        @Override
                        public void run() {
                            view.hideLoading();//隐藏进度条
                        }
                    })
                    .compose(RxLifecycleUtils.bindToLifecycle(view));
        }
    };
}
 
Example #6
Source File: RxUtil.java    From Hands-Chopping with Apache License 2.0 6 votes vote down vote up
public static <T> ObservableTransformer<T, T> applySchedulers(final IView view) {
    return new ObservableTransformer<T, T>() {
        @Override
        public Observable<T> apply(Observable<T> observable) {
            return observable.subscribeOn(Schedulers.io())
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            view.showLoading();//显示进度条
                        }
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(new Action() {
                        @Override
                        public void run() {
                            view.hideLoading();//隐藏进度条
                        }
                    }).compose(RxLifecycleUtils.bindToLifecycle(view));
        }
    };
}
 
Example #7
Source File: DetailPresenter.java    From Hands-Chopping with Apache License 2.0 6 votes vote down vote up
public void requestSteamGameDetail(String id,String name,boolean isBundle){
    mModel.getSteamGameDetail(id,name,isBundle)
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(3,2))
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(()->{
                mRootView.hideLoading();
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
            .subscribe(new ErrorHandleSubscriber<GameDetailBean>(mErrorHandler) {
                @Override
                public void onNext(GameDetailBean gameDetailBean) {
                    if(!isBundle){
                        requestSteamGameReview(id, name,gameDetailBean);
                    }else {
                        mRootView.onSteamFinished(gameDetailBean);
                        mRootView.hideLoading();
                    }
                }
            });
}
 
Example #8
Source File: DetailPresenter.java    From Hands-Chopping with Apache License 2.0 6 votes vote down vote up
public void requestSteamGameReview(String id,String name,GameDetailBean glb){
    mModel.getSteamGameReview(id, name)
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(3,2))
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(()->{
                mRootView.hideLoading();
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
            .subscribe(new ErrorHandleSubscriber<GameDetailBean>(mErrorHandler) {
                @Override
                public void onNext(GameDetailBean gameDetailBean) {
                    glb.setReviews(gameDetailBean.getReviews());
                    glb.setId(id);
                    glb.setTitle(name);
                    mRootView.onSteamFinished(glb);
                }
            });
}
 
Example #9
Source File: RxUtils.java    From Hands-Chopping with Apache License 2.0 6 votes vote down vote up
public static <T> ObservableTransformer<T, T> applySchedulers(final IView view) {
    return new ObservableTransformer<T, T>() {
        @Override
        public Observable<T> apply(Observable<T> observable) {
            return observable.subscribeOn(Schedulers.io())
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            view.showLoading();//显示进度条
                        }
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(new Action() {
                        @Override
                        public void run() {
                            view.hideLoading();//隐藏进度条
                        }
                    }).compose(RxLifecycleUtils.bindToLifecycle(view));
        }
    };
}
 
Example #10
Source File: VideoDetailPresenter.java    From LQRBiliBlili with MIT License 6 votes vote down vote up
public void loadData(String aid) {
        Observable.zip(mModel.getSummaryData(aid), mModel.getReplyData(aid, page, rows), (summary, reply) -> new VideoDetail(summary, reply))
                .retryWhen(new RetryWithDelay(3, 2))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<VideoDetail>(mErrorHandler) {
                    @Override
                    public void onNext(@NonNull VideoDetail videoDetail) {
                        setVideoDetail(videoDetail);
                    }
                });
//        mModel.getSummaryData(aid)
//                .retryWhen(new RetryWithDelay(3, 2))
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<Summary>(mErrorHandler) {
//                    @Override
//                    public void onNext(@NonNull Summary summary) {
//                        setVideoDetail(summary);
//                    }
//                });

    }
 
Example #11
Source File: RecommendPresenter.java    From LQRBiliBlili with MIT License 6 votes vote down vote up
public void loadData(int idx, boolean refresh, boolean clearCache) {
    mModel.getRecommendIndexData(idx, refresh, clearCache)
            .retryWhen(new RetryWithDelay(3, 2))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe(disposable -> {
                if (refresh || clearCache) mRootView.showLoading();
            })
            .doFinally(() -> mRootView.hideLoading())
            .observeOn(Schedulers.io())
            .map(indexData -> mModel.parseIndexData(indexData))
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
            .subscribe(new ErrorHandleSubscriber<List<RecommendMultiItem>>(mErrorHandler) {
                @Override
                public void onNext(@NonNull List<RecommendMultiItem> recommendMultiItems) {
                    if (recommendMultiItems != null) {
                        setAdapter(recommendMultiItems, refresh);
                    }
                }
            });
}
 
Example #12
Source File: SalePresenter.java    From Hands-Chopping with Apache License 2.0 6 votes vote down vote up
public void requestSankoSaleGameList(){
        mModel.getSankoSaleGame()
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3,2))
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<GameSaleList>(mErrorHandler) {
                    @Override
                    public void onNext(GameSaleList gameSaleList) {
                        gsl.setGameListBeanArrayList(gameSaleList.getGameListBeanArrayList());
                        requestSteamSaleGameList();
//                        int i=0;
//                        for(GameListBean bean:gameSaleList.getGameListBeanArrayList()){
//                            i++;
//                            Timber.tag("Sanko").w(bean.getId());
//                            Timber.tag("Sanko").w(bean.getImgUrl());
//                            Timber.tag("Sanko").w(bean.getOff()+" "+i);
//                            Timber.tag("Sanko").w(bean.getOldPrice()+" "+i);
//                            Timber.tag("Sanko").w(bean.getNowPrice()+" "+i);
//                            Timber.tag("Sanko").w(bean.getTitle());
//                        }
                    }
                });
    }
 
Example #13
Source File: SearchPresent.java    From Hands-Chopping with Apache License 2.0 6 votes vote down vote up
public void searchFromSteam(String keyword){

        mModel.getSteamSearchGame(keyword)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3,2))
                .doOnSubscribe(disposable -> {mRootView.showLoading();})
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<GameSearchList>(mErrorHandler){
                    @Override
                    public void onNext(GameSearchList gameSearchList) {
                        gsl.setGameSearchBeanArrayList(gameSearchList.getGameSearchBeanArrayList());
                        searchFromSanko(keyword);
                    }
//                        int i=0;
//                        for(GameSearchBean bean:gameSearchList.getGameSearchBeanArrayList()){
//                            i++;
//                            Timber.tag("Steam").w(bean.getId());
//                            Timber.tag("Steam").w(bean.getImgUrl());
//                            Timber.tag("Steam").w(bean.getPrice()+" "+i);
//                            Timber.tag("Steam").w(bean.getTitle());
//                        }
//                    }
                });
    }
 
Example #14
Source File: RxUtils.java    From Aurora with Apache License 2.0 6 votes vote down vote up
public static <T> ObservableTransformer<T, T> applySchedulers(final IView view,final boolean isLoadMore) {
    return new ObservableTransformer<T, T>() {
        @Override
        public Observable<T> apply(Observable<T> observable) {
            return observable.subscribeOn(Schedulers.io())
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            if (!isLoadMore){
                                view.showLoading();
                            }
                        }
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doAfterTerminate(new Action() {
                        @Override
                        public void run() {
                            view.hideLoading();//隐藏进度条
                        }
                    }).compose(RxLifecycleUtils.bindToLifecycle(view));
        }
    };
}
 
Example #15
Source File: SplashActivity.java    From LQRBiliBlili with MIT License 5 votes vote down vote up
@Override
    public void initData(Bundle savedInstanceState) {
        Observable.timer(2000, TimeUnit.MILLISECONDS)
                .compose(RxLifecycleUtils.bindToLifecycle(this))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    ARouter.getInstance().build("/app/main").navigation();
//                    ArmsUtils.startActivity(MainActivity.class)
                });
    }
 
Example #16
Source File: GankHomePresenter.java    From lifecycle-component with Apache License 2.0 5 votes vote down vote up
public void requestGirls(final boolean pullToRefresh) {
    if (pullToRefresh) lastPage = 1;//下拉刷新默认只请求第一页

    mModel.getGirlList(NUMBER_OF_PAGE, lastPage)
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
            .doOnSubscribe(disposable -> {
                if (pullToRefresh)
                    mRootView.showLoading();//显示下拉刷新的进度条
                else
                    mRootView.startLoadMore();//显示上拉加载更多的进度条
            }).subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(() -> {
                if (pullToRefresh)
                    mRootView.hideLoading();//隐藏下拉刷新的进度条
                else
                    mRootView.endLoadMore();//隐藏上拉加载更多的进度条
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
            .subscribe(new ErrorHandleSubscriber<GankBaseResponse<List<GankItemBean>>>(mErrorHandler) {
                @Override
                public void onNext(GankBaseResponse<List<GankItemBean>> datas) {
                    lastPage = lastPage + 1;
                    if (pullToRefresh) mDatas.clear();//如果是下拉刷新则清空列表
                    preEndIndex = mDatas.size();//更新之前列表总长度,用于确定加载更多的起始位置
                    mDatas.addAll(datas.getResults());
                    if (pullToRefresh)
                        mAdapter.notifyDataSetChanged();
                    else
                        mAdapter.notifyItemRangeInserted(preEndIndex, datas.getResults().size());
                }
            });
}
 
Example #17
Source File: LivePresenter.java    From LQRBiliBlili with MIT License 5 votes vote down vote up
public void loadData(boolean refresh) {
    mModel.getLiveList(refresh)
            .retryWhen(new RetryWithDelay(3, 2))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe(disposable -> {
                mRootView.showLoading();
            })
            .doFinally(() -> {
                mRootView.hideLoading();
            })
            .observeOn(Schedulers.io())
            .map(getAllListDataResult -> {
                GetAllListData.DataBean getAllListData = getAllListDataResult.getData();
                List<LiveMultiItem> data = new ArrayList<>();
                if (getAllListData != null) {
                    // 轮播
                    mBanner = getAllListData.getBanner();
                    // 推荐
                    data.addAll(mModel.parseRecommendData(getAllListData));
                    // 分类数据
                    data.addAll(mModel.parsePartitions(getAllListData));
                }
                return data;
            })
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
            .subscribe(new ErrorHandleSubscriber<List<LiveMultiItem>>(mErrorHandler) {
                @Override
                public void onNext(@NonNull List<LiveMultiItem> liveMultiItems) {
                    setBanner();
                    if (liveMultiItems != null)
                        setAdapter(liveMultiItems);
                }
            });
}
 
Example #18
Source File: VideoDetailPresenter.java    From LQRBiliBlili with MIT License 5 votes vote down vote up
public void loadPlayUrl(String aid) {
    // TODO: 2017/11/1 根据加载情况动态修改提示
    mRootView.setTvVideoStartInfoStr(startInfoStr);
    mModel.getPlayurl(aid)
            .retryWhen(new RetryWithDelay(3, 2))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
            .subscribe(new ErrorHandleSubscriber<PlayUrl>(mErrorHandler) {
                @Override
                public void onNext(PlayUrl playUrl) {
                    mRootView.playVideo(playUrl);
                }
            });
}
 
Example #19
Source File: RxUtils.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
public static <T> ObservableTransformer<T, T> applySchedulers(final IView view) {
    return observable -> observable.subscribeOn(Schedulers.io())
            .doOnSubscribe(disposable -> {
                view.showLoading();//显示进度条
            })
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(() -> {
                view.hideLoading();//隐藏进度条
            }).compose(RxLifecycleUtils.bindToLifecycle(view));
}
 
Example #20
Source File: SearchPresent.java    From Hands-Chopping with Apache License 2.0 5 votes vote down vote up
public void searchFromSanko(String keyword){
        mModel.getSankoSearchGame(keyword)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3,2))
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(()->{
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<GameSearchList>(mErrorHandler){
                    @Override
                    public void onNext(GameSearchList gameSearchList) {
                        gsl.getGameSearchBeanArrayList().addAll(gameSearchList.getGameSearchBeanArrayList());
                        mDatas.clear();
                        mDatas.addAll(gsl.getGameSearchBeanArrayList());
                        mAdapter.notifyDataSetChanged();
//                        int i=0;
//                        for(GameSearchBean bean:gameSearchList.getGameSearchBeanArrayList()){
//                            i++;
//                            Timber.tag("Sanko").w(bean.getId());
//                            Timber.tag("Sanko").w(bean.getImgUrl());
//                            Timber.tag("Sanko").w(bean.getPrice()+" "+i);
//                            Timber.tag("Sanko").w(bean.getTitle());
//                        }
                    }
                });
    }
 
Example #21
Source File: SalePresenter.java    From Hands-Chopping with Apache License 2.0 5 votes vote down vote up
public void requestSteamSaleGameList(){
        mModel.getSteamSaleGame()
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3,2))
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<GameSaleList>(mErrorHandler) {
                    @Override
                    public void onNext(GameSaleList gameSaleList) {
                        gsl.getGameListBeanArrayList().addAll(gameSaleList.getGameListBeanArrayList());
                        mDatas.clear();
                        mDatas.addAll(gsl.getGameListBeanArrayList());
                        mAdapter.notifyDataSetChanged();
                        mRootView.imgLoad();
//                        int i=0;
//                        for(GameListBean bean:gameSaleList.getGameListBeanArrayList()){
//                            i++;
//                            Timber.tag("Steam").w(bean.getId());
//                            Timber.tag("Steam").w(bean.getImgUrl());
//                            Timber.tag("Steam").w(bean.getOff()+" "+i);
//                            Timber.tag("Steam").w(bean.getOldPrice()+" "+i);
//                            Timber.tag("Steam").w(bean.getNowPrice()+" "+i);
//                            Timber.tag("Steam").w(bean.getTitle());
//                        }
                    }
                });
    }
 
Example #22
Source File: GoldHomePresenter.java    From lifecycle-component with Apache License 2.0 5 votes vote down vote up
public void requestDatas(final boolean pullToRefresh) {
    if (pullToRefresh) lastPage = 0;//下拉刷新默认只请求第一页

    mModel.getGoldList(GoldConstants.GOLD_TYPE_ANDROID, GoldConstants.NUMBER_OF_PAGE, lastPage)
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
            .doOnSubscribe(disposable -> {
                if (pullToRefresh)
                    mRootView.showLoading();//显示下拉刷新的进度条
                else
                    mRootView.startLoadMore();//显示上拉加载更多的进度条
            }).subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(() -> {
                if (pullToRefresh)
                    mRootView.hideLoading();//隐藏下拉刷新的进度条
                else
                    mRootView.endLoadMore();//隐藏上拉加载更多的进度条
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
            .subscribe(new ErrorHandleSubscriber<GoldBaseResponse<List<GoldListBean>>>(mErrorHandler) {
                @Override
                public void onNext(GoldBaseResponse<List<GoldListBean>> data) {
                    lastPage = lastPage + 1;
                    if (pullToRefresh) mDatas.clear();//如果是下拉刷新则清空列表
                    preEndIndex = mDatas.size();//更新之前列表总长度,用于确定加载更多的起始位置
                    mDatas.addAll(data.getResults());
                    if (pullToRefresh)
                        mAdapter.notifyDataSetChanged();
                    else
                        mAdapter.notifyItemRangeInserted(preEndIndex, data.getResults().size());
                }
            });
}
 
Example #23
Source File: UserPresenter.java    From MVPArms with Apache License 2.0 4 votes vote down vote up
private void requestFromModel(boolean pullToRefresh) {
    if (pullToRefresh) {
        lastUserId = 1;//下拉刷新默认只请求第一页
    }

    //关于RxCache缓存库的使用请参考 http://www.jianshu.com/p/b58ef6b0624b

    boolean isEvictCache = pullToRefresh;//是否驱逐缓存,为ture即不使用缓存,每次下拉刷新即需要最新数据,则不使用缓存

    if (pullToRefresh && isFirst) {//默认在第一次下拉刷新时使用缓存
        isFirst = false;
        isEvictCache = false;
    }

    mModel.getUsers(lastUserId, isEvictCache)
            .subscribeOn(Schedulers.io())
            .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
            .doOnSubscribe(disposable -> {
                if (pullToRefresh) {
                    mRootView.showLoading();//显示下拉刷新的进度条
                } else {
                    mRootView.startLoadMore();//显示上拉加载更多的进度条
                }
            }).subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally(() -> {
                if (pullToRefresh) {
                    mRootView.hideLoading();//隐藏下拉刷新的进度条
                } else {
                    mRootView.endLoadMore();//隐藏上拉加载更多的进度条
                }
            })
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
            .subscribe(new ErrorHandleSubscriber<List<User>>(mErrorHandler) {
                @Override
                public void onNext(List<User> users) {
                    lastUserId = users.get(users.size() - 1).getId();//记录最后一个id,用于下一次请求
                    if (pullToRefresh) {
                        mUsers.clear();//如果是下拉刷新则清空列表
                    }
                    preEndIndex = mUsers.size();//更新之前列表总长度,用于确定加载更多的起始位置
                    mUsers.addAll(users);
                    if (pullToRefresh) {
                        mAdapter.notifyDataSetChanged();
                    } else {
                        mAdapter.notifyItemRangeInserted(preEndIndex, users.size());
                    }
                }
            });
}
 
Example #24
Source File: RxUtils.java    From Aurora with Apache License 2.0 2 votes vote down vote up
public static <T> LifecycleTransformer<T> bindToLifecycle(IView view) {
    return RxLifecycleUtils.bindToLifecycle(view);

}
 
Example #25
Source File: RxUtils.java    From Hands-Chopping with Apache License 2.0 2 votes vote down vote up
/**
 * 此方法已废弃
 *
 * @param view
 * @param <T>
 * @return
 * @see RxLifecycleUtils 此类可以实现 {@link RxLifecycle} 的所有功能, 使用方法和之前一致
 * @deprecated Use {@link RxLifecycleUtils#bindToLifecycle(IView)} instead
 */
@Deprecated
public static <T> LifecycleTransformer<T> bindToLifecycle(IView view) {
    return RxLifecycleUtils.bindToLifecycle(view);
}
 
Example #26
Source File: RxUtils.java    From TikTok with Apache License 2.0 2 votes vote down vote up
/**
 * 此方法已废弃
 *
 * @param view
 * @param <T>
 * @return
 * @see RxLifecycleUtils 此类可以实现 {@link RxLifecycle} 的所有功能, 使用方法和之前一致
 * @deprecated Use {@link RxLifecycleUtils#bindToLifecycle(IView)} instead
 */
@Deprecated
public static <T> LifecycleTransformer<T> bindToLifecycle(IView view) {
    return RxLifecycleUtils.bindToLifecycle(view);
}
 
Example #27
Source File: RxUtils.java    From MVPArms with Apache License 2.0 2 votes vote down vote up
/**
 * 此方法已废弃
 *
 * @param view
 * @param <T>
 * @return
 * @see RxLifecycleUtils 此类可以实现 {@link RxLifecycle} 的所有功能, 使用方法和之前一致
 * @deprecated Use {@link RxLifecycleUtils#bindToLifecycle(IView)} instead
 */
@Deprecated
public static <T> LifecycleTransformer<T> bindToLifecycle(IView view) {
    return RxLifecycleUtils.bindToLifecycle(view);
}