スワイプでの更新をアプリへ追加する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Compose の方法を試す
Jetpack Compose は、Android で推奨される UI ツールキットです。Compose でプルして更新する方法について学びます。
スワイプでの更新のユーザー インターフェース パターンは、SwipeRefreshLayout
ウィジェット内にすべて実装されています。このウィジェットは縦方向のスワイプを検出して、特有の進行状況バーを表示し、アプリ内のコールバック メソッドをトリガーします。この動作を有効にするには、ListView
または GridView
の親としてウィジェットをレイアウト ファイルに追加し、ユーザーがスワイプしたときに呼び出される更新動作を実装します。
このページでは、ウィジェットを既存のレイアウトに追加する方法について説明します。また、アクションバーのオーバーフロー領域に更新アクションを追加する方法についても説明します。更新アクションを追加することにより、スワイプ ジェスチャーを実行できないユーザーも、外部デバイスを使用して手動更新をトリガーできるようになります。
SwipeRefreshLayout の依存関係を追加する
アプリで SwipeRefreshLayout
を使用するには、build.gradle
ファイルに次の依存関係を追加します。
Groovy
dependencies {
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01"
}
Kotlin
dependencies {
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01")
}
「スワイプで更新」ウィジェットを既存のアプリに追加するには、SwipeRefreshLayout
を単一の ListView
または GridView
の親として追加します。SwipeRefreshLayout
がサポートするのは、単一の子(ListView
または GridView
)に限られます。
次の例は、ListView
を含む既存のレイアウト ファイルに SwipeRefreshLayout
ウィジェットを追加する方法を示しています。
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
なお、ListFragment
でも SwipeRefreshLayout
ウィジェットを使用することができます。ID が "@android:id/list"
の ListView
がレイアウトに格納されている場合、「スワイプで更新」機能は自動的にサポートされます。ただし、上記のように ListView
を明示的に宣言することで、デフォルト ListFragment
ビュー構造よりも優先されるようになります。デフォルト ビュー構造を使用する場合は、SwipeRefreshLayout
および ListFragment
の動作の部分をオーバーライドします。
更新アクションをアクションバーに追加する
アプリのアクションバーに更新アクションを追加して、スワイプ ジェスチャーを実行できないユーザーでも手動更新をトリガーできるようにします。たとえば、ユーザー補助機能が必要なユーザーは、キーボードや十字キーなどの外部デバイスを使用することで、アクションバーのアクションをトリガーできます。
更新アクションは、android:showAsAction=never
属性を設定して、ボタンではなくメニュー項目として追加します。このアクションをボタンとして表示した場合、更新ボタン アクションと「スワイプで更新」アクションを別のものだとユーザーが認識するおそれがあります。更新アクションをアクションバーであまり目立たないようにすることにより、スワイプでの更新の手動操作のほうをユーザーにすすめる一方で、十字キーのユーザーが通常探す場所に利用しやすい操作方法を配置することができます。
「スワイプで更新」アクションをオーバーフロー領域に追加する方法を次のコードに示します。
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_refresh"
android:showAsAction="never"
android:title="@string/menu_refresh"/>
</menu>
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。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,["# Add swipe-to-refresh to your app\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\nThe swipe-to-refresh user interface pattern is implemented entirely within the\n[SwipeRefreshLayout](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout)\nwidget, which detects the vertical swipe, displays a distinctive progress bar, and triggers callback\nmethods in your app. Enable this behavior by adding the widget to your layout file as the parent of\na [ListView](/reference/android/widget/ListView) or\n[GridView](/reference/android/widget/GridView) and implementing the\nrefresh behavior that is invoked when the user swipes.\n\nThis page shows how to add the widget to an existing layout. It also shows how to add a refresh\naction to the action bar overflow area so that users who can't use the swipe gesture can trigger a\nmanual update with an external device.\n\nAdd SwipeRefreshLayout dependency\n---------------------------------\n\nTo use `SwipeRefreshLayout` in your app, add the following dependency to your\n`build.gradle` file: \n\n### Groovy\n\n```groovy\ndependencies {\n implementation \"androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01\"\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n implementation(\"androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01\")\n}\n```\n\nAdd the SwipeRefreshLayout Widget\n---------------------------------\n\nTo add the swipe-to-refresh widget to an existing app, add `SwipeRefreshLayout` as the\nparent of a single `ListView` or `GridView`.\n`SwipeRefreshLayout` only supports a single `ListView` or\n`GridView` child.\n\nThe following example demonstrates how to add the `SwipeRefreshLayout` widget to an\nexisting layout file containing a `ListView`: \n\n```xml\n\u003candroidx.swiperefreshlayout.widget.SwipeRefreshLayout\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:id=\"@+id/swiperefresh\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cListView\n android:id=\"@android:id/list\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\" /\u003e\n\n\u003c/androidx.swiperefreshlayout.widget.SwipeRefreshLayout\u003e\n```\n\nYou can also use the `SwipeRefreshLayout` widget with a\n[ListFragment](/reference/androidx/fragment/app/ListFragment). If the\nlayout contains a `ListView` with the ID `\"@android:id/list\"`, the\nswipe-to-refresh functionality is automatically supported. However, explicitly declaring the\n`ListView` this way supersedes the default `ListFragment` view structure. If\nyou want to use the default view structure, override parts of the `SwipeRefreshLayout`\nand `ListFragment` behavior.\n\nAdd a refresh action to the action bar\n--------------------------------------\n\nAdd a refresh action to your app's action bar so users who can't perform swipe gestures can\ntrigger a manual update. For example, users with accessibility needs can trigger action bar actions\nusing external devices, such as keyboards and D-pads.\n\nAdd the refresh action as a menu item, rather than as a button, by setting the attribute\n`android:showAsAction=never`. If you display the action as a button, users might assume\nthe refresh button action is different from the swipe-to-refresh action. Making the refresh action\nless conspicuous in the action bar encourages users to perform manual updates with swipe gestures\nwhile maintaining the accessible option where D-pad users look for it.\n\nThe following code demonstrates how to add the swipe-to-refresh action to the overflow area: \n\n```xml\n\u003cmenu xmlns:android=\"http://schemas.android.com/apk/res/android\" \u003e\n \u003citem\n android:id=\"@+id/menu_refresh\"\n android:showAsAction=\"never\"\n android:title=\"@string/menu_refresh\"/\u003e\n\u003c/menu\u003e\n```"]]