响应刷新请求
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
试试 Compose 方式
Jetpack Compose 是推荐用于 Android 的界面工具包。了解如何在 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],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```"]]