새로고침 요청에 응답
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Compose 사용해 보기
Jetpack Compose는 Android에 권장되는 UI 도구 키트입니다. Compose에서 풀 투 리프레시를 사용하는 방법을 알아보세요.
이 문서에서는 사용자가 스와이프 동작으로 새로고침을 트리거하거나 작업 모음 새로고침 작업을 사용하여 수동 새로고침을 요청할 때 앱을 업데이트하는 방법을 보여줍니다.
새로고침 동작에 응답
사용자가 스와이프하여 새로고침 동작을 하면 시스템에서 진행률 표시기를 표시하고 앱의 콜백 메서드를 호출합니다. 콜백 메서드가 앱의 데이터를 업데이트합니다.
앱에서 새로고침 동작에 응답하려면 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()
를 재정의합니다. 재정의 메서드에서 값이 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);
}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-26(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-26(UTC)"],[],[],null,["# Respond to a refresh request\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to pull to refresh in Compose. \n[Pull to refresh in Compose →](/develop/ui/compose/components/pull-to-refresh) \n\nThis document shows how to update your app when the user requests a manual\nrefresh, whether they trigger it with a swipe gesture or use the action bar\nrefresh action.\n\nRespond to the refresh gesture\n------------------------------\n\nWhen the user makes the swipe-to-refresh gesture, the system displays the\nprogress indicator and calls your app's callback method. Your callback method is\nresponsible for updating the app's data.\n\nTo respond to the refresh gesture in your app, implement the\n[SwipeRefreshLayout.OnRefreshListener](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.OnRefreshListener)\ninterface and its\n[onRefresh()](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.OnRefreshListener#onRefresh())\nmethod. The `onRefresh()` method is invoked when the user performs a\nswipe gesture.\n\nPut the code for the actual update operation in a separate method, preferably\nin a `ViewModel`, and call that update method from your\n`onRefresh()` implementation. That way, you can use the same update\nmethod to perform the update when the user triggers a refresh from the action\nbar.\n\nIn your update method, call\n[setRefreshing(false)](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout#setRefreshing(boolean))\nwhen it finishes updating the data. Calling this method instructs the\n[SwipeRefreshLayout](/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout)\nto remove the progress indicator and update the view contents.\n\nFor example, the following code implements `onRefresh()` and\ninvokes the method `myUpdateOperation()` to update the data displayed\nby a\n[ListView](/reference/android/widget/ListView): \n\n### Kotlin\n\n```kotlin\n// Sets up a SwipeRefreshLayout.OnRefreshListener that invokes when\n// the user performs a swipe-to-refresh gesture.\n\nmySwipeRefreshLayout.setOnRefreshListener {\n Log.i(LOG_TAG, \"onRefresh called from SwipeRefreshLayout\")\n\n // This method performs the actual data-refresh operation and calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation()\n}\n```\n\n### Java\n\n```java\n// Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when\n// the user performs a swipe-to-refresh gesture.\n\nmySwipeRefreshLayout.setOnRefreshListener(() -\u003e {\n Log.i(LOG_TAG, \"onRefresh called from SwipeRefreshLayout\");\n\n // This method performs the actual data-refresh operation and calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation();\n }\n);\n```\n\nRespond to the refresh action\n-----------------------------\n\nIf the user requests a refresh by using the action bar, the system calls the\n[onOptionsItemSelected()](/reference/androidx/fragment/app/Fragment#onOptionsItemSelected(android.view.MenuItem))\nmethod. Your app responds to this call by displaying the progress indicator and\nrefreshing the app's data.\n\nTo respond to the refresh action, override\n`onOptionsItemSelected()`. In your override method, trigger the\n`SwipeRefreshLayout` progress indicator by calling\n`setRefreshing()` with the value `true`, then perform the\nupdate operation. Perform the actual update in a separate method, so the same\nmethod can be called whether the user triggers the update with a swipe or uses\nthe action bar. When the update finishes, call `setRefreshing(false)`\nto remove the refresh progress indicator.\n\nThe following code shows how to respond to the request action: \n\n### Kotlin\n\n```kotlin\n// Listen for option item selections to receive a notification when the user\n// requests a refresh by selecting the refresh action bar item.\n\noverride fun onOptionsItemSelected(item: MenuItem): Boolean {\n when (item.itemId) {\n\n // Check whether the user triggers a refresh:\n R.id.menu_refresh -\u003e {\n Log.i(LOG_TAG, \"Refresh menu item selected\")\n\n // Signal SwipeRefreshLayout to start the progress indicator.\n mySwipeRefreshLayout.isRefreshing = true\n\n // Start the refresh background task. This method calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation()\n\n return true\n }\n }\n\n // User doesn't trigger a refresh. Let the superclass handle this action.\n return super.onOptionsItemSelected(item)\n}\n```\n\n### Java\n\n```java\n// Listen for option item selections to receive a notification when the user\n// requests a refresh by selecting the refresh action bar item.\n\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n switch (item.getItemId()) {\n\n // Check whether the user triggers a refresh:\n case R.id.menu_refresh:\n Log.i(LOG_TAG, \"Refresh menu item selected\");\n\n // Signal SwipeRefreshLayout to start the progress indicator.\n mySwipeRefreshLayout.setRefreshing(true);\n\n // Start the refresh background task. This method calls\n // setRefreshing(false) when it finishes.\n myUpdateOperation();\n\n return true;\n }\n\n // User doesn't trigger a refresh. Let the superclass handle this action.\n return super.onOptionsItemSelected(item);\n}\n```"]]