Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set recyclerview item background color repeatedly

I am trying to set different background color in cardview but it's not working I have color array that contain color

public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.MyViewHolder> {
    private Context mContext;
    private List<String> leavetypeList;
    private List<String> leavebalanceList;

    public String[] mColors = {
            "3F51B5","FF9800","009688","673AB7"
    };


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView leavecount,leavename;
        public ImageView thumbnail;
        public RelativeLayout rlauthor;
        CardView cardView;
        View viewline;

        public MyViewHolder(View view) {
            super(view);
            leavecount = (TextView)view.findViewById(R.id.tvleavenumber);
            leavename = (TextView)view.findViewById(R.id.tvleavetype);
            cardView=(CardView) view.findViewById(R.id.cdvdashboard);
        }
    }

    public DashboardAdapter(Context context, List<String> leavetypeList,List<String> leavebalanceList) {
        this.mContext = context;
        this.leavetypeList = leavetypeList;
        this.leavebalanceList = leavebalanceList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.raw_dashboard, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        Typeface tf;
        tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/HKNova-Medium.ttf");
        holder.leavecount.setTypeface(tf);
        holder.leavename.setTypeface(tf);

        holder.leavecount.setText(leavebalanceList.get(position));
        holder.leavename.setText(leavetypeList.get(position));

        String color="#"+mColors[position];

        for(int c=1;c<mColors.length;c++)
        {

            holder.cardView.setCardBackgroundColor(Color.parseColor(color));

            if(c==mColors.length)
            {
                 c=1;
            }
        }


    }

    @Override
    public int getItemCount() {
        return leavetypeList.size();
    }
}

I want this type of output but only getting the last color and if color finish than show other item background color start from color[0] position

Any help would be highly appreciated

enter image description here

like image 432
Mitesh Makwana Avatar asked Dec 17 '25 12:12

Mitesh Makwana


1 Answers

It will not work like that. You need to put this logic inside onBindViewHolder where you can change color with help of position. Use like

public String[] mColors = {"#3F51B5","#FF9800","#009688","#673AB7"};

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    holder.itemView.setBackgroundColor(Color.parseColor(mColors[position % 4])); // 4 can be replaced by mColors.length
}

And look how I redefined mColors array with # prefixed. So you need to add # while calculating colors.

like image 125
Pankaj Kumar Avatar answered Dec 19 '25 06:12

Pankaj Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!