更新リクエストに応答する

Compose の方法を試す
Jetpack Compose は、Android に推奨される UI ツールキットです。Compose でプルして更新する方法について学びます。

このドキュメントでは、ユーザーが手動更新をリクエストしてきたときに、アプリを更新する方法について説明します。ユーザーがスワイプ ジェスチャーを使用して更新をトリガーするケースと、アクションバーの更新アクションを使用して更新をトリガーするケースがあります。

更新操作に応答する

ユーザーがスワイプで更新のジェスチャーを実行すると、システムは進行状況インジケーターを表示し、アプリのコールバック メソッドを呼び出します。コールバック メソッドがアプリのデータを更新します。

アプリ内で更新ジェスチャーに応答するには、SwipeRefreshLayout.OnRefreshListener インターフェースとその onRefresh() メソッドを実装します。onRefresh() メソッドは、ユーザーがスワイプ ジェスチャーを行うと呼び出されます。

実際の更新処理用のコードは別個のメソッド(ViewModel が望ましい)に配置し、その更新メソッドを onRefresh() 実装から呼び出します。これにより、ユーザーがアクションバーから更新をトリガーしたときにも、同じ更新メソッドを使用して更新処理を実行することができます。

更新メソッドでは、データの更新が完了したら setRefreshing(false) を呼び出します。このメソッドを呼び出すと、SwipeRefreshLayout に対して、進行状況インジケーターを削除し、ビュー コンテンツを更新するよう指示します。

たとえば、次のコードは onRefresh() を実装し、myUpdateOperation() メソッドを呼び出して ListView によって表示されるデータを更新します。

Kotlin

// Sets up a SwipeRefreshLayout.OnRefreshListener that invokes when
// the user performs a swipe-to-refresh gesture.

mySwipeRefreshLayout.setOnRefreshListener {
    Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout")

    // This method performs the actual data-refresh operation and calls
    // setRefreshing(false) when it finishes.
    myUpdateOperation()
}

Java

// Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when
// the user performs a swipe-to-refresh gesture.

mySwipeRefreshLayout.setOnRefreshListener(() -> {
    Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");

    // This method performs the actual data-refresh operation and calls
    // setRefreshing(false) when it finishes.
    myUpdateOperation();
  }
);

更新アクションに応答する

ユーザーがアクションバーを使用して更新をリクエストした場合、システムは、onOptionsItemSelected() メソッドを呼び出します。アプリはこの呼び出しに応答して、進行状況インジケーターを表示し、アプリのデータを更新します。

更新アクションに応答するには、onOptionsItemSelected() をオーバーライドします。このオーバーライド メソッド内で、true 値を指定して setRefreshing() を呼び出すことで、SwipeRefreshLayout 進行状況インジケーターをトリガーし、更新処理を実行します。実際の更新処理は、別個のメソッド内で行うようにします。これにより、ユーザーがスワイプを使用して更新をトリガーした場合でも、アクションバーを使用して更新をトリガーした場合でも、同じメソッドを呼び出すことができます。更新が完了したら、setRefreshing(false) を呼び出して更新の進行状況インジケーターを削除します。

更新のリクエスト アクションに応答する方法を次のサンプルコードに示します。

Kotlin

// Listen for option item selections to receive a notification when the user
// requests a refresh by selecting the refresh action bar item.

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {

        // Check whether the user triggers a refresh:
        R.id.menu_refresh -> {
            Log.i(LOG_TAG, "Refresh menu item selected")

            // Signal SwipeRefreshLayout to start the progress indicator.
            mySwipeRefreshLayout.isRefreshing = true

            // Start the refresh background task. This method calls
            // setRefreshing(false) when it finishes.
            myUpdateOperation()

            return true
        }
    }

    // User doesn't trigger a refresh. Let the superclass handle this action.
    return super.onOptionsItemSelected(item)
}

Java

// Listen for option item selections to receive a notification when the user
// requests a refresh by selecting the refresh action bar item.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        // Check whether the user triggers a refresh:
        case R.id.menu_refresh:
            Log.i(LOG_TAG, "Refresh menu item selected");

            // Signal SwipeRefreshLayout to start the progress indicator.
            mySwipeRefreshLayout.setRefreshing(true);

            // Start the refresh background task. This method calls
            // setRefreshing(false) when it finishes.
            myUpdateOperation();

            return true;
    }

    // User doesn't trigger a refresh. Let the superclass handle this action.
    return super.onOptionsItemSelected(item);
}