본문 바로가기
Android/Code Piece

RecyclerView의 스크롤 차단하기

by featherwing 2020. 4. 23.
반응형

1한 뷰에 여러개의 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의 스크롤을 차단해 주지 않으면 ScrollView와 충돌하게 되어 이중스크롤이 되게 됩니다.

 

1. 아래와 같은 코드를 사용하면 RecyclerView의 스크롤을 차단할 수 있습니다.

mRecyclerview.setLayoutManager(new LinearLayoutManager(this){
    @Override
    public boolean canScrollVertically() { 
    return false;//세로스크롤 차단
    }

    @Override
    public boolean canScrollHorizontally() {
    return false;//가로스크롤 차단
    }
});

RecyclerView 단독으로 사용할 때는 해당 코드는 참 좋은 방법인데, 

ScrollView와 RecyclerView + 해당코드의 경우 레이아웃 내의 일부 RecyclerView에 아이템이 더 적게 표시되는 현상이 발생할 때가 있습니다.

 

 

2. NestedScrollView와 RecyclerView를 사용합니다.

반응형

댓글