Nuke Olaf - Log Store

[Android] 당겨서 새로고침 - swipe refresh layout 본문

Android

[Android] 당겨서 새로고침 - swipe refresh layout

NukeOlaf 2020. 3. 26. 14:03

1. SwipeRefreshLayout 이란 무엇인가?

Swipe Refresh Layout 이란, 화면의 컨텐츠를 새로고침할 떄 사용되는 view 이다.

view 를 수직으로 당겨서 화면의 contents 를 새로고침할 떄 사용한다.

Swipe Refresh Layout 을 초기화하는 Activity 에는 OnRefreshListener 를 추가해주어야 한다.
Swipe Refresh Layout 는 사용자 제스처가 실행될때마다 Listener 에게 이를 알려준다.

Listener 는 contents 새로고침을 정확히 언제 실행할지 결정한다.

Listener 가 새로고침이 필요없다고 결정하면,
새로고침의 시각적 요소를 취소하기 위해 setRefreshing(false) 를 호출할 것이다.
만약 Activity 가 progress 애니메이션을 보여주길 원한다면, setRefreshing(true) 를 호출,
gesture 와 progress 애니메이션을 비활성화하려면, setEnabled(false) 를 호출할 것이다.

 

2. SwipeRefreshLayout 을 사용하는 방법

(1) 새로고침을 적용할 view 를 SwipeRefreshLayout 으로 감싸준다.

RecyclerVIew 를 감싸주었다.

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/main_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

 

(2) SwipeRefreshLayout 에 OnRefreshListener 를 등록한다.

refresh_layout.setOnRefreshListener {
    // 새로고침 코드를 작성
    var data = MainData("refresh", "refreshing completed")
    recyclerViewAdapter.add(data)
    recyclerViewAdapter.notifyDataSetChanged()
    
    // 새로고침 완료시,
    // 새로고침 아이콘이 사라질 수 있게 isRefreshing = false
    refresh_layout.isRefreshing = false
}

 

(3) RefreshLayout 의 color 변경하기

refresh_layout.setColorSchemeColors( R.color.colorRefresh )

 

 

참고 >>>

안드로이드 디벨롭퍼 문서

The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child. This view will also be made the target of the gesture and will be forced to match both the width and the height supplied in this layout. The SwipeRefreshLayout does not provide accessibility events; instead, a menu item must be provided to allow refresh of the content wherever this gesture is used.

https://developer.android.com/reference/androidx/swiperefreshlayout/widget/package-summary

 

 

Comments