I'm working on improving my RecyclerView
and CardView
skills as I'm new to this.
I created a CardView
layout and RecyclerView
layout and then Layout Manager and View adapter, minimum that's required to get the app looking like this -
http://i.stack.imgur.com/DZzNi.jpg
What I want to do is - I want different background colors for every element. For example - Red for "Froyo" , Amber "Gingerbread" and so on.
Any way I can do it?
Also, I want elements separated by 1dp
Thank you
Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.
With the help of CardView, we can add radius and elevation to our items of RecyclerView. CardView gives a rich look and feels to our list of data. RecyclerView: RecyclerView is an extended version of ListView. in RecyclerView we can load a large amount of data and items of RecyclerView can have a custom design.
Yes you can do that in RecyclerViews onBindViewHolder
method by referring to each card by it's position.
public void onBindViewHolder(MyViewHolder holder, int position) {
if(position==1)
holder.view.setBackgroundColor(Color.RED);
else if(position==2)
holder.view.setBackgroundColor(Color.parseColor("#amberColorCode")); //and so on..
}
here view
is your TextView
or any other view which you are using as RecyclerView
row item.
For separating element my 1 density pixel: Modify your widget.Card View and specify the required space in your relative layout.[Change your widget.card view accordingly] https://i.stack.imgur.com/nSJ7W.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With