Swiperefreshlayout实现下拉加载刷新

3,846 阅读4分钟

1. Demo展示

2. Swiperefreshlayout介绍

  • 首先是依赖包
dependencies {
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
}
  • 官方文档描述

The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child. This view will also be made the target of the gesture and will be forced to match both the width and the height supplied in this layout. The SwipeRefreshLayout does not provide accessibility events; instead, a menu item must be provided to allow refresh of the content wherever this gesture is used.

翻译版本:

当用户可以通过垂直滑动手势刷新视图内容时,就应该使用SwipeRefreshLayout。实例化此视图的活动应该添加一个OnRefreshListener,以便在完成刷新手势时得到通知。SwipeRefreshLayout会在每次手势再次完成时通知监听器;侦听器负责正确地确定何时真正开始刷新其内容。如果侦听器确定不应该进行刷新,则必须调用setRefreshing(false)来取消任何刷新的可视指示。如果一个活动希望只显示进度动画,它应该调用setRefreshing(true)。要禁用手势和进度动画,请在视图上调用setEnabled(false)。

这个布局应该成为视图的父视图,该视图将作为手势的结果刷新,并且只能支持一个直接子视图。该视图也将成为手势的目标,并将被迫匹配此布局中提供的宽度和高度。SwipeRefreshLayout不提供可访问事件;相反,必须提供一个菜单项,以允许在使用此手势的任何地方刷新内容。

  • 重点提取
    • 一般SwipeRefreshLayout用于垂直刷新视图
    • 在实例化视图活动也就是使用刷新功能时应该添加一个OnRefreshListener,以便在完成刷新手势时得到通知
    • 只能支持一个直接子视图,也就是SwipeRefreshLayout只能包裹一个子视图即ListView或者RecycleView等

3. 核心描述

  • isRefreshing()

    返回当前状态是不是刷新状态

  • setColorSchemeResources(int... colorResIds)

    设置下拉进度条的颜色,参数为可变参数int... colorResIds,参数是资源id也就是color里的资源,可以设置不同的颜色,每转一圈就显示一种颜色,按照给的参数顺序显示颜色。

  • setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)

    设置刷新监听,需要重写onRefresh()方法,一般从顶部下拉刷新时会调用此方法,在此方法设置刷新数据具体逻辑以及设置下拉进度条消失等一些工作

  • setRefreshing(boolean refreshing)

4. 代码使用和Demo描述

activity_main.xml

SwipeRefreshLayout里面只包含一个RecycleView,官方文档描述有说明

<?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">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/srl_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView       mRecyclerView;
    private List<Tag>          mTags = new ArrayList<>();
    private DemoAdapter        mAdapter;
    private int count = mTags.size();

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


    /**
     * 初始化视图View
     */
    public void initView() {
        mRefreshLayout = findViewById(R.id.srl_main);
        mRecyclerView = findViewById(R.id.rv_main);

        mAdapter = new DemoAdapter(mTags);

        // 设置layoutManager
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 初始化数据,首先给出5个Tag数据
     */
    public void initData() {
        for (int i = 0; i < 5; i++) {
            Tag tag = new Tag(i + "", "name-" + i);
            mTags.add(tag);
        }

        count = mTags.size();
    }

    /**
     * 初始化事件
     */
    public void initEvent() {
        // 设置下拉监听
        mRefreshLayout.setOnRefreshListener(this);
        // 刷新渐变颜色
        mRefreshLayout.setColorSchemeResources(
                R.color.colorPrimary,
                R.color.colorPrimaryDark,
                R.color.colorAccent
        );

    }

    /**
     * 刷新方法
     */
    @Override
    public void onRefresh() {
        addData();
    }

    /**
     * 真正的刷新逻辑在这里,自定义的,根据情况自己写
     */
    private void addData() {
        // 延迟2s刷新
        mHandler.postDelayed(mRefresh, 2000);
    }

    private Handler mHandler = new Handler();

    private Runnable mRefresh = new Runnable() {
        @Override
        public void run() {
            mRefreshLayout.setRefreshing(false);

            int now = mTags.size()+5;
            for (; count < now; count++) {
                Tag tag = new Tag(count + "", "name-" + count);
                mTags.add(tag);
            }

            Log.d("tags",mTags.toString());
            Log.d("tags",mTags.size()+"");
            mAdapter.notifyDataSetChanged();
        }
    };
}

DemoAdapter

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.ViewHolder> {
    private List<Tag> mTags;

    public DemoAdapter(List<Tag> tags) {
        mTags = tags;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_tag, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tvTagId.setText(mTags.get(position).getTagId());
        holder.tvTagName.setText(mTags.get(position).getTagName());
    }

    @Override
    public int getItemCount() {
        return mTags.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTagId;
        TextView tvTagName;

        private ViewHolder(View view) {
            super(view);
            tvTagId = view.findViewById(R.id.tv_tagid);
            tvTagName = view.findViewById(R.id.tv_tagName);
        }
    }
}

item_tag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:background="@color/colorAccent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_tagid"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_tagName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="20sp" />

</LinearLayout>

Tag实体类

public class Tag {
    private String tagId;
    private String tagName;

    public Tag(String tagId, String tagName) {
        this.tagId = tagId;
        this.tagName = tagName;
    }

    public String getTagId() {
        return tagId;
    }

    public void setTagId(String tagId) {
        this.tagId = tagId;
    }

    public String getTagName() {
        return tagName;
    }

    public void setTagName(String tagName) {
        this.tagName = tagName;
    }

    @Override
    public String toString() {
        return "Tag{" +
                "tagId='" + tagId + '\'' +
                ", tagName='" + tagName + '\'' +
                '}';
    }
}

GitHub

Demo示例的代码在这里哦 SwipeRefreshLayoutDemo