更新リクエストに応答する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
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);
}
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-26 UTC。"],[],[],null,["# Respond to a refresh request\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to pull to refresh in Compose. \n[Pull to refresh in Compose →](/develop/ui/compose/components/pull-to-refresh) \n\nThis document shows how to update your app when the user requests a manual\nrefresh, whether they trigger it with a swipe gesture or use the action bar\nrefresh action.\n\nRespond to the refresh gesture\n------------------------------\n\nWhen the user makes the swipe-to-refresh gesture, the system displays the\nprogress indicator and calls your app's callback method. Your callback method is\nresponsible for updating the app's data.\n\nTo respond to the refresh gesture in your app, implement the\n[SwipeRefreshLayout.OnRefreshListener](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.OnRefreshListener)\ninterface and its\n[onRefresh()](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.OnRefreshListener#onRefresh())\nmethod. The `onRefresh()` method is invoked when the user performs a\nswipe gesture.\n\nPut the code for the actual update operation in a separate method, preferably\nin a `ViewModel`, and call that update method from your\n`onRefresh()` implementation. That way, you can use the same update\nmethod to perform the update when the user triggers a refresh from the action\nbar.\n\nIn your update method, call\n[setRefreshing(false)](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout#setRefreshing(boolean))\nwhen it finishes updating the data. Calling this method instructs the\n[SwipeRefreshLayout](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout)\nto remove the progress indicator and update the view contents.\n\nFor example, the following code implements `onRefresh()` and\ninvokes the method `myUpdateOperation()` to update the data displayed\nby a\n[ListView](/reference/android/widget/ListView): \n\n### Kotlin\n\n```kotlin\n// Sets up a SwipeRefreshLayout.OnRefreshListener that invokes when\n// the user performs a swipe-to-refresh gesture.\n\nmySwipeRefreshLayout.setOnRefreshListener {\n Log.i(LOG_TAG, \"onRefresh called from SwipeRefreshLayout\")\n\n // This method performs the actual data-refresh operation and calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation()\n}\n```\n\n### Java\n\n```java\n// Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when\n// the user performs a swipe-to-refresh gesture.\n\nmySwipeRefreshLayout.setOnRefreshListener(() -\u003e {\n Log.i(LOG_TAG, \"onRefresh called from SwipeRefreshLayout\");\n\n // This method performs the actual data-refresh operation and calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation();\n }\n);\n```\n\nRespond to the refresh action\n-----------------------------\n\nIf the user requests a refresh by using the action bar, the system calls the\n[onOptionsItemSelected()](/reference/androidx/fragment/app/Fragment#onOptionsItemSelected(android.view.MenuItem))\nmethod. Your app responds to this call by displaying the progress indicator and\nrefreshing the app's data.\n\nTo respond to the refresh action, override\n`onOptionsItemSelected()`. In your override method, trigger the\n`SwipeRefreshLayout` progress indicator by calling\n`setRefreshing()` with the value `true`, then perform the\nupdate operation. Perform the actual update in a separate method, so the same\nmethod can be called whether the user triggers the update with a swipe or uses\nthe action bar. When the update finishes, call `setRefreshing(false)`\nto remove the refresh progress indicator.\n\nThe following code shows how to respond to the request action: \n\n### Kotlin\n\n```kotlin\n// Listen for option item selections to receive a notification when the user\n// requests a refresh by selecting the refresh action bar item.\n\noverride fun onOptionsItemSelected(item: MenuItem): Boolean {\n when (item.itemId) {\n\n // Check whether the user triggers a refresh:\n R.id.menu_refresh -\u003e {\n Log.i(LOG_TAG, \"Refresh menu item selected\")\n\n // Signal SwipeRefreshLayout to start the progress indicator.\n mySwipeRefreshLayout.isRefreshing = true\n\n // Start the refresh background task. This method calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation()\n\n return true\n }\n }\n\n // User doesn't trigger a refresh. Let the superclass handle this action.\n return super.onOptionsItemSelected(item)\n}\n```\n\n### Java\n\n```java\n// Listen for option item selections to receive a notification when the user\n// requests a refresh by selecting the refresh action bar item.\n\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n switch (item.getItemId()) {\n\n // Check whether the user triggers a refresh:\n case R.id.menu_refresh:\n Log.i(LOG_TAG, \"Refresh menu item selected\");\n\n // Signal SwipeRefreshLayout to start the progress indicator.\n mySwipeRefreshLayout.setRefreshing(true);\n\n // Start the refresh background task. This method calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation();\n\n return true;\n }\n\n // User doesn't trigger a refresh. Let the superclass handle this action.\n return super.onOptionsItemSelected(item);\n}\n```"]]