Categories
android android-recyclerview

RecyclerView – get all existing views/viewholders

I want to update the RecyclerView while it’s displaying data, in my case, I show images with or without labels.

Defaultly I set the visibility of the label when I create the view holder and that’s fine, but I want the user to change the labels visibility through the menu while the RecyclerView is shown, so I want to manually update the visibility for all existing views in the RecyclerView.

Can I somehow get all existing Views? I need all, not only the visible ones, I don’t want that a later recycled View is not updated…

In adapter class:

Boolean isVisible = false;
public CustomAdapter(boolean isVisible) {
this.isVisible= isVisible;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
...
if (isVisible){
//setVisibility(View.VISIBLE)
}else{
//setVisibility(View.INVISIBLE
}
}
public void updateVisibility(boolean newValue){
isVisible= newValue;
}

In Activity where you want to update the value where the adapter is instantiated:

adapter.updateVisibility(false);
adapter.notifydataSetChanged();