Phản hồi yêu cầu làm mới

Thử cách dùng Compose
Jetpack Compose là bộ công cụ giao diện người dùng được đề xuất cho Android. Tìm hiểu cách kéo để làm mới trong Compose.

Tài liệu này hướng dẫn cách cập nhật ứng dụng khi người dùng yêu cầu làm mới theo cách thủ công, cho dù họ kích hoạt thao tác này bằng cử chỉ vuốt hay sử dụng thao tác làm mới thanh thao tác.

Phản hồi cử chỉ làm mới

Khi người dùng thực hiện thao tác vuốt để làm mới, hệ thống sẽ hiển thị chỉ báo tiến trình và gọi phương thức gọi lại của ứng dụng. Phương thức gọi lại chịu trách nhiệm cập nhật dữ liệu của ứng dụng.

Để phản hồi thao tác làm mới trong ứng dụng, hãy triển khai giao diện SwipeRefreshLayout.OnRefreshListener và phương thức onRefresh() của giao diện đó. Phương thức onRefresh() được gọi khi người dùng thực hiện thao tác vuốt.

Đặt mã cho thao tác cập nhật thực tế trong một phương thức riêng biệt, tốt nhất là trong ViewModel và gọi phương thức cập nhật đó từ quá trình triển khai onRefresh(). Bằng cách đó, bạn có thể sử dụng cùng một phương thức cập nhật để thực hiện việc cập nhật khi người dùng kích hoạt thao tác làm mới từ thanh thao tác.

Trong phương thức cập nhật, hãy gọi setRefreshing(false) khi phương thức này hoàn tất việc cập nhật dữ liệu. Việc gọi phương thức này sẽ hướng dẫn SwipeRefreshLayout xoá chỉ báo tiến trình và cập nhật nội dung khung hiển thị.

Ví dụ: mã sau đây triển khai onRefresh() và gọi phương thức myUpdateOperation() để cập nhật dữ liệu do ListView hiển thị:

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();
  }
);

Phản hồi thao tác làm mới

Nếu người dùng yêu cầu làm mới bằng thanh thao tác, hệ thống sẽ gọi phương thức onOptionsItemSelected(). Ứng dụng của bạn phản hồi lệnh gọi này bằng cách hiển thị chỉ báo tiến trình và làm mới dữ liệu của ứng dụng.

Để phản hồi thao tác làm mới, hãy ghi đè onOptionsItemSelected(). Trong phương thức ghi đè, hãy kích hoạt chỉ báo tiến trình SwipeRefreshLayout bằng cách gọi setRefreshing() với giá trị true, sau đó thực hiện thao tác cập nhật. Thực hiện quá trình cập nhật thực tế trong một phương thức riêng biệt, vì vậy, bạn có thể gọi cùng một phương thức cho dù người dùng kích hoạt quá trình cập nhật bằng thao tác vuốt hay sử dụng thanh thao tác. Khi quá trình cập nhật hoàn tất, hãy gọi setRefreshing(false) để xoá chỉ báo tiến trình làm mới.

Mã sau đây cho biết cách phản hồi thao tác yêu cầu:

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);
}