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

        在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就可以自动下载包了     

     implementation 'androidx.appcompat:appcompat:1.0.0'                 //必须 1.0.0 以上

    implementation  'io.github.scwang90:refresh-layout-kernel:2.0.6'      //核心必须依赖
    implementation  'io.github.scwang90:refresh-header-classics:2.0.6'    //经典刷新头
    implementation  'io.github.scwang90:refresh-header-radar:2.0.6'       //雷达刷新头
    implementation  'io.github.scwang90:refresh-header-falsify:2.0.6'     //虚拟刷新头
    implementation  'io.github.scwang90:refresh-header-material:2.0.6'    //谷歌刷新头
    implementation  'io.github.scwang90:refresh-header-two-level:2.0.6'   //二级刷新头
    implementation  'io.github.scwang90:refresh-footer-ball:2.0.6'        //球脉冲加载
    implementation  'io.github.scwang90:refresh-footer-classics:2.0.6'    //经典加载

    

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

android.useAndroidX=true
android.enableJetifier=true

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

        写布局代码

<?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.smart.refresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/smart_refresh_layout">


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


        </androidx.recyclerview.widget.RecyclerView>

    </com.scwang.smart.refresh.layout.SmartRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

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

public class DataBean {
    
    private int image;
    private String title;

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

创建一个图片类,DataIcon.class

public class DataIcon {

    public static int[] icon = {
            R.mipmap.buqunguishu,
            R.mipmap.caidan,
            R.mipmap.daitihuo,
            R.mipmap.dingdanliebiao,
            R.mipmap.fanlitixian,
            R.mipmap.fenxianghaibao,
            R.mipmap.fuwu,
            R.mipmap.hexiaoyuanguanli,
            R.mipmap.huoli,
            R.mipmap.jifen,
            R.mipmap.maijiagouwuche,
            R.mipmap.neiyi
    };

}

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

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_image"
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_text"
        android:text="这个是标题"
        app:layout_constraintLeft_toRightOf="@id/item_image"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginLeft="20dp"/>
    

</androidx.constraintlayout.widget.ConstraintLayout>

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

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

    private List<DataBean> data;

    //有参构造器,给外部传数据进来适配器
    public RecyclerViewAdapter(List<DataBean> data) {
        this.data = data;
    }

    //创建视图
    @Override
    public RecyclerViewAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new RecyclerViewHolder(v);
    }

    //赋值操作
    @Override
    public void onBindViewHolder(RecyclerViewAdapter.RecyclerViewHolder holder, int position) {

        //给控件赋值
        holder.imageView.setImageResource(data.get(position).getImage());
        holder.textView.setText(data.get(position).getTitle());

    }

    //条目的数量
    @Override
    public int getItemCount() {
        return data.size();
    }

    //声明控件,绑定控件
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

        public RecyclerViewHolder(View v) {
            super(v);
            imageView = v.findViewById(R.id.item_image);
            textView = v.findViewById(R.id.item_text);
        }
    }
}

最后写上MainActivity.class的代码

public class MainActivity extends AppCompatActivity {

    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView recyclerView;

    private List<DataBean> data;
    private RecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data = new ArrayList<>();

        //初始化数据,真是开发是后台返回的数据,我们这里用假数据,自己创建
        setData();

        //初始化视图
        initView();

        //设置RecyclerView的适配器
        setAdapter(true, false);

        //设置刷新逻辑
        setRefresh();

    }

    /*
    * 设置刷新的方法
    * */
    private void setRefresh() {

        //设置头部刷新的样式
        smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(this));
        //设置页脚刷新的样式
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(this));
        //设置头部刷新时间监听
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                smartRefreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                //添加一条新数据,再最开头的位置
                addData();
                Toast.makeText(MainActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                smartRefreshLayout.finishLoadMore(2000);
                //添加一条新数据,再最后的位置
                addDatas();
                Toast.makeText(MainActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
            }
        });

    }

    /*
    * 添加一条新的数据
    * */
    private void addData() {

        DataBean dataBean = new DataBean();
        dataBean.setImage(R.mipmap.baobao);
        dataBean.setTitle("这是新添加的数据哦");
        data.add(0,dataBean);
        adapter.notifyDataSetChanged();

    }
    /*
     * 在最底添加一条新的数据
     * */
    private void addDatas() {
        DataBean dataBean = new DataBean();
        dataBean.setImage(R.mipmap.baobao);
        dataBean.setTitle("这是新添加的数据哦");
        data.add(data.size(),dataBean);
        adapter.notifyDataSetChanged();

    }


    /*
    * 初始化数据,自己创建数据
    * */
    private void setData() {

        for (int i = 0; i < DataIcon.icon.length; i++) {
            DataBean dataBean = new DataBean();
            dataBean.setImage(DataIcon.icon[i]);
            dataBean.setTitle("这是第"+ (i + 1) + "张图");
            data.add(dataBean);
        }


    }

    /**
     * 参数1:设置布局是否垂直 true:垂直 false:水平
     * 参数2:设置布局的方向 true:反向 false:正常
     * */
    private void setAdapter(boolean isVertical, boolean isReverse) {

        //创建一个适配器对象,传一个集合进去给适配器
        adapter = new RecyclerViewAdapter(data);
        //设置垂直还是水平,三元表达式
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(isVertical? RecyclerView.VERTICAL:RecyclerView.HORIZONTAL);

        //设置方向
        manager.setReverseLayout(false);

        //把设置好的方向和排版给RecyclerView
        recyclerView.setLayoutManager(manager);

        //设置适配器器
        recyclerView.setAdapter(adapter);


    }

    private void initView() {

        //声明绑定控件
        smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        recyclerView = findViewById(R.id.recycler_view);

    }
}

