I am creating an Android application that contains a RecyclerView with a nested CardView. I need to alternate every other Card to a different color. I am using @Override to override the onBindViewHolder(ViewHolder vh, int pos) method. I need to change the background color from that method (I assume), but there are no methods to set the background color of a ViewHolder!
I'm sorry for my noobie-ness, I'm learning Android development now.
-Ben
EDIT: Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_9);
pieRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
pieRecyclerView.setHasFixedSize(true);
pies = makePies();
pieLayoutManager = new LinearLayoutManager(this);
pieRecyclerView.setLayoutManager(pieLayoutManager);
PieAdapter adapter = new PieAdapter(pies);
pieRecyclerView.setAdapter(adapter);
}
public class PieAdapter extends RecyclerView.Adapter<ViewHolder> {
Context mContext;
ArrayList<Pie> mPies;
LayoutInflater mInflater;
public PieAdapter(ArrayList<Pie> pies) {
mPies = pies;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Pie currentPie = mPies.get(position);
holder.textViewName.setText(currentPie.mName);
holder.textViewDescription.setText(currentPie.mDescription);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String price = formatter.format(currentPie.mPrice);
holder.textViewPrice.setText(price);
}
@Override
public int getItemCount() {
return mPies.size();
}
}
private class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public TextView textViewDescription;
public TextView textViewPrice;
public ViewHolder(View v) {
super(v);
textViewName = (TextView) v.findViewById(R.id.textViewName);
textViewDescription = (TextView) v.findViewById(R.id.textViewDescription);
textViewPrice = (TextView) v.findViewById(R.id.textViewPrice);
}
}
private ArrayList<Pie> makePies() {
ArrayList<Pie> pies = new ArrayList<Pie>();
pies.add(new Pie("Apple", "An old-fashoned favorite.", 1.5));
pies.add(new Pie("Blueberry", "Made with fresh Maine blueberries.", 1.5));
pies.add(new Pie("Cherry", "Delicious and fresh made daily", 2.0));
pies.add(new Pie("Coconut Cream", "A customer favorite.", 2.5));
return pies;
}
private class Pie {
String mName;
String mDescription;
double mPrice;
public Pie(String name, String description, double price) {
this.mName = name;
this.mDescription = description;
this.mPrice = price;
}
}`
The ViewHolder object is not itself a View. If you want to change the background of the entire list item, you probably want to call viewHolder.itemView.setBackgroundColor(...). The itemView of a ViewHolder is whatever View you passed into the constructor.
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