I have a RecyclerView of CardViews. I am trying to have it where you click on one of the CardViews and it changes the background of that CardView. This part works. Where I am stuck is trying to change all of the other CardViews back to white so it doesn't look like multiple are selected.
I feel the best way to do this is a for loop but I can't seem to find what the for loop should be. I also tried resetting all the CardViews with notifyDataSetChanged()
but that did not work either.
Here is my onItemClicked()
function (that works correctly):
mAdapter = new TransferCard(list, getContext(), new TransferCard.OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
//What code goes here to change all Views in the RecyclerView to have a background of white
RelativeLayout temp = v.findViewById(R.id.cardForeground);
item = list.get(position);
temp.setBackgroundColor(getResources().getColor(R.color.selected));
}
});
Here is my onCreateViewHolder()
where the setOnTouchListener()
event is.
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.transfer_card, parent, false);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(v, parent.indexOfChild(v));
}
});
return new MyViewHolder(itemView);
}
It seems I am just about there, but I need to know what to do with the for loop.
Can you post the whole Recycler-View-code? You could add an ArrayList<Spinner> to your AttendanceAdapter and add the Spinners to the ArrayList in the constructor of AttendanceViewHolder . Then you can iterate over all spinners after a button click. An other way would be the getChildAt() method of recyclerview.
RecyclerView. Adapter base class for presenting paged data from PagingData s in a RecyclerView . Base class for an Adapter. Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView .
for (int x = recyclerView.getChildCount(), i = 0; i < x; ++i) {
ViewHolder holder = recyclerView.getChildViewHolder(recyclerView.getChildAt(i));
}
In general, you should write your adapter in such a way that onBindViewHolder()
is the only place where you modify your views. For what you're trying to do, I think the easiest thing to do would be to keep track of the last position that was tapped, and then have onBindViewHolder()
set the background to black or white based on whether its position equals the last tapped position.
@Override
public void onItemClick(View v, int position) {
lastTappedPosition = position;
mAdapter.notifyDataSetChanged();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position == lastTappedPosition) {
// set color selected
} else {
// set color unselected
}
...
}
You could improve upon this by only notifying the adapter that the old tapped position and the new tapped position have changed (so that you don't have to re-bind every view), but in the interest of simplicity I left it as-is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With