BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载

BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载,第1张

package com.jswjw.CharacterClient.teacher.home.activity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jswjw.CharacterClient.R;
import com.jswjw.CharacterClient.base.SimpleJsonCallBack;
import com.jswjw.CharacterClient.config.Constant;
import com.jswjw.CharacterClient.config.HttpConfig;
import com.jswjw.CharacterClient.teacher.examinedata.adapter.ExamineDataExpandAdapter;
import com.jswjw.CharacterClient.teacher.model.ExamineDataEntity;
import com.jswjw.CharacterClient.util.SPUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TestActivity extends AppCompatActivity {

    @BindView(R.id.recycleView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private ExamineDataExpandAdapter adapter;
    private int pageIndex = 1;
    private int current;
    private int total;
    private boolean isRefresh;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        adapter = new ExamineDataExpandAdapter(null, this);
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        adapter.bindToRecyclerView(recyclerView);
        requestData();

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (current >= total) {
                    adapter.loadMoreEnd();
                } else {
                    pageIndex++;
                    requestData();
                }

            }
        }, recyclerView);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                requestData();
                isRefresh = true;

            }
        });

    }

    public void requestData() {
        OkGo.post(HttpConfig.BASEURL + HttpConfig.TeacherUrl.USERLIST).tag(this)
                .params(Constant.USER_FLOW, SPUtil.getUserFlow())
                .params(Constant.DEPTFLOW, SPUtil.getUserInfo().deptFlow)
                .params(Constant.PAGEINDEX, pageIndex)
                .params(Constant.PAGESIZE, 10)//response.body().doctorList;
                .execute(new SimpleJsonCallBack() {
                    @Override
                    public void onSuccess(Response response) {
                        total = response.body().dataCount;
                        if (isRefresh){
                            adapter.replaceData(response.body().doctorList);
                            isRefresh = false;
                            swipeRefreshLayout.setRefreshing(false);
                        }else {
                            adapter.addData(response.body().doctorList);
                        }

                        current = adapter.getData().size();
                        adapter.loadMoreComplete();
                    }
                });
    }
}



    

        

    



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存