• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android的RecyclerView下拉/上拉刷新数据

武飞扬头像
裹着心的光m
帮助1

        在Android中的列表视图(我们这里以RecyclerView为例)中有很多数据的时候,往往要采取限制数据条目显示,然后通过刷新再添加新的数据显示,这样看的就会比较美观,那么这种列表视图是怎么实现刷新的呢,我们一起来看看吧。

我们先看看美团的刷新

学新通

美团下拉/上拉都是支持刷新的,我们现在来实现一下吧,go

1.第一步

        我们先创建一个空的Android项目学新通

创建Android项目,Android Studio会有很多模板,我们现在不整那么多花里胡哨的,选择Empty Activity(空的模板),点击next下(下一步)

学新通

Name:这个就不用说了吧,这个是项目的名称

Package name:这个也是有一套标准的,没有域名的话直接用默认的

Save location:这个是项目存储的文件目录,自己定存放目录

Language:开发的语言,选择java,也可以用Kotlin,我用的是java

Minimum SDK:看下面的99.5%,这个没有要求,建议选普及率高的版本,我用的都是5.0

点击finsh就能构建出一个新项目了

学新通

2.第二步,引入第三方框架

        去浏览器,打开GitHub引入jar包,不知道在网址的朋友点这里GitHub - scwang90/SmartRefreshLayout: 🔥下拉刷新、上拉加载、二级刷新、淘宝二楼、RefreshLayout、OverScroll,Android智能下拉刷新框架,支持越界回弹、越界拖动,具有极强的扩展性,集成了几十种炫酷的Header和 Footer。🔥下拉刷新、上拉加载、二级刷新、淘宝二楼、RefreshLayout、OverScroll,Android智能下拉刷新框架,支持越界回弹、越界拖动,具有极强的扩展性,集成了几十种炫酷的Header和 Footer。 - GitHub - scwang90/SmartRefreshLayout: 🔥下拉刷新、上拉加载、二级刷新、淘宝二楼、RefreshLayout、OverScroll,Android智能下拉刷新框架,支持越界回弹、越界拖动,具有极强的扩展性,集成了几十种炫酷的Header和 Footer。学新通https://github.com/scwang90/SmartRefreshLayout或者看这里:

1.找到这个文件,点开,复制一下代码加入对应的地方,Android studio就可以自动下载包了     

  1.  
    implementation 'androidx.appcompat:appcompat:1.0.0' //必须 1.0.0 以上
  2.  
     
  3.  
    implementation 'io.github.scwang90:refresh-layout-kernel:2.0.6' //核心必须依赖
  4.  
    implementation 'io.github.scwang90:refresh-header-classics:2.0.6' //经典刷新头
  5.  
    implementation 'io.github.scwang90:refresh-header-radar:2.0.6' //雷达刷新头
  6.  
    implementation 'io.github.scwang90:refresh-header-falsify:2.0.6' //虚拟刷新头
  7.  
    implementation 'io.github.scwang90:refresh-header-material:2.0.6' //谷歌刷新头
  8.  
    implementation 'io.github.scwang90:refresh-header-two-level:2.0.6' //二级刷新头
  9.  
    implementation 'io.github.scwang90:refresh-footer-ball:2.0.6' //球脉冲加载
  10.  
    implementation 'io.github.scwang90:refresh-footer-classics:2.0.6' //经典加载

    学新通

2.添加混淆文件里面的代码

  1.  
    android.useAndroidX=true
  2.  
    android.enableJetifier=true

学新通

3. 第三步,找到MainActivity的布局文件

        写布局代码

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    xmlns:app="http://schemas.android.com/apk/res-auto"
  4.  
    xmlns:tools="http://schemas.android.com/tools"
  5.  
    android:layout_width="match_parent"
  6.  
    android:layout_height="match_parent"
  7.  
    tools:context=".MainActivity">
  8.  
     
  9.  
    <com.scwang.smart.refresh.layout.SmartRefreshLayout
  10.  
    android:layout_width="match_parent"
  11.  
    android:layout_height="match_parent"
  12.  
    android:id="@ id/smart_refresh_layout">
  13.  
     
  14.  
     
  15.  
    <androidx.recyclerview.widget.RecyclerView
  16.  
    android:layout_width="match_parent"
  17.  
    android:layout_height="match_parent"
  18.  
    android:id="@ id/recycler_view">
  19.  
     
  20.  
     
  21.  
    </androidx.recyclerview.widget.RecyclerView>
  22.  
     
  23.  
    </com.scwang.smart.refresh.layout.SmartRefreshLayout>
  24.  
     
  25.  
    </androidx.constraintlayout.widget.ConstraintLayout>
学新通

