I'm having a problem regarding CardView that has multiple select. The problem is in this pic
I want it to just mark one cardview so that if I press another one, then the previous one will turn white (Single select). So basically I want it to have the same behavior as radio buttons.
The code for my onBindViewHolder
in my Adapter. I believe it is here it fails to do the single select.
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.txt_treatment_name.setText(treatmentList.get(position).getTreatmentName());
holder.txt_treatment_price.setText(treatmentList.get(position).getTreatmentPrice());
holder.txt_treatment_description.setText(treatmentList.get(position).getTreatmentDescription());
if (cardViewList.contains(holder.card_treatment))
cardViewList.add(holder.card_treatment);
holder.setiRecyclerItemSelectedListener((view, pos) -> {
// Set white background for all cards that aren't selected
for (CardView cardView:cardViewList)
cardView.setCardBackgroundColor(context.getColor(R.color.colorWhite)); //
//Set background for selected item
holder.card_treatment.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
holder.txt_treatment_name.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_treatment_description.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_treatment_price.setTextColor(context.getColor(R.color.colorWhite));
//Send broadcast to tell BookingActivity to enable Button NEXT
Intent intent = new Intent(Common.KEY_ENABLE_BUTTON_NEXT);
intent.putExtra(Common.KEY_TREATMENT, treatmentList.get(pos));
localBroadcastManager.sendBroadcast(intent);
});
}
And the code for my ViewHolder
that has the custom itemSelectedListener
static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txt_treatment_name, txt_treatment_price, txt_treatment_description;
CardView card_treatment;
IRecyclerItemSelectedListener iRecyclerItemSelectedListener;
void setiRecyclerItemSelectedListener(IRecyclerItemSelectedListener iRecyclerItemSelectedListener) {
this.iRecyclerItemSelectedListener = iRecyclerItemSelectedListener;
}
MyViewHolder(@NonNull View itemView) {
super(itemView);
txt_treatment_name = itemView.findViewById(R.id.txt_treatment);
txt_treatment_price = itemView.findViewById(R.id.txt_price);
txt_treatment_description = itemView.findViewById(R.id.txt_description);
card_treatment = itemView.findViewById(R.id.card_treatment);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
iRecyclerItemSelectedListener.onItemSelectedListener(v, getAdapterPosition());
}
}
I followed a youtube guide here and in his example, it works just fine. I just don't know how to remove the multiple select and implement "Radio button" behavior in this.
Thank you in advance!
In your adapter have a global position int, that holds which position is clicked:
private int clickedPosition=-1;
//in onbindviewholder
if(clickedPosition==position){
//changed color
holder.card_treatment.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
}else{
//white color
holder.card_treatment.setCardBackgroundColor(context.getColor(R.color.White));
}
//when you click some item
holder.setiRecyclerItemSelectedListener((view, pos) -> {
//hold the clicked position and change color
holder.card_treatment.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
clickedPosition = position;
this.notifyDataSetChanged();
});