Android 开发者
开发
核心领域
UI
View
响应刷新请求
使用收藏集井然有序地管理内容 根据您的偏好保存内容并进行分类。
试试 Compose 的方法
Jetpack Compose 是 Android 推荐的 UI 工具包。了解如何在 Compose 中实现下拉刷新。
Compose 中的下拉刷新 →
本文档介绍了当用户请求手动刷新时如何更新您的应用,无论是通过滑动操作还是使用操作栏刷新操作触发的刷新。
响应刷新手势
当用户执行下拉刷新手势时,系统会显示进度指示器并调用您的应用的回调方法。您的回调方法负责更新应用的数据。
要在您的应用中响应刷新手势,请实现 SwipeRefreshLayout.OnRefreshListener 接口及其 onRefresh() 方法。onRefresh() 方法在用户执行滑动操作时调用。
将实际更新操作的代码放在单独的方法中,最好是在 ViewModel 中,然后在您的 onRefresh() 实现中调用该更新方法。这样,当用户通过滑动或操作栏触发刷新时,您都可以使用相同的更新方法执行更新。
在您的更新方法中,数据更新完成后,请调用 setRefreshing(false)。调用此方法会指示 SwipeRefreshLayout 移除进度指示器并更新 View 内容。
例如,以下代码实现了 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()。在您的覆盖方法中,通过调用 setRefreshing() 并传入值 true 来触发 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 年 2 月 10 日。
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooSteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[]]