SwipeRefreshLayout

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

使用:

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

点击下载

android互助群:

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

 

 

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大不懂

码字不易,一块也是爱,么么

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

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

打赏作者

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

抵扣说明:

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

余额充值