我希望它仅标记一个卡片视图,以便如果我按下另一个卡片视图,则前一个卡片将变为白色(单选)。所以基本上我希望它具有与单选按钮相同的行为。
我的适配器中onBindViewHolder
的代码。我相信这是它无法进行单选的原因。
@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);
});
}
以及我的具有自定义itemSelectedListener的ViewHolder
的代码
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());
}
}
[我遵循了youtube指南here,在他的示例中,它工作正常。我只是不知道如何在其中删除多个选择并实现“单选按钮”行为。
谢谢你!
在您的适配器中具有一个全局位置int,该位置保持单击的位置:
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();
});