如何为整个回收器视图而不仅仅是可见部分设置背景?

How to set background for entire recyclerview and not only the visible part?

提问人:user20958695 提问时间:11/10/2023 最后编辑:user20958695 更新时间:11/11/2023 访问量:41

问:

我有一个带有项目列表的 RecyclerView,它占用的空间比屏幕提供的要多,我需要为整个列表添加背景。背景应扩展到整个列表大小,即背景应在开始时部分显示,滚动时应看到背景的其余部分。当我使用可绘制项设置 RecyclerView 的 background 属性时,它只会扩展到可见部分。

有没有办法让它在整个 RecyclerView 上扩展?

溶液:

我尝试了答案和评论中的建议:

  1. 将 RecyclerView 包装在布局中并将可绘制对象提供给外部布局仍然仅在列表的可见部分具有背景。

  2. 将项放在 LinearLayout 中,它是 NestedScrollView 的子项。

  3. 经过几次反复试验,我找到了另一种解决方案:将 RecyclerView 包装在 FrameLayout(或任何其他布局)中,并将其包装在 NestedScrollView 中:

<androidx.core.widget.NestedScrollView
            android:layout_width="150dp"
            android:layout_height="0dp">

        <FrameLayout
            android:background="@drawable/my_drawable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </FrameLayout>

</androidx.core.widget.NestedScrollView>
安卓 Android-RecyclerView

评论

0赞 CommonsWare 11/10/2023
可见部分是“整个 RecyclerView”。如果列表中没有很多项目,可以考虑使用 a 和我们的背景和项目,用 a 包装起来进行滚动。LinearLayoutNestedScrollView
0赞 user20958695 11/10/2023
@CommonsWare 谢谢。它不是很多项目,但项目的数量和内容基于经常更改的数据,因此我无法修复 LinearLayout 中的许多项目。有没有办法对项目动态列表执行此操作?
0赞 CommonsWare 11/10/2023
您可以在运行时子项。您不仅限于在布局资源 XML 文件中使用。add()LinearLayoutLinearLayout

答:

1赞 IODevBlue 11/10/2023 #1

尝试在包含 XML 格式的 RecyclerView 的父布局上设置背景:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/your_background">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
0赞 Harshali 11/11/2023 #2

我已经创建了显示背景图像的演示,背景图像应该随着项目滚动而滚动。RecyclerViewRecyclerView

为 和 添加以下代码RecyclerViewNestedScrollViewLinearLayout

<androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/background_poster">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rclMovie"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginHorizontal="4dp"               
                tools:listitem="@layout/item_movie" />

        </LinearLayout>

</androidx.core.widget.NestedScrollView>

有了这个,我就可以将背景图像滚动与项目同步。RecyclerView