새로고침 요청에 응답

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

새로고침 동작에 응답

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

앱의 새로고침 동작에 응답하려면 다음을 구현합니다. 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()
}

자바

// 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 진행률 표시기 값이 truesetRefreshing()를 사용하고, 그런 다음 업데이트 작업을 수행합니다. 별도의 메서드에서 실제 업데이트를 실행하므로 메서드는 사용자가 스와이프로 업데이트를 트리거하거나 확인할 수 있습니다 업데이트가 완료되면 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);
}