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

Tài liệu này cho biết 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 ứng dụng bằng cử chỉ vuốt hay sử dụng thao tác làm mới trên thanh thao tác.

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

Khi người dùng thực hiện cử chỉ 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 của bạn chịu trách nhiệm cập nhật dữ liệu của ứng dụng.

Để phản hồi cử chỉ 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 một cử chỉ vuốt.

Đặt mã cho thao tác cập nhật thực tế trong một phương thức riêng, 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 cập nhật khi người dùng kích hoạt một lần 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 quá trình cập nhật dữ liệu hoàn tất. 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 thành phần 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 mà 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 cách sử dụng thanh thao tác, thì hệ thống sẽ gọi phương thức onOptionsItemSelected(). Ứng dụng của bạn sẽ 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. Hãy thực hiện cập nhật thực tế trong một phương thức riêng để phương thức tương tự có thể được gọi nếu 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.

Đoạn 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);
}