ตอบกลับคำขอรีเฟรช
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ลองใช้วิธีเขียน
Jetpack Compose เป็นชุดเครื่องมือ UI ที่แนะนําสําหรับ Android ดูวิธีปัดเพื่อรีเฟรชในเครื่องมือเขียน
เอกสารนี้แสดงวิธีอัปเดตแอปเมื่อผู้ใช้ขอให้รีเฟรชด้วยตนเอง ไม่ว่าจะเรียกให้รีเฟรชด้วยท่าทางสัมผัสการปัดหรือใช้การดำเนินการรีเฟรชของแถบการดำเนินการ
การตอบสนองต่อท่าทางการรีเฟรช
เมื่อผู้ใช้ใช้ท่าทางสัมผัสปัดเพื่อรีเฟรช ระบบจะแสดงตัวบ่งชี้ความคืบหน้าและเรียกเมธอดการเรียกกลับของแอป เมธอดการเรียกกลับจะรับผิดชอบในการอัปเดตข้อมูลของแอป
หากต้องการตอบสนองต่อท่าทางสัมผัสเพื่อรีเฟรชในแอป ให้ใช้
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()
ในเมธอดการลบล้าง ให้ทริกเกอร์ตัวบ่งชี้ความคืบหน้า SwipeRefreshLayout
โดยเรียกใช้ setRefreshing()
ด้วยค่า true
จากนั้นดําเนินการอัปเดต ทำการอัปเดตจริงในเมธอดแยกต่างหาก เพื่อให้เรียกเมธอดเดียวกันได้ไม่ว่าผู้ใช้จะเรียกให้อัปเดตด้วยการปัดหรือใช้แถบการดำเนินการ เมื่อการอัปเดตเสร็จสิ้นแล้ว ให้เรียกใช้ 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 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],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```"]]