提问人:user20958695 提问时间:11/10/2023 最后编辑:user20958695 更新时间:11/11/2023 访问量:41
如何为整个回收器视图而不仅仅是可见部分设置背景?
How to set background for entire recyclerview and not only the visible part?
问:
我有一个带有项目列表的 RecyclerView,它占用的空间比屏幕提供的要多,我需要为整个列表添加背景。背景应扩展到整个列表大小,即背景应在开始时部分显示,滚动时应看到背景的其余部分。当我使用可绘制项设置 RecyclerView 的 background 属性时,它只会扩展到可见部分。
有没有办法让它在整个 RecyclerView 上扩展?
溶液:
我尝试了答案和评论中的建议:
将 RecyclerView 包装在布局中并将可绘制对象提供给外部布局仍然仅在列表的可见部分具有背景。
将项放在 LinearLayout 中,它是 NestedScrollView 的子项。
经过几次反复试验,我找到了另一种解决方案:将 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>
答:
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
我已经创建了显示背景图像的演示,背景图像应该随着项目滚动而滚动。RecyclerView
RecyclerView
为 和 添加以下代码RecyclerView
NestedScrollView
LinearLayout
<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
评论
LinearLayout
NestedScrollView
add()
LinearLayout
LinearLayout