Reputation: 779
I have a recyclerview like the image below
the background color is given dynamically in adapter using position, hence only if there is item(card) i can color the background i have linearlayout as cardview's parent and i am coloring the linear layout.
recyclerview:
<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"
/>
this is item resource:
<?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>
adapter:
public LinearLayout back;
public MyViewHolder(View view) {
super(view);
back = view.findViewById(R.id.back);
}
adapter onBindViewHolder:
holder.back.setBackgroundColor(Color.parseColor(
EntriesItemList.get(getCategory(position))
.getDisposalTypeCategory()
.getColor()));
now the requirement is color even if there is no item like the empty space in 1st section.
that can be done only if i can change the recyclerview background(not card background) dynamically based on adapter position, but looks impossible as there is no relation between the two. any suggestions on how to do this?
Upvotes: 0
Views: 1745
Reputation: 779
I have solved this by always making sure there are 3 items in a row by adding dummy invisible items, so that the background color extends all the way through the row.
Upvotes: 1
Reputation: 134
One solution is that you pass the RecyclerView in the adapter constructor and in your adapter onBindViewHolder method change the RecyclerView background according to your adapter position.
Upvotes: 0