Android的SwipeRefreshLayout – Android下拉/滑动刷新

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

安卓手机上的SwipeRefreshLayout

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

安卓 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);
    }
}
  • In the above code we’ve created an ArrayList of strings and add it to the ArrayAdapter object that’s later set onto the ListView.
  • We’ve added a shuffle method that shuffles the whole ArrayList every time the onRefresh() is called.
  • We’ve used the Collections framework method to randomly shuffle the ArrayList by setting a random seed as the current time in milli seconds.
  • setRefreshing(false) is an important line of code. It notifies the SwipeRefreshLayout instance that the refreshing is completed and it should stop the refreshing loader animation.
  • The default refreshing animation color is set to black. We can change it using the method setColorSchemeResources()

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

下载 Android SwipeRefreshLayout 项目

发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds