Cardview - set a different background color for each element

I am working on improving the skills of RecyclerView and CardView since I am new to this.

I created the CardView layout and the RecyclerView layout, and then the Layout Manager and View interface, the minimum necessary for the application to look like this:

http://i.stack.imgur.com/DZzNi.jpg

What I want to do is I need different background colors for each element. For example - Red for "Froyo", Amber "Gingerbread", etc. on the.

Anyway, can I do this?

Also, I need elements separated by 1dp

thanks

+5
source share
2 answers

Yes, you can do this in the RecyclerViews onBindViewHolder method by specifying each map at that 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 that you use as a RecyclerView row element.

+11
source

To separate an element, my 1 pixel density: Change the widget. View the map and specify the required space in your relative layout. [Change widget.map view accordingly] https://i.stack.imgur.com/nSJ7W.png

+1
source

Source: https://habr.com/ru/post/1236783/


All Articles