Is there any way to redraw all items of RecyclerView
?
I have some Themes (in style.xml) and after changing the theme, I need the RecyclerView
to be redrawn.
So I want a method that will force to re-call onCreateViewHolder
for each items of the adapter.
I tried to:
adapter.notifyDataSetChanged
but onCreateViewHolder
is not calledrecyclerView.setVisibility(View.GONE)
and then recyclerView.setVisibility(View.VISIBLE)
recyclerView.invalidate()
recyclerView.setAdapter(null)
and then recyclerView.setAdapter(adapter)
.I mention that the RecyclerView
is attached to an Activity, not to a Fragment.
addItemDecoration(headerDecoration); The decoration is also reusable since there is no need to modify the adapter or the RecyclerView at all.
notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.
getItemCount() - returns The number of items currently available in adapter. This method returns the size of the collection that contains the items we want to display.
I found the answer! The correct way to do this is:
recyclerView.setAdapter(null); recyclerView.setLayoutManager(null); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(myLayoutManager); myAdapter.notifyDataSetChanged();
After that, all the items are getting the new style!
Simply setting recyclerview's adapter again worked for me (I wanted RecyclerView to redraw all items' layout again)
/** * Forces RecyclerView adapter to call createViewHolder() - i.e. redraw all all items' layouts */ private fun resetAdapterState() { val myAdapter = recyclerView.adapter recyclerView.adapter = myAdapter }
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