Search code examples
javaandroidandroid-linearlayout

Make LinearLayout with Recycler View scrollable


I'm trying to develop an app which has three horizontal RecyclerView, each one wrapped in a Linear Layout. Since they are pretty large (and I'm thinking to add three more of them) I need to make the view scrollable. The last row is shown incorrectly and I cannot scroll the list to view the cards as on the top two rows. This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Movies"
            android:layout_marginLeft="@dimen/_10sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:textSize="@dimen/_20ssp"
            android:textStyle="bold"
            android:textColor="#ffffff"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_5sdp"
            android:text="Action"
            android:layout_marginLeft="@dimen/_10sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:textSize="@dimen/_12ssp"
            android:textColor="@color/colorAccent"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="@dimen/_150sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:orientation="vertical">
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/rc_view"/>


        </LinearLayout>

        <View
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:layout_height="0.5dp"
            android:background="#01d277"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_5sdp"
            android:layout_gravity="center"
            android:text="Fantasy"
            android:layout_marginLeft="@dimen/_10sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:textSize="@dimen/_12ssp"
            android:textColor="@color/colorAccent"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10sdp"
            android:minHeight="@dimen/_150sdp"
            android:orientation="vertical">
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/rc_view2"/>

        </LinearLayout>

        <View
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:layout_height="0.5dp"
            android:background="#01d277"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_5sdp"
            android:layout_gravity="center"
            android:text="Animation"
            android:layout_marginLeft="@dimen/_10sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:textSize="@dimen/_12ssp"
            android:textColor="@color/colorAccent"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10sdp"
            android:minHeight="@dimen/_150sdp"
            android:orientation="vertical">
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/rc_view3"/>

        </LinearLayout>

    </LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

How can I solve? I already modified android:layout_height="wrap_content" in the external LinearLayout but nothing changed.


Solution

  • Solved by adding a ScrollView tag outside the external LinearLayout:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Movies"
                    android:layout_marginLeft="@dimen/_10sdp"
                    android:layout_marginRight="@dimen/_10sdp"
                    android:textSize="@dimen/_20ssp"
                    android:textStyle="bold"
                    android:textColor="#ffffff"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_5sdp"
                    android:text="Action"
                    android:layout_marginLeft="@dimen/_10sdp"
                    android:layout_marginRight="@dimen/_10sdp"
                    android:textSize="@dimen/_12ssp"
                    android:textColor="@color/colorAccent"/>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="@dimen/_150sdp"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:orientation="vertical">
                    <androidx.recyclerview.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:id="@+id/rc_view"/>
    
    
                </LinearLayout>
    
                <View
                    android:layout_marginTop="@dimen/_10sdp"
                    android:layout_width="match_parent"
                    android:layout_gravity="center"
                    android:layout_height="0.5dp"
                    android:background="#01d277"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_5sdp"
                    android:layout_gravity="center"
                    android:text="Fantasy"
                    android:layout_marginLeft="@dimen/_10sdp"
                    android:layout_marginRight="@dimen/_10sdp"
                    android:textSize="@dimen/_12ssp"
                    android:textColor="@color/colorAccent"/>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:minHeight="@dimen/_150sdp"
                    android:orientation="vertical">
                    <androidx.recyclerview.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:id="@+id/rc_view2"/>
    
                </LinearLayout>
    
                <View
                    android:layout_marginTop="@dimen/_10sdp"
                    android:layout_width="match_parent"
                    android:layout_gravity="center"
                    android:layout_height="0.5dp"
                    android:background="#01d277"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_5sdp"
                    android:layout_gravity="center"
                    android:text="Animation"
                    android:layout_marginLeft="@dimen/_10sdp"
                    android:layout_marginRight="@dimen/_10sdp"
                    android:textSize="@dimen/_12ssp"
                    android:textColor="@color/colorAccent"/>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:minHeight="@dimen/_150sdp"
                    android:orientation="vertical">
                    <androidx.recyclerview.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:id="@+id/rc_view3"/>
    
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>