My RecyclerView is having some elements in it. Now I tried to change background color of every second element, but my code doesn't work... It is my method onBindViewHolder
public void onBindViewHolder(CityViewHolder holder, int position) {
String cityName = cityList.get(position);
holder.cityTextView.setText(cityName);
if (position%2 == 0) {
holder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorLightGrey));
}
else {
holder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorGrey));
}
}
Do it like this instead (same place in your adapter):
if (position%2 == 0) {
holder.itemView.setBackgroundColor(Color.parseColor("#fafafa"));
} else {
holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));
}
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