首页 > 解决方案 > 加载项目时如何设置recyclerview的背景?

问题描述

我的应用程序中有一个 recyclerview,我想将其设置background为黑色。我已经尝试使用该background属性设置 recyclerview 的背景,但它只在具有项目的 recyclerview 部分设置黑色背景。

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/items"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:listitem="@layout/item_card">
</androidx.recyclerview.widget.RecyclerView>

如果回收站视图没有填满整个屏幕,则底部始终为白色。当活动刚刚打开并且 recyclerview 的项目仍在加载时,recyclerview 仅显示为空白(白色)空间。

你基本上如何设置recyclerview的背景?

编辑:这是整个布局

<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/spacing_middle"
        android:paddingEnd="@dimen/spacing_middle"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/items"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            tools:listitem="@layout/item_card">
        </androidx.recyclerview.widget.RecyclerView>

        <ProgressBar
            android:id="@+id/progress_bar"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:layout_gravity="center"
            android:indeterminateTint="@color/white"
            android:progressDrawable="@drawable/circular_progress_bar" />
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

标签: androidandroid-recyclerview

解决方案


将 recyclerview 放在滚动视图中从来都不是一个好主意,也不需要。您遇到问题的原因是因为您尝试在没有定义高度的东西上匹配父级,因为它在滚动视图中。
实际上,您需要的唯一两件事是 RV 和进度条,使用相对布局并将 RV 对齐到顶部,将 PB 对齐到底部并将 RV 设置为 PB 上方的布局。将相对布局的颜色设置为黑色,您根本不需要为 RV 着色。


推荐阅读