CarView Group 在 android 中选择和取消选择项目

jis*_*isa 2 android android-cardview android-recyclerview

选择地点分行

这里,分支项目作为按钮在回收视图内的卡片视图上对齐。我需要在每个卡片视图上实现单击,并且颜色应该改变,但要点是每次我单击另一个卡片视图时,之前选择的卡片视图都应该取消选择。我什至没有实现这个的逻辑。请帮忙提供一个简单的方法

小智 5

你可以试试这个,

public class yourRecyclerViewAdapter extends RecyclerView.Adapter<yourRecyclerViewAdapter.yourViewHolder> {

    private static int lastCheckedPos = 0;

         ... ...

    public void onBindViewHolder(ViewHolder holder, final int position) {

        if(position == lastCheckedPos) {
            holder.cardView..setCardBackgroundColor(Color.RED); //Define the re
        } else {
            holder.cardView..setCardBackgroundColor(Color.WHITE);
        }

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int prevPos = lastCheckedPos;
                lastCheckedPos = position;
                notifyItemChanged(prevPos);
                notifyItemChanged(lastCheckedPos);
           }
       });
    }
       ... ... 
}
Run Code Online (Sandbox Code Playgroud)

它可能给出实现的基本逻辑