How to prevent RecyclerView item from blinking after notifyItemChanged(pos)?
I currently have a recycler view whose data updates every 5 secs. To update the data on the list, I am using
notifyItemChanged(position);
notifyItemRangeChanged(position, mList.size());
Each time I call notifyItemChanged(), the items on my recycler view update properly, however, it will blink because this causes onBindViewHolder to be called again. So it's as though it is a fresh load each time. How can I prevent this from happening, if possible?
Solution 1:
RecyclerView has built in animations which usually add a nice polished effect. in your case you'll want to disable them:
((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
(The default recycler view animator should already be an instance of SimpleItemAnimator)
For Kotlin,
(mRecyclerView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
Solution 2:
You can disable the item animations.
mRecyclerView.setItemAnimator(null);
Solution 3:
The problem may not come from animation but from not stable id of the list item.
To use stable IDs, you need to:
- setHasStableIds(true)
In RecyclerView.Adapter, we need to set setHasStableIds(true); true means this adapter would publish a unique value as a key for item in data set. Adapter can use the key to indicate they are the same one or not after notifying data changed.
- override getItemId(int position)
Then we must override getItemId(int position), to return identified long for the item at position. We need to make sure there is no different item data with the same returned id.
The source of solution for that is here.