AndroidのSwipeRefreshLayoutは、AndroidのPull/Swipe Down to Refreshに対応しています。

このチュートリアルでは、Androidのスワイプダウンによる画面の更新またはプルダウンによる画面の更新について説明し、実装します。このAndroidのマテリアルデザインのUIパターンは、Gmail、Facebook、Twitterなどの多くのアプリケーションでよく見られ、AndroidのSwipeRefreshLayoutを使用して実装されています。

アンドロイドのSwipeRefreshLayout

Android SwipeRefreshLayoutは、スクロール可能な子ビューを1つしか持つことができないViewGroupです。ScrollView、ListView、またはRecyclerViewのいずれかを使用することができます。SwipeRefreshLayoutの基本的な目的は、ユーザーが画面を手動で更新できるようにすることです。これは、Facebookのニュースフィード画面でもよく見られます。サポートライブラリでこのレイアウトが利用可能になる前は、ListViewなどをリフレッシュするためにカスタムのスワイプダウンジェスチャーの作成と検出に頼らなければなりませんでした。このクラスには、重要なリスナーであるOnRefreshListenerが1つ含まれています。スワイプダウンすると、このリスナーがトリガーされ、OnRefresh()メソッドが呼び出されます。このメソッドは、必要に応じてオーバーライドすることができます。このチュートリアルでは、スワイプダウンされると画面が更新され、リストの行がシャッフルされるListViewで構成されるアプリケーションを開発します。

アンドロイドのSwipeRefreshLayoutプロジェクトの構造

アンドロイドの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.scdev.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.scdev.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 *