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

ลองใช้วิธีเขียน
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);
}