使用 Recycler View 使 LinearLayout 可滚动

Make LinearLayout with Recycler View scrollable

我正在尝试开发一个具有三个水平 RecyclerView 的应用程序,每个 RecyclerView 都包含在一个线性布局中。由于它们非常大(我正在考虑再添加三个),因此我需要使视图可滚动。 最后一行显示不正确,我无法滚动列表来查看前两行中的卡片。这是我的 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>

我该如何解决?我已经在外部 LinearLayout 中修改了 android:layout_height="wrap_content" 但没有任何改变。

通过在外部LinearLayout外添加一个ScrollView标签解决:

<?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>

如果ScrollView没有成功你可以使用androidx.core.widget.NestedScrollView

<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">
 <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_marginBottom="8dp"

            >
    <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.core.widget.NestedScrollView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

希望对你有用。