본문 바로가기
Android/Dev

NestedScrollView - ScrollView 와 RecyclerView 사용시의 오류 해결

by featherwing 2020. 4. 23.
반응형

한 뷰에 여러개의 RecyclerView를 사용하는 경우, 아래와 같이 ScrollView 안에 Recyclerview를 넣게 됩니다

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/scrollView"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/list_name1"
            android:layout_margin="16dp"
            android:textSize="12dp"
            android:layout_width="wrap_content"
            android:text="recyclerview 1"
            android:layout_height="wrap_content"/>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <androidx.appcompat.widget.AppCompatTextView
            android:layout_margin="16dp"
            android:textSize="12dp"
            android:layout_width="wrap_content"
            android:id="@+id/list_name2"
            android:text="recyclerview 2"
            android:layout_height="wrap_content"/>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</ScrollView>

 

이때 일부 RecyclerView에서, 아이템들이 다 표시되지 못하고 데이터보다 더 적게 표시되는 현상이 계속 발생했었습니다.

 

로그를 달아보니, list의 아이템들이 정확하고 list.size()도 정확한데,  size보다 더 적은 아이템들이 계속 표시되는 현상이었습니다.

 

별다른 해결책을 찾지 못하고 잊어버리고 있었는데, 최근 여러개의 RecyclerView를 다시 써야 할 일이 생겨서 보니

 

또 다시 이런 현상이 발생하였습니다.

 

 

원인은... 정확하게는 알 수 없고,  ScrollView와 RecyclerView간에 버그가 있는것 같습니다. 

 

단, 해결책은 정확합니다.

 

ScrollView 대신 NestedScrollView를 사용하면 문제가 싹 해결됩니다.

 

거기에, ScrollView 속에 RecyclerView를 넣을 때 발생하는 이중 스크롤 문제도 별도의 코드를 쓰지 않고 해결할 수 있습니다.

 

사용방법은 아래와 같습니다.

 

1. ScrollView 대신 NestedScrollView 사용

2. RecyclerView 속성에 android:nestedScrollingEnabled="false" 부여

<NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/scrollView"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/list_name1"
            android:layout_margin="16dp"
            android:textSize="12dp"
            android:layout_width="wrap_content"
            android:text="recyclerview 1"
            android:layout_height="wrap_content"/>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview_1"
            android:nestedScrollingEnabled="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <androidx.appcompat.widget.AppCompatTextView
            android:layout_margin="16dp"
            android:textSize="12dp"
            android:layout_width="wrap_content"
            android:id="@+id/list_name2"
            android:text="recyclerview 2"
            android:layout_height="wrap_content"/>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview_2"
            android:nestedScrollingEnabled="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</NestedScrollView>

 

RecyclerView에는 ScrollView 대신 NestedScrollView를 사용해 주세요.

반응형

댓글