先创建一个数据Bean类,DataBean.class

  1.  
    public class DataBean {
  2.  
     
  3.  
    private int image;
  4.  
    private String title;
  5.  
     
  6.  
    public int getImage() {
  7.  
    return image;
  8.  
    }
  9.  
     
  10.  
    public void setImage(int image) {
  11.  
    this.image = image;
  12.  
    }
  13.  
     
  14.  
    public String getTitle() {
  15.  
    return title;
  16.  
    }
  17.  
     
  18.  
    public void setTitle(String title) {
  19.  
    this.title = title;
  20.  
    }
  21.  
    }
学新通

创建一个图片类,DataIcon.class

  1.  
    public class DataIcon {
  2.  
     
  3.  
    public static int[] icon = {
  4.  
    R.mipmap.buqunguishu,
  5.  
    R.mipmap.caidan,
  6.  
    R.mipmap.daitihuo,
  7.  
    R.mipmap.dingdanliebiao,
  8.  
    R.mipmap.fanlitixian,
  9.  
    R.mipmap.fenxianghaibao,
  10.  
    R.mipmap.fuwu,
  11.  
    R.mipmap.hexiaoyuanguanli,
  12.  
    R.mipmap.huoli,
  13.  
    R.mipmap.jifen,
  14.  
    R.mipmap.maijiagouwuche,
  15.  
    R.mipmap.neiyi
  16.  
    };
  17.  
     
  18.  
    }
学新通

在创建适配器所需的item布局文件,item_layout.xml

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:layout_width="match_parent"
  4.  
    android:layout_height="wrap_content"
  5.  
    xmlns:app="http://schemas.android.com/apk/res-auto"
  6.  
    android:layout_margin="5dp">
  7.  
     
  8.  
    <ImageView
  9.  
    android:layout_width="wrap_content"
  10.  
    android:layout_height="wrap_content"
  11.  
    android:id="@ id/item_image"
  12.  
    android:src="@mipmap/ic_launcher"
  13.  
    android:scaleType="fitXY"
  14.  
    app:layout_constraintTop_toTopOf="parent"
  15.  
    app:layout_constraintLeft_toLeftOf="parent"/>
  16.  
     
  17.  
    <TextView
  18.  
    android:layout_width="wrap_content"
  19.  
    android:layout_height="wrap_content"
  20.  
    android:id="@ id/item_text"
  21.  
    android:text="这个是标题"
  22.  
    app:layout_constraintLeft_toRightOf="@id/item_image"
  23.  
    app:layout_constraintTop_toTopOf="parent"
  24.  
    app:layout_constraintBottom_toBottomOf="parent"
  25.  
    android:layout_marginLeft="20dp"/>
  26.  
     
  27.  
     
  28.  
    </androidx.constraintlayout.widget.ConstraintLayout>
学新通

然后创建适配器类,RecyclerViewAdapter.class

  1.  
    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
  2.  
     
  3.  
    private List<DataBean> data;
  4.  
     
  5.  
    //有参构造器,给外部传数据进来适配器
  6.  
    public RecyclerViewAdapter(List<DataBean> data) {
  7.  
    this.data = data;
  8.  
    }
  9.  
     
  10.  
    //创建视图
  11.  
    @Override
  12.  
    public RecyclerViewAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  13.  
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
  14.  
    return new RecyclerViewHolder(v);
  15.  
    }
  16.  
     
  17.  
    //赋值操作
  18.  
    @Override
  19.  
    public void onBindViewHolder(RecyclerViewAdapter.RecyclerViewHolder holder, int position) {
  20.  
     
  21.  
    //给控件赋值
  22.  
    holder.imageView.setImageResource(data.get(position).getImage());
  23.  
    holder.textView.setText(data.get(position).getTitle());
  24.  
     
  25.  
    }
  26.  
     
  27.  
    //条目的数量
  28.  
    @Override
  29.  
    public int getItemCount() {
  30.  
    return data.size();
  31.  
    }
  32.  
     
  33.  
    //声明控件,绑定控件
  34.  
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
  35.  
     
  36.  
    ImageView imageView;
  37.  
    TextView textView;
  38.  
     
  39.  
    public RecyclerViewHolder(View v) {
  40.  
    super(v);
  41.  
    imageView = v.findViewById(R.id.item_image);
  42.  
    textView = v.findViewById(R.id.item_text);
  43.  
    }
  44.  
    }
  45.  
    }
学新通

