Android RecyclerView 分页加载

Android RecyclerView 分页加载,第1张

 1.布局文件



    

        

        

        

    

2.代码

public class TestFragment extends LazyLoadFragment {

    private ListAdapter listAdapter;
    private SmartRefreshLayout srl_id;
    private RecyclerView rv_id;
    private List list;
    private int page = 1;
    private int count = 10;

   
    @Override
    protected int setContentView() {
        return R.layout.fragment_test;
    }


    @Override
    protected void lazyLoad() {

        srl_id = findViewById(R.id.srl_id);
        rv_id = findViewById(R.id.rv_id);
        rv_id.setLayoutManager(new LinearLayoutManager(getActivity()));

        list = new ArrayList<>();
        for (int i = 1; i <= count ; i++){
            list.add(i+1+"");
        }

        listAdapter = new ListAdapter(getActivity());
        rv_id.setAdapter(listAdapter);
        listAdapter.setList(list);

        //刷新
        srl_id.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                list.clear();
                for (int i = 1; i <= count ; i++){
                    list.add(i+1+"");
                }
            }
        });

        //加载
        srl_id.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(1000);

                count =+ 10;
                page++;

                for (int i = 1; i <= count ; i++){
                    list.add(i+1+"");
                }

                listAdapter.notifyDataSetChanged();
            }
        });
    }
}

3.adapter

public class ListAdapter extends RecyclerView.Adapter {

    Context context;
    List list;

    public ListAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_list, null);
        return new Myvh(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull Myvh holder, int position) {
        holder.item_tv.setText(position+1+"");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Myvh extends RecyclerView.ViewHolder {

        private final TextView item_tv;

        public Myvh(@NonNull View itemView) {
            super(itemView);
            item_tv = itemView.findViewById(R.id.item_tv);
        }
    }
}

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存