App노자

[Android] SwipeRefreshLayout (새로고침) 본문

Android/AndroidStudio

[Android] SwipeRefreshLayout (새로고침)

앱의노예 2023. 7. 8. 10:34

1. SwipeRefreshLayout이란?


SwipeRefreshLayout은 Android 앱에서 새로고침 기능을 구현할 수 있는 UI 위젯 중 하나이다

SwipeRefreshLayout은 사용자가 화면을 스와이프 하면 새로고침 동작을 수행하여

새로운 데이터를 가져오거나 화면을 업데이트하는 데 사용돤다

새로고침이 완료될 때마다 알림을 받을 OnRefreshListener를 추가해야 하며

SwipeRefreshLayout은 처리가 완료될 때마다 리스너에게 알린다

리스너가 새로 고침이 필요 없다고 판단하면 setRefreshing(false)을 호출하여 새로 고침의 시각적 표시를 취소한다

SwipeRefreshLayout은새로 고침 기능을 넣을 뷰의 부모로 작성해야 하며 하나의 자식 view만 지원할 수 있으며

주로 리스트나 그리드와 같은 스크롤 가능한 컨테이너 위젯과 함께 사용된다

 

https://developer.android.com/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout

 

SwipeRefreshLayout  |  Android Developers

androidx.constraintlayout.core.motion.parse

developer.android.com

2. build.gradle (Module)


dependencies {
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
}

build.gradle(Module: app)에서 Swiperefreshlayout과 관련된 빌드 종속성을 추가한다

3. XML


<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id='@+id/refresh_layout'>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SwipeRefreshLayout"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

 

 

 

 

3. Kotlin 파일


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var RefreshLayout: SwipeRefreshLayout = findViewById(R.id.refresh_layout)

        RefreshLayout.setOnRefreshListener {
            println("SwipeRefreshLayout Test")
            RefreshLayout.isRefreshing = false
        }

    }
}