CarView Group with select an deselecting items in android

非 Y 不嫁゛ 提交于 2020-01-15 12:02:31

问题


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


回答1:


You can try this,

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

It may give a base logic on the implementation



来源:https://stackoverflow.com/questions/44200614/carview-group-with-select-an-deselecting-items-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!