ตอบกลับคำขอรีเฟรช

ลองใช้วิธีแบบ Compose
Jetpack Compose เป็นชุดเครื่องมือ UI ที่แนะนำสำหรับ Android ดูวิธีดึงเพื่อรีเฟรชใน Compose

เอกสารนี้แสดงวิธีอัปเดตแอปเมื่อผู้ใช้ขอรีเฟรชด้วยตนเอง ไม่ว่าจะเรียกให้แสดงด้วยท่าทางสัมผัสแบบปัดหรือใช้การดำเนินการรีเฟรชในแถบการดำเนินการ

ตอบสนองต่อท่าทางการรีเฟรช

เมื่อผู้ใช้ทำท่าทางสัมผัสแบบปัดเพื่อรีเฟรช ระบบจะแสดงตัวบ่งชี้ความคืบหน้าและเรียกใช้เมธอด Callback ของแอป เมธอด Callback มีหน้าที่อัปเดตข้อมูลของแอป

หากต้องการตอบสนองต่อท่าทางการรีเฟรชในแอป ให้ใช้อินเทอร์เฟซ 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);
}