分页加载(记录)

长时间不用老是忘掉记一下,记一下。。。

/**
 * 用户评价 已评价列表
 */
public class AlreadyEvaluatedFragment extends BaseLazyFragment {
    private RecyclerView fragment_already_evaluated_recycler; 
    private SwipeRefreshLayout fragment_already_evaluated_refresh; //下拉刷新

    private List<AlreadyEvaluatedBean.DataBean.ListBean> list = new ArrayList<>();
    private Integer pageSize = 10; //已经加载的条数
    private Integer pageNum = 1; //当前页数
    private int allPageSize; //总条数(后台获取)
    private UserEvaluationActivity activity;
    private RecyclerAlreadyEvaluatedAdapter adapter;

    @Override
    public void initView() {
        fragment_already_evaluated_recycler = view.findViewById(R.id.fragment_already_evaluated_recycler);
        fragment_already_evaluated_refresh = view.findViewById(R.id.fragment_already_evaluated_refresh);
        // 下拉刷新
        fragment_already_evaluated_refresh.setColorSchemeColors(Color.parseColor("#87CEFA"), Color.parseColor("#BA55D3"),
                Color.parseColor("#D2691E"), Color.parseColor("#DC143C"));
        fragment_already_evaluated_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
            //下拉刷新需要把页码与已加载条数都恢复
                pageNum = 1;
                pageSize = 10;
               initData(0);
            }
        });
    }

    @Override
    public View getMyView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activity = (UserEvaluationActivity) getActivity(); // 获取activity
        return inflater.inflate(R.layout.fragment_already_evaluated, container, false);
    }

    @Override
    public void loadData() {
        initData(0);
    }

    private void initData(final int type) {// type = 1:加载更多; type = 0:默认加载
        fragment_already_evaluated_refresh.setRefreshing(true);
        RetrofitUtils.getInstance().getApiServier(IMapService.class)
                .alreadyEvaluated(testToken, pageNum, 10) // 每页10条(每次加载10条)
                .compose(RxScheduler.<AlreadyEvaluatedBean>ObservableToMain())
                .compose(this.<AlreadyEvaluatedBean>bindToLifecycle())
                .subscribe(new BaseObserver<AlreadyEvaluatedBean>() {
                    @Override
                    public void onNext(AlreadyEvaluatedBean alreadyEvaluatedBean) {
                        super.onNext(alreadyEvaluatedBean);
                        fragment_already_evaluated_refresh.setRefreshing(false);
                        if (type ==0){ //只有在默认加载的时候才会清除已经存储的列表,加载更多列表不需要清除已经存储的列表数据
                            if (list != null && list.size() != 0) {
                                list.clear();
                            }
                        }
                        if (alreadyEvaluatedBean.getCode() == 0) {
                            if (alreadyEvaluatedBean.getData() != null && alreadyEvaluatedBean.getData().getList() != null && alreadyEvaluatedBean.getData().getList().size() != 0) {
                                list = alreadyEvaluatedBean.getData().getList();
                                allPageSize = alreadyEvaluatedBean.getData().getTotal(); //获取总条数
                            }
                        } else {
                            ToastUtils.showShort(alreadyEvaluatedBean.getMsg() + alreadyEvaluatedBean.getCode());
                        }
                        if (type == 0) { //默认加载列表
                            initRecycler(); //加载列表
                        }
                        if (type == 1) { //加载更多列表
                            adapter.addData(list);
                            adapter.loadMoreComplete();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        fragment_already_evaluated_refresh.setRefreshing(false);
                        initRecycler(); //加载列表
                        ToastUtils.showShort("请求失败" + e.getMessage());
                    }
                });
    }

    // 初始化列表
    private void initRecycler() {
        adapter = new RecyclerAlreadyEvaluatedAdapter(R.layout.fragment_already_evaluated_recycler_item, list);
        fragment_already_evaluated_recycler.setLayoutManager(new LinearLayoutManager(activity));
        fragment_already_evaluated_recycler.setAdapter(adapter);
        // 上拉加载
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (pageSize < allPageSize) {
                    pageNum++;
                    pageSize += 10;
                    initData(1);
                } else {
                    adapter.loadMoreEnd();
                }
            }
        }, fragment_already_evaluated_recycler);
        // 如果无数据就显示空布局
        if (list == null || list.size() == 0) {
            View view = LayoutInflater.from(activity).inflate(R.layout.recycler_view_empty_view, null);
            adapter.setEmptyView(view);
        }
    }

    // 内部类适配器
    private class RecyclerAlreadyEvaluatedAdapter extends BaseQuickAdapter<AlreadyEvaluatedBean.DataBean.ListBean, BaseViewHolder> {

        RecyclerAlreadyEvaluatedAdapter(int layoutResId, @Nullable List<AlreadyEvaluatedBean.DataBean.ListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, AlreadyEvaluatedBean.DataBean.ListBean item) {
            helper.setText(R.id.fragment_already_evaluation_type, item.getSecondName());
            helper.setText(R.id.fragment_already_evaluation_address, item.getCurrentCity());
            helper.setText(R.id.fragment_already_evaluation_time, item.getCreateTime());
            AppCompatRatingBar ratingBar = helper.itemView.findViewById(R.id.fragment_already_evaluation_attitude_rating_bar);
            ratingBar.setRating((float) item.getEvaluateRank());
        }
    }
}

发布了111 篇原创文章 · 获赞 22 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览