为应用添加滑动刷新功能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
试试 Compose 方式
Jetpack Compose 是推荐用于 Android 的界面工具包。了解如何在 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
子级。
以下示例展示了如何将 SwipeRefreshLayout
微件添加到包含 ListView
的现有布局文件中:
<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>
您也可以将 SwipeRefreshLayout
widget 与 ListFragment
搭配使用。如果布局包含 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],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```"]]