我有一个类似下图的回收视图
背景色在adapter using position中动态给定,因此,只有在有项目(卡)的情况下,我才能对背景进行着色,我将linearlayout作为cardview的父级,并对线性布局进行着色。
回收视图:
<android.support.v7.widget.RecyclerView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="40dp"
style="@style/scrollbar_style"
/>
这是项目资源:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/back"
android:paddingBottom="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iconEntry"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_marginBottom="2dp"/>
<TextView
android:id="@+id/titleEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_marginBottom="2dp"/>
<TextView
android:id="@+id/titleSpanishEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_marginBottom="2dp"/>
<TextView
android:id="@+id/countEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textSize="10sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
适配器:
public LinearLayout back;
public MyViewHolder(View view) {
super(view);
back = view.findViewById(R.id.back);
}
BindViewHolder上的适配器:
holder.back.setBackgroundColor(Color.parseColor(
EntriesItemList.get(getCategory(position))
.getDisposalTypeCategory()
.getColor()));
现在的要求是颜色,即使没有像第一节中的空白那样的项目。
只有根据适配器位置动态更改recyclerview背景(而不是卡背景)才能做到这一点,但看起来不可能,因为两者之间没有任何关系。有什么建议吗?