이 과정에서는 사용자가 스와이프 동작이나 작업 모음 새로고침 작업을 통해 새로고침을 트리거하여 수동 새로고침을 요청하는 경우 앱을 업데이트하는 방법을 보여줍니다.
새로고침 작업에 응답
사용자가 스와이프 작업을 하면 시스템에서 진행률 표시기를 표시하고 앱의 콜백 메서드를 호출합니다. 콜백 메서드가 앱의 데이터를 실제로 업데이트합니다.
앱에서 새로고침 동작에 응답하려면 SwipeRefreshLayout.OnRefreshListener
인터페이스와 onRefresh()
메서드를 구현합니다. 사용자가 스와이프 동작을 실행하면 onRefresh()
메서드가 호출됩니다.
실제 업데이트 작업을 위한 코드는 별도의 메서드에 지정하고 onRefresh()
구현에서 이 업데이트 메서드를 호출해야 합니다. 그러면 사용자가 작업 모음에서 새로고침을 트리거하는 경우 동일한 업데이트 메서드를 사용하여 업데이트를 실행할 수 있습니다.
업데이트 메서드는 데이터 업데이트가 완료되면 setRefreshing(false)
을 호출합니다. 이 메서드를 호출하여 SwipeRefreshLayout
에서 진행률 표시기를 삭제하고 뷰 콘텐츠를 업데이트하도록 지시합니다.
예를 들어 다음 코드는 onRefresh()
를 구현하고 myUpdateOperation()
메서드를 호출하여 ListView
에 표시되는 데이터를 업데이트합니다.
Kotlin
/* * 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. // The method calls setRefreshing(false) when it's finished. myUpdateOperation() }
자바
/* * Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user * performs a swipe-to-refresh gesture. */ mySwipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout"); // This method performs the actual data-refresh operation. // The method calls setRefreshing(false) when it's finished. myUpdateOperation(); } } );
새로고침 작업에 응답
사용자가 작업 모음을 사용하여 새로고침을 요청하면 시스템에서는 onOptionsItemSelected()
메서드를 호출합니다. 앱에서는 진행률 표시기를 표시하고 앱의 데이터를 새로고침하여 이 호출에 응답해야 합니다.
새로고침 작업에 응답하려면 onOptionsItemSelected()
를 재정의합니다. 재정의 메서드에서 값이 true
인 setRefreshing()
을 호출하여 SwipeRefreshLayout
진행률 표시기를 트리거하고 업데이트 작업을 실행합니다. 다시 한번 별도의 메서드로 실제 업데이트를 실행해야 하므로 사용자가 스와이프 또는 작업 모음을 통해 업데이트를 트리거하면 동일한 메서드를 호출할 수 있습니다. 업데이트가 완료되면 setRefreshing(false)
을 호출하여 새로고침 진행률 표시기를 삭제합니다.
다음 코드는 요청 작업에 응답하는 방법을 보여줍니다.
Kotlin
/* * Listen for option item selections so that we 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 if user triggered 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's finished. myUpdateOperation() return true } } // User didn't trigger a refresh, let the superclass handle this action return super.onOptionsItemSelected(item) }
자바
/* * Listen for option item selections so that we 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 if user triggered 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's finished. myUpdateOperation(); return true; } // User didn't trigger a refresh, let the superclass handle this action return super.onOptionsItemSelected(item); }
참고: 새로고침 동작에 응답에서 설명한 대로 사용자가 스와이프 작업을 통해 새로고침을 트리거하면 setRefreshing()
을 호출할 필요가 없습니다.
SwipeRefreshLayout
위젯이 진행률 표시기를 표시하고 업데이트가 완료되면 표시기를 삭제합니다. 하지만 스와이프 동작을 제외한 방법으로 업데이트가 트리거되면 setRefreshing()
을 사용하여 명시적으로 진행률 표시기를 사용 설정해야 합니다.
실제로 데이터를 새로고침하는 메서드가 setRefreshing(false)
을 호출하여 업데이트가 완료되었음을 알립니다.