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

标签 android android-recyclerview android-cardview

select location branch

Here the Branch items are aligned on a cardview within the recycleview as a button. I need to implement a click on each cardview and the color should change, but the point is that each time i click another cardView the selected cardView before should deselected. I don't even have a logic to implement this. Please help with an easy method

最佳答案

你可以试试这个,

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);
           }
       });
    }
       ... ... 
}

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

关于android - CarView Group 在 android 中选择和取消选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44200614/

相关文章:

java - 无法从 RecyclerView 中的 URL 加载图像

android - CardView 里面的 CardView 没有阴影

android Whatsapp 类似个人资料界面

java - 如果包含其他数据则显示列表 AlertDialog

java - RecyclerView 单项 ID

android - 在 Google map android 上放置点击监听器

android - 如何使用数据绑定(bind)初始化 Recycler View 适配器上的点击监听器?

android - View 不包含卡片 View 中的内容

java - iText 列宽错误

android - 设置一些 TextView 的文本