运行代码展示

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

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

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

  • 18
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: 下拉刷新是一种常见的UI交互方式,可以在Android Studio使用。下拉刷新可以让用户在列表或网格视图下拉刷新数据。在Android Studio,可以使用SwipeRefreshLayout控件来实现下拉刷新功能。该控件可以包含一个子视图,例如RecyclerView或ListView,以显示数据。当用户下拉SwipeRefreshLayout时,可以触发onRefresh()回调方法来更新数据。要使用SwipeRefreshLayout,请在布局文件添加该控件,并在Java代码设置onRefresh()回调方法。 ### 回答2: Android Studio下拉刷新Android开发非常常见的功能之一,即在应用一个View,用户可以通过下拉View来刷新数据或者重新加载数据。通常情况下,我们在使用RecyclerView或ListView来展示数据时,我们需要实现下拉刷新的功能。在Android Studio下,实现下拉刷新功能需要用到SwipeRefreshLayout控件。 在Android Studio实现下拉刷新的步骤如下: 1. 在布局文件添加SwipeRefreshLayout控件 例如: ``` <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!--您的RecyclerView或ListView--> <RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> ``` 2. 在Activity或者Fragment找到SwipeRefreshLayout控件并设置监听器 例如: ``` val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipe_refresh_layout) swipeRefreshLayout.setOnRefreshListener { //执行数据刷新操作 //例如请求网络数据等 } ``` 3. 在数据刷新完毕后调用SwipeRefreshLayout.setRefreshing(false)方法结束刷新动画。 例如: ``` swipeRefreshLayout.setRefreshing(false) ``` 以上便是在Android Studio下实现下拉刷新的基本步骤,当然还可以自定义下拉刷新的样式等等。SwipeRefreshLayout控件非常方便、易用,一般情况下,只需要创建该组件,然后添加在需要下拉刷新的控件上,再设置一个下拉刷新监听器,便可以实现下拉刷新的功能了。 ### 回答3: 下拉刷新是常用的一种用户界面交互设计,它可以使应用程序在用户划动屏幕的时候更新数据。在Android开发,通过使用第三方的下拉刷新库或者自定义实现下拉刷新都是非常常见的方式之一。而在Android Studio,也有自带的下拉刷新控件可以轻松的集成到应用。 首先,我们需要在布局文件添加下拉刷新控件。这可以通过添加一个“androidx.swiperefreshlayout.widget.SwipeRefreshLayout”标签来实现,我们也可以在该标签添加一个子视图以显示下拉刷新的内容。 接下来,在代码,我们可以通过调用SwipeRefreshLayout的方法设置刷新动作的监听器,以便处理下拉刷新时需要执行的操作。例如,我们可以在监听器的onRefresh()方法更新数据,并在完成后调用SwipeRefreshLayout的setRefreshing(false)方法以停止刷新动画。 除此之外,我们也可以使用自定义的下拉刷新控件来实现更为个性化的效果。这可以通过继承SwipeRefreshLayout来实现,我们可以自定义刷新动画的样式,颜色以及刷新时的处理逻辑。同时,自定义控件也提供了更高的灵活性,包括为下拉刷新添加自定义的状态显示等。 总之,下拉刷新Android应用程序非常常用的设计元素之一。我们可以通过设置SwipeRefreshLayout的监听器,或者继承它并进行自定义来实现下拉刷新的功能。这个功能也在Android Studio得到了很好的集成和支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FireDuration

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值