欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发中SwipeRefreshLayout实现下拉刷新

程序员文章站 2022-03-03 22:10:55
SwipeRefreshLayout是用于实现下拉刷新的核心类,使用很简单只需要使用SwipeRefreshLayout将Recyclerview包裹一下就行。使用:xml:

SwipeRefreshLayout是用于实现下拉刷新的核心类,使用很简单只需要使用SwipeRefreshLayout将Recyclerview包裹一下就行。

Android开发中SwipeRefreshLayout实现下拉刷新

使用:

xml:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/srl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

activity:

srl = findViewById(R.id.srl);
srl.setColorSchemeResources(R.color.colorPrimary);
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        refreshFruits();
    }
});


完整代码:

xml:

<?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.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toobar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_scrollFlags="scroll|enterAlways|snap" />

        </com.google.android.material.appbar.AppBarLayout>
        <!--        &lt;!&ndash;        scroll:RV 向上滚动的时候ToolBar会跟着一起向上滚动并实现隐藏&ndash;&gt;-->
        <!--        &lt;!&ndash;        enterAlways:当RV像下滚动的时候 toolbar会跟着一起向下滑动&ndash;&gt;-->
        <!--        &lt;!&ndash;        snap:当还没有完全显示、隐藏的时候,会根据当前滚动的距离,自动显示隐藏&ndash;&gt;-->
        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/srl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rl"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

activity:

package com.example.myswiperefreshlayout;

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

import android.os.Bundle;

import com.example.myswiperefreshlayout.R;

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

public class MainActivity extends AppCompatActivity {
    private com.example.myswiperefreshlayout.Fruit[] fruits = {new com.example.myswiperefreshlayout.Fruit("Apple", R.drawable.apple), new com.example.myswiperefreshlayout.Fruit("Banana", R.drawable.banana),
            new com.example.myswiperefreshlayout.Fruit("Orange", R.drawable.orange), new com.example.myswiperefreshlayout.Fruit("Watermelon", R.drawable.watermelon),
            new com.example.myswiperefreshlayout.Fruit("Pear", R.drawable.pear), new com.example.myswiperefreshlayout.Fruit("Grape", R.drawable.grape),
            new com.example.myswiperefreshlayout.Fruit("Pineapple", R.drawable.pineapple), new com.example.myswiperefreshlayout.Fruit("Strawberry", R.drawable.strawberry),
            new com.example.myswiperefreshlayout.Fruit("Cherry", R.drawable.cherry), new com.example.myswiperefreshlayout.Fruit("Mango", R.drawable.mango)};
    private List<com.example.myswiperefreshlayout.Fruit> fruitList = new ArrayList<>();
    private SwipeRefreshLayout srl;
    private FruitAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        RecyclerView rl = findViewById(R.id.rl);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        rl.setLayoutManager(layoutManager);
        adapter = new FruitAdapter(fruitList);
        rl.setAdapter(adapter);

        srl = findViewById(R.id.srl);
        srl.setColorSchemeResources(R.color.colorPrimary);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFruits();
            }
        });
    }

    private void initFruits() {
        fruitList.clear();
        for (int i = 0; i < 50; i++) {
            Random random = new Random();
            int index = random.nextInt(fruits.length);
            fruitList.add(fruits[index]);
        }
    }
    private void refreshFruits() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initFruits();
                        adapter.notifyDataSetChanged();
                        srl.setRefreshing(false);//刷新结束隐藏进度条
                    }
                });
            }
        }).start();
    }

}

转发标明出处https://blog.csdn.net/qq_35698774/article/details/108167612

点击下载

感谢:郭霖的《第一行代码 第二版》