RecyclerView使用-高级篇(上拉刷新下拉加载更多)
通常RecyclerView使用的时候需要配合上拉刷新下拉加载更多的功能
我们通过开源控件SmartRefreshLayout来实现
SmartRefreshLayout官网:https://github.com/scwang90/SmartRefreshLayout
使用方法如下:
第一步:添加依赖
在gradle文件中添加
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2' implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.2'
第二步:布局文件
在activity_main.xml中SmartRefreshLayout控件将RecyclerView包裹起来
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </androidx.constraintlayout.widget.ConstraintLayout>
第三步:逻辑代码
在MainActivity的onCreate方法中添加下拉刷新和上拉加载更多的功能
下拉刷新 :
RefreshLayout refreshLayout = findViewById(R.id.refreshLayout); refreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败 mNewsList.clear(); for (int i = 0; i < 10; i++) { News news = new News(); news.title = "标题 新内容" + i; news.content = "内容" + i; mNewsList.add(news); } mMyAdapter.notifyDataSetChanged(); } });
上拉加载更多
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(RefreshLayout refreshlayout) { refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败 for (int i = 0; i < 10; i++) { News news = new News(); news.title = "标题 新内容" + i; news.content = "内容" + i; mNewsList.add(news); } mMyAdapter.notifyDataSetChanged(); } });
效果如下:
完整代码
MainActivity.java文件
package com.lucashu.recyclerview; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener; import com.scwang.smartrefresh.layout.listener.OnRefreshListener; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { RecyclerView mRecyclerView; MyAdapter mMyAdapter ; List<News> mNewsList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.recyclerview); // 构造一些数据 for (int i = 0; i < 10; i++) { News news = new News(); news.title = "标题" + i; news.content = "内容" + i; mNewsList.add(news); } mMyAdapter = new MyAdapter(); mRecyclerView.setAdapter(mMyAdapter); LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this); mRecyclerView.setLayoutManager(layoutManager);
RefreshLayout refreshLayout = findViewById(R.id.refreshLayout); refreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败 mNewsList.clear(); for (int i = 0; i < 10; i++) { News news = new News(); news.title = "标题 新内容" + i; news.content = "内容" + i; mNewsList.add(news); } mMyAdapter.notifyDataSetChanged(); } }); refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(RefreshLayout refreshlayout) { refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败 for (int i = 0; i < 10; i++) { News news = new News(); news.title = "标题 新内容" + i; news.content = "内容" + i; mNewsList.add(news); } mMyAdapter.notifyDataSetChanged(); } }); } class MyAdapter extends RecyclerView.Adapter<MyViewHoder> { @NonNull @Override public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(MainActivity.this, R.layout.item_list, null); MyViewHoder myViewHoder = new MyViewHoder(view); return myViewHoder; } @Override public void onBindViewHolder(@NonNull MyViewHoder holder, final int position) { News news = mNewsList.get(position); holder.mTitleTv.setText(news.title); holder.mTitleContent.setText(news.content); holder.mRootView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"点击了:"+ position,Toast.LENGTH_LONG).show(); } }); } @Override public int getItemCount() { return mNewsList.size(); } } class MyViewHoder extends RecyclerView.ViewHolder { TextView mTitleTv; TextView mTitleContent; ConstraintLayout mRootView; public MyViewHoder(@NonNull View itemView) { super(itemView); mTitleTv = itemView.findViewById(R.id.textView); mTitleContent = itemView.findViewById(R.id.textView2); mRootView = itemView.findViewById(R.id.rootview); } } }
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" /> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </androidx.constraintlayout.widget.ConstraintLayout>