回應重新整理要求
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
試試 Compose 的方式
Jetpack Compose 是 Android 推薦的 UI 工具包。瞭解如何在 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 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間: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```"]]