android中paging用法

 public void initPaging(Fragment fragment) {
        PagedList.Config config = new PagedList.Config.Builder()
                .setPageSize(10)    //每页显示的词条数
                .setEnablePlaceholders(false)
                .setInitialLoadSizeHint(10) //首次加载的数据量
                .setPrefetchDistance(5)     //距离底部还有多少条数据时开始预加载
                .build();

        pageData = new LivePagedListBuilder(factory, config)
                .build();
        pageData.observe(fragment, pagedList -> {
            mView.loadSuccess(pagedList);
        });
    }
   DataSource.Factory factory = new DataSource.Factory() {
        @NonNull
        @Override
        public DataSource create() {
            if (dataSource == null || dataSource.isInvalid()) {
                dataSource = new DataSource();
            }
            return dataSource;
        }
    };
    class DataSource extends PositionalDataSource {


        @Override
        public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {


            loadData(1, 10, callback, null);
        }


        @Override
        public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback callback) {

            int page = 0;
            if (params.startPosition < 10) {
                page = params.startPosition + 1;
            } else {
                page = params.startPosition / 10 + 1;
            }
            loadData(page, 10, null, callback);
        }


    }
  private void loadData(int page, int count,
                          PositionalDataSource.LoadInitialCallback callback1,
                          PositionalDataSource.LoadRangeCallback callback2) {
        Map paramMap = new HashMap<>();


        paramMap.put("page", String.valueOf(page));
        paramMap.put("rows", String.valueOf(count));


        mModel.list(paramMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mView))
                .subscribe(new ErrorHandleSubscriber>(context) {
                    @Override
                    public void onNext(CommonListSubject subject) {
                        if (callback1 != null) {

                            if (subject.resultSize == 0) {
                                mView.empty();
                                mView.noMoreData();
                            } else {
                                callback1.onResult(subject.rows, 0);
                            }
                        }
                        if (callback2 != null) {
                            callback2.onResult(subject.rows);
                            if (subject.resultSize == 0) {
                                mView.noMoreData();
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.error(e);

                    }
                });


    }

 

你可能感兴趣的:(android中paging用法)