아래로 당겨 새로고침 : SwipeRefreshLayout

2021. 9. 2. 00:21

1. build.gradle에 추가

// Swipe Refresh Layout
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

 

2. xml layout 작성

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        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" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

RecyclerView, ListView 등 새로고침 될 영역을 SwipeRefreshLayout에 넣어준다.

 

3. Activity 작성

binding.swipeRefreshLayout.setOnRefreshListener {
        binding.swipeRefreshLayout.isRefreshing = false
        // todo 새로고침할 데이터
}

swipeRefreshLayout에 OnRefreshListener를 달아준다.

위 코드에서는 ViewBinding 후 작성하였다.

isRefreshing을 false로 해주어야 새로고침 후에 돌아가는 아이콘을 지워줄 수 있다.

아래에 새로고침 할 내용을 적어주면 된다.

728x90

BELATED ARTICLES

more