Android CardView 使用自定义 onItemSelectedListener 移除多个 select

Android CardView Remove multiple select with custom onItemSelectedListener

关于具有多个 select 的 CardView,我遇到了问题。问题出在这张图

我希望它只标记一个卡片视图,这样如果我按下另一个,那么前一个就会变成白色(单select)。所以基本上我希望它具有与单选按钮相同的行为。

适配器中 onBindViewHolder 的代码。我相信它在这里无法完成单个 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);
    });
}

我的 ViewHolder 的代码具有自定义 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());
    }
}

我遵循了 youtube 指南 here,在他的示例中,效果很好。我只是不知道如何删除多个 select 并在其中实现 "Radio button" 行为。

提前致谢!

在您的适配器中有一个全局位置 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();

});