Android RecyclerView 分页加载

 1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/srl_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="10dp"
        app:srlAccentColor="#00000000"
        app:srlEnablePreviewInEditMode="true"
        app:srlPrimaryColor="#00000000"
        tools:ignore="MissingClass">

        <com.scwang.smartrefresh.layout.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</RelativeLayout>

2.代码

public class TestFragment extends LazyLoadFragment {

    private ListAdapter listAdapter;
    private SmartRefreshLayout srl_id;
    private RecyclerView rv_id;
    private List<String> 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<ListAdapter.Myvh> {

    Context context;
    List<String> list;

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

    public void setList(List<String> 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);
        }
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值