此页面由 Cloud Translation API 翻译。
Android Developers
Develop
Core areas
UI
Views
响应刷新请求
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
试试 Compose 方式
Jetpack Compose 是推荐用于 Android 的界面工具包。了解如何在 Compose 中下拉刷新。
在 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。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-07-26。"],[],[]]
