새로고침 요청에 응답

이 문서에서는 사용자가 스와이프 동작으로 트리거하거나 작업 모음 새로고침 작업을 사용하여 수동 새로고침을 요청할 때 앱을 업데이트하는 방법을 보여줍니다.

새로고침 동작에 응답

사용자가 스와이프하여 새로고침 동작을 하면 시스템에서 진행률 표시기를 표시하고 앱의 콜백 메서드를 호출합니다. 콜백 메서드는 앱의 데이터 업데이트를 담당합니다.

앱에서 새로고침 동작에 응답하려면 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()를 재정의합니다. 재정의 메서드에서 true 값으로 setRefreshing()를 호출하여 SwipeRefreshLayout 진행률 표시기를 트리거한 후 업데이트 작업을 실행합니다. 별도의 메서드에서 실제 업데이트를 실행합니다. 그러면 사용자가 스와이프하여 업데이트를 트리거하거나 작업 모음을 사용하더라도 동일한 메서드를 호출할 수 있습니다. 업데이트가 완료되면 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);
}