问题
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