接下来实现刷新效果

1.添加控件SwipeRefreshLayout

如果找不到该控件,请在build.gradle中添加

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'

如果报错,请在File—>Project Structure—>app那里点加号选择Library Dependency

搜索swiperefreshlayout,选择anroidx开头的GroupID即可

android recycleView上拉加载 recyclerview下拉刷新控件_移动开发

添加SwipeRefreshLayout并且创建id
res/layout下的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
    
       <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>

2.在MainActivity中实现刷新操作

首先找到刷新控件
在MainActivity中创建成员变量

private SwipeRefreshLayout mswipeRefreshLayout;

在OnCreate()中绑定刷新控件,并执行刷新方法

mswipeRefreshLayout = findViewById(R.id.refresh_layout);
handlerDownPullUpdate();

创建刷新方法,完成刷新操作

private void handlerDownPullUpdate() {
		//设置刷新的转圈的颜色
        mswipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary);
        //设置可用
        mswipeRefreshLayout.setEnabled(true);
        //刷新操作监听
        mswipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //在这里面执行刷新数据的操作
                /**
                 * 当我们在顶部下拉的时候,这个方法就会被执行
                 *  但是这个方法是MainThread是主线程,不可以执行耗时操作
                 *  一般来说,我们请求数据要开一个线程去获取
                 *  //这里面演示,直接添加数据
                 */

                ItemBean data = new ItemBean();
                data.title = "this is a sample which is newly added";
                data.icon = R.mipmap.sample_1;
                mdata.add(0,data);
                //更新UI,我们假装卡3秒再添加数据
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //这里做两件事,1.让刷新停止 2.更新列表
                        listViewAdapter.notifyDataSetChanged();
                        mswipeRefreshLayout.setRefreshing(false);
                    }
                },3000);
            }
        });
    }

效果图

android recycleView上拉加载 recyclerview下拉刷新控件_移动开发_02


可以看到,每下拉刷新一次,就有一个新的图片添加,且刷新的小圆圈的颜色就是我们设置的颜色

完整代码

MainActivity.java

package com.example.simplerecycler;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import adapter.ListViewAdapter;
import entity.ItemBean;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<ItemBean> mdata;
    public static int[] icons = {
            R.mipmap.sample_0,
            R.mipmap.sample_1,
            R.mipmap.sample_2,
            R.mipmap.sample_3,
            R.mipmap.sample_4,
            R.mipmap.sample_5,
            R.mipmap.sample_6,
            R.mipmap.sample_7,
    };    //存放图片资源的id
    private SwipeRefreshLayout mswipeRefreshLayout;
    private ListViewAdapter listViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到控件
        recyclerView = findViewById(R.id.recycler_view);
        //准备数据
        initData();

        listViewAdapter = new ListViewAdapter(mdata);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);


        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);

        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3,GridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        recyclerView.setAdapter(listViewAdapter);

        mswipeRefreshLayout = findViewById(R.id.refresh_layout);


        handlerDownPullUpdate();
    }

    private void handlerDownPullUpdate() {
        mswipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary);
        mswipeRefreshLayout.setEnabled(true);
        mswipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //在这里面执行刷新数据的操作
                /**
                 * 当我们在顶部下拉的时候,这个方法就会被执行
                 *  但是这个方法是MainThread是主线程,不可以执行耗时操作
                 *  一般来说,我们请求数据要开一个线程去获取
                 *  //这里面演示,直接添加数据
                 */

                ItemBean data = new ItemBean();
                data.title = "this is a sample which is newly added";
                data.icon = R.mipmap.sample_1;
                mdata.add(0,data);
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //这里做两件事,1.让刷新停止 2.更新列表
                        listViewAdapter.notifyDataSetChanged();
                        mswipeRefreshLayout.setRefreshing(false);
                    }
                },3000);
            }
        });
    }

    private void initData() {
        //创建集合
        mdata = new ArrayList<>();
        //创建数据
        for (int i = 0; i < MainActivity.icons.length; i++) {
            //创建数据对象
            ItemBean data = new ItemBean();
            data.icon = MainActivity.icons[i];
            data.title = "第" + i + "条目";
            mdata.add(data);
        }
    }
}

ItemBean.java

package entity;

public class ItemBean {
    public int icon;
    public String title;
}

ListViewAdapter

public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.InnerHolder> {

    private List<ItemBean> mData;

    public ListViewAdapter(List<ItemBean> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(),R.layout.item_list_view,null);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        if(mData != null){
            return mData.size();
        }
        return 0;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        private ImageView icon;
        private TextView title;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            title = itemView.findViewById(R.id.title);
        }

        public void setData(ItemBean itemBean) {
            icon.setImageResource(itemBean.icon);
            title.setText(itemBean.title);
        }
    }
}

接下来是布局文件
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


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

</RelativeLayout>

item_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardUseCompatPadding="true"
        android:background="#fff">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/icon"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/sample_0"
                android:scaleType="fitXY"/>
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是文本"
                android:layout_toRightOf="@+id/icon"
                android:textSize="25dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"/>
        </RelativeLayout>
    </androidx.cardview.widget.CardView>


</RelativeLayout>

如果up主看我不爽,请联系我删除。。。。