一手遮天 Android - view(RecyclerView): RecyclerView 下拉刷新(结合 SwipeRefreshLayout 控件实现)

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(RecyclerView): RecyclerView 下拉刷新(结合 SwipeRefreshLayout 控件实现)

示例如下:

/view/recyclerview/RecyclerViewDemo3.java

/**
 * 演示 RecyclerView 的下拉刷新,结合 SwipeRefreshLayout 控件实现
 *
 *
 * 引入 SwipeRefreshLayout 控件的话,需要在 app 的 build.gradle 中配置好 implementation "androidx.swiperefreshlayout:swiperefreshlayout:x.x.x"
 */

package com.webabcd.androiddemo.view.recyclerview;

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

import android.os.Bundle;
import android.os.Handler;

import com.webabcd.androiddemo.R;

public class RecyclerViewDemo3 extends AppCompatActivity {

    private RecyclerView _recyclerView;
    private SwipeRefreshLayout _swipeRefreshLayout1;


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

        _recyclerView = findViewById(R.id.recyclerView1);
        _swipeRefreshLayout1 = findViewById(R.id.swipeRefreshLayout1);

        sample();
    }

    private void sample() {

        _recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewDemo3.this));
        _recyclerView.setAdapter(new MyRecyclerViewAdapter(MyData.generateDataList()));

        // 下拉刷新提示动画的前景色
        _swipeRefreshLayout1.setColorSchemeResources(R.color.white);
        // 下拉刷新提示动画的背景色
        _swipeRefreshLayout1.setProgressBackgroundColorSchemeResource(R.color.orange);
        // 监听下拉刷新事件
        _swipeRefreshLayout1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 隐藏下拉刷新提示动画
                        _swipeRefreshLayout1.setRefreshing(false);

                        // 更新 RecyclerView 的数据
                        ((MyRecyclerViewAdapter)_recyclerView.getAdapter()).updateData(MyData.generateDataList());
                        _recyclerView.getAdapter().notifyDataSetChanged();
                    }
                },2000);
            }
        });
    }
}

/layout/activity_view_recyclerview_recyclerviewdemo3.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="match_parent"
    android:orientation="vertical">

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

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

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-06-02 08:28  webabcd  阅读(57)  评论(0编辑  收藏  举报