안드로이드
[Android] SwipeRefreshLayout과 ScrollView 동시 사용시 문제
Zibro
2022. 1. 24. 16:49
728x90
반응형
화면을 아래로 당겨서 UI를 새로고침 하기 위해서 사용한 SwipeRefreshLayout과 세로로 긴 화면을 스크롤하여 보여주기 위해 사용한 ScrollView를 동시에 사용했을 때 아래로 스크롤 한 다음, 다시 위로 스크롤하려 했을 때 스크롤되지 않고 무조건 새로고침이 되는 상황이 발생했었습니다.🤷🏻♂️
위 문제점을 해결하기 위한 방법이 2가지가 있습니다.
1. NestedScrollView 사용
이 방법은 간단한데, ScrollView 대신에 NestedScrollView를 사용하면 해결됩니다.

안드로이드 개발자 문서에서도 나와있다시피 NestedScrollView는 ScrollView와 비슷하지만 부모와 자식 사이에서의 중첩 스크롤을 지원한다고 나와있어서 아주 간단한 해결책이 될 수 있습니다. 🔥
반응형
2. ScrollView가 최상단에 위치했을 때만 Swipe가 작동하도록 처리
아래와 같은 구조로 작성된 xml에서 ScrollView가 최상단에 위치 했을 때만 Swipe가 작동하도록 처리를 하려면
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
...생략
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
ScrollView의 Y축이 0에 위치 했을 때만 SwipeRefreshLayout을 활성화하여 새로고침 할 수 있도록 작성해줍니다!👍🏻
//scrollview와 refreshlayout을 동시에 쓰기 위해서
//scrollview의 scroll y축이 0, 즉 최상단에 위치했을 때만 refreshLayout을 활성화하도록함
binding.scrollView.viewTreeObserver.addOnScrollChangedListener {
refreshLayout.isEnabled = scrollView.scrollY ==0
}
출처
https://velog.io/@kimbsu00/Android-7
https://oneday0012.tistory.com/24
728x90
반응형