Android SwipeRefreshLayout完全指南:轻松实现下拉刷新功能

在本教程中,我们将讨论并实现Android的下拉刷新功能。这种Android材料设计用户界面模式在许多应用程序中非常常见,例如Gmail、Facebook、Twitter,并使用Android SwipeRefreshLayout进行实现。

Android SwipeRefreshLayout详解

Android SwipeRefreshLayout是一个ViewGroup,只能容纳一个可滚动的子元素。它可以是ScrollView、ListView或RecyclerView。SwipeRefreshLayout的基本功能是让用户能够手动刷新屏幕内容,这在Facebook的新闻订阅页面中非常常见。在支持库提供此布局之前,开发者不得不创建和检测自定义的下拉手势来刷新ListView等控件。这个类包含一个重要的监听器OnRefreshListener。当用户向下滑动时,会触发这个监听器并调用onRefresh()方法。我们可以根据需要重写这个方法。在本教程中,我们将开发一个应用程序,其中包含一个ListView,当用户向下滑动时,会刷新屏幕并重新排序列表项。

Android SwipeRefreshLayout项目结构

Android SwipeRefreshLayout代码实现

以下是activity_main.xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.Olivia.swipetorefresh.MainActivity">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeToRefresh"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
            </ListView>

        </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

在上面的布局文件中,我们在SwipeRefreshLayout中添加了一个ListView作为其子视图。下面是MainActivity.java类的代码实现:

 
package com.Olivia.swipetorefresh;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ArrayList arrayList = new ArrayList();
    SwipeRefreshLayout mSwipeRefreshLayout;
    ListView mListView;

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

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh);
        mListView = (ListView) findViewById(R.id.listView);

        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);

        arrayList.add("First Element");
        arrayList.add("Second Element");
        arrayList.add("Third Element");
        arrayList.add("Fourth Element");
        arrayList.add("Fifth Element");

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuffle();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    public void shuffle(){
        Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);
    }
}
  • 在上述代码中,我们创建了一个字符串ArrayList并将其添加到ArrayAdapter对象中,然后将该适配器设置到ListView上。
  • 我们添加了一个shuffle方法,每次调用onRefresh()时都会对整个ArrayList进行随机排序。
  • 我们使用了Collections框架的方法,通过将当前时间(毫秒)设置为随机种子来随机打乱ArrayList。
  • setRefreshing(false)是一行重要的代码。它通知SwipeRefreshLayout实例刷新已完成,应该停止刷新加载动画。
  • 默认的刷新动画颜色设置为黑色。我们可以使用setColorSchemeResources()方法来更改它。

以下是应用程序运行时的输出效果。这就结束了本教程。您可以从下面的链接下载Android SwipeRefreshLayout项目,并尝试不同的方法来实现Android屏幕的下拉刷新功能。

下载 Android SwipeRefreshLayout 项目

bannerAds