最后写上MainActivity.class的代码

  1.  
    public class MainActivity extends AppCompatActivity {
  2.  
     
  3.  
    private SmartRefreshLayout smartRefreshLayout;
  4.  
    private RecyclerView recyclerView;
  5.  
     
  6.  
    private List<DataBean> data;
  7.  
    private RecyclerViewAdapter adapter;
  8.  
     
  9.  
    @Override
  10.  
    protected void onCreate(Bundle savedInstanceState) {
  11.  
    super.onCreate(savedInstanceState);
  12.  
    setContentView(R.layout.activity_main);
  13.  
     
  14.  
    data = new ArrayList<>();
  15.  
     
  16.  
    //初始化数据,真是开发是后台返回的数据,我们这里用假数据,自己创建
  17.  
    setData();
  18.  
     
  19.  
    //初始化视图
  20.  
    initView();
  21.  
     
  22.  
    //设置RecyclerView的适配器
  23.  
    setAdapter(true, false);
  24.  
     
  25.  
    //设置刷新逻辑
  26.  
    setRefresh();
  27.  
     
  28.  
    }
  29.  
     
  30.  
    /*
  31.  
    * 设置刷新的方法
  32.  
    * */
  33.  
    private void setRefresh() {
  34.  
     
  35.  
    //设置头部刷新的样式
  36.  
    smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(this));
  37.  
    //设置页脚刷新的样式
  38.  
    smartRefreshLayout.setRefreshFooter(new BallPulseFooter(this));
  39.  
    //设置头部刷新时间监听
  40.  
    smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
  41.  
    @Override
  42.  
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
  43.  
     
  44.  
    smartRefreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
  45.  
    //添加一条新数据,再最开头的位置
  46.  
    addData();
  47.  
    Toast.makeText(MainActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
  48.  
    }
  49.  
    });
  50.  
    smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
  51.  
    @Override
  52.  
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
  53.  
     
  54.  
    smartRefreshLayout.finishLoadMore(2000);
  55.  
    //添加一条新数据,再最后的位置
  56.  
    addDatas();
  57.  
    Toast.makeText(MainActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
  58.  
    }
  59.  
    });
  60.  
     
  61.  
    }
  62.  
     
  63.  
    /*
  64.  
    * 添加一条新的数据
  65.  
    * */
  66.  
    private void addData() {
  67.  
     
  68.  
    DataBean dataBean = new DataBean();
  69.  
    dataBean.setImage(R.mipmap.baobao);
  70.  
    dataBean.setTitle("这是新添加的数据哦");
  71.  
    data.add(0,dataBean);
  72.  
    adapter.notifyDataSetChanged();
  73.  
     
  74.  
    }
  75.  
    /*
  76.  
    * 在最底添加一条新的数据
  77.  
    * */
  78.  
    private void addDatas() {
  79.  
    DataBean dataBean = new DataBean();
  80.  
    dataBean.setImage(R.mipmap.baobao);
  81.  
    dataBean.setTitle("这是新添加的数据哦");
  82.  
    data.add(data.size(),dataBean);
  83.  
    adapter.notifyDataSetChanged();
  84.  
     
  85.  
    }
  86.  
     
  87.  
     
  88.  
    /*
  89.  
    * 初始化数据,自己创建数据
  90.  
    * */
  91.  
    private void setData() {
  92.  
     
  93.  
    for (int i = 0; i < DataIcon.icon.length; i ) {
  94.  
    DataBean dataBean = new DataBean();
  95.  
    dataBean.setImage(DataIcon.icon[i]);
  96.  
    dataBean.setTitle("这是第" (i 1) "张图");
  97.  
    data.add(dataBean);
  98.  
    }
  99.  
     
  100.  
     
  101.  
    }
  102.  
     
  103.  
    /**
  104.  
    * 参数1:设置布局是否垂直 true:垂直 false:水平
  105.  
    * 参数2:设置布局的方向 true:反向 false:正常
  106.  
    * */
  107.  
    private void setAdapter(boolean isVertical, boolean isReverse) {
  108.  
     
  109.  
    //创建一个适配器对象,传一个集合进去给适配器
  110.  
    adapter = new RecyclerViewAdapter(data);
  111.  
    //设置垂直还是水平,三元表达式
  112.  
    LinearLayoutManager manager = new LinearLayoutManager(this);
  113.  
    manager.setOrientation(isVertical? RecyclerView.VERTICAL:RecyclerView.HORIZONTAL);
  114.  
     
  115.  
    //设置方向
  116.  
    manager.setReverseLayout(false);
  117.  
     
  118.  
    //把设置好的方向和排版给RecyclerView
  119.  
    recyclerView.setLayoutManager(manager);
  120.  
     
  121.  
    //设置适配器器
  122.  
    recyclerView.setAdapter(adapter);
  123.  
     
  124.  
     
  125.  
    }
  126.  
     
  127.  
    private void initView() {
  128.  
     
  129.  
    //声明绑定控件
  130.  
    smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
  131.  
    recyclerView = findViewById(R.id.recycler_view);
  132.  
     
  133.  
    }
  134.  
    }
学新通

运行代码展示

学新通

 实在不行的可以在gitee拉源码哦,地址在这:https://gitee.com/li-jier/csdn_recycler-view.git

觉得有帮助的朋友,给小弟点个赞哦,谢谢~

祝大家在编码的路上一路风雨无阻,加油~

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcahcc
系列文章
更多 icon
同类精品
更多 icon
继续加载