Android SwipeRefreshLayout – Android下拉/向下滑动以刷新
在本教程中,我们将讨论并实现Android"向下滑动以刷新"或者" Android拉动以刷新屏幕"。
这种Android Material Design用户界面模式在Gmail,Facebook,Twitter等许多应用程序中非常常见,并使用Android SwipeRefreshLayout实现。
Android SwipeRefreshLayout
Android SwipeRefreshLayout是一个ViewGroup,只能容纳一个可滚动的子级。
它可以是ScrollView,ListView或者RecyclerView。
SwipeRefreshLayout的基本需求是允许用户手动刷新屏幕。
这在Facebook Newsfeed屏幕中很常见。
在支持库中无法使用此布局之前,我们不得不依靠创建和检测自定义向下滑动手势来刷新(例如ListView)。
此类包含一个重要的侦听器,即OnRefreshListener。
向下滑动时,将触发此侦听器,并调用OnRefresh()方法。
我们可以根据需要重写此方法。
在本教程中,我们将开发一个包含ListView的应用程序,该应用程序在向下滑动时将刷新屏幕并刷新列表行。
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.theitroad.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.theitroad.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()方法更改它

