[Android/Layout]리사이클러뷰 내 Swipe 기능으로 리스트 새로고침

2021. 5. 16. 22:24Android/UI-UX 디자인

728x90
반응형

1. SwipeRefreshLayout을 사용하기 위해 implementation 해준다.

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

 

2. 리사이클러뷰가 존재하는 xml에서 리사이클러뷰는 SwipeRefreshLayout으로 감싸준다.

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

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

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

 

3. 스와이프뷰에 setOnRefreshListener를 통해 리스트를 갱신하는 기능을 넣어준다.

view.Swipe_HomeF_SwipeRefresh.setOnRefreshListener {
	// 리사이클러뷰와 연결한 배열을 초기화하고 수정하는 소스를 추가해준다.
    
    // 배열이 변경되었다는 것을 리사이클러뷰에 알려준다
	RecyclerView.adapter?.notifyDataSetChanged()
    // 새로고침 아이콘을 멈춰준다. -> 넣어주지 않으면 무한로딩
	view.Swipe_HomeF_SwipeRefresh.isRefreshing = false
}

 

728x90
반응형