How to prevent RecyclerView element from blinking after notifyItemChanged (pos)?

I currently have a recycler view whose data is updated every 5 seconds. To update the data in the list, I use

notifyItemChanged(position);
notifyItemRangeChanged(position, mList.size());

Each time I call notifyItemChanged (), the elements in my recycler view update properly, however, it will flash because it calls onBindViewHolder to call again. So, as if it is a fresh load every time. How can I prevent this, if possible?

+18
source share
5 answers

RecyclerView has a built-in animation that usually adds a nice, honed effect. in your case, you will want to disable them:

((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

( SimpleItemAnimator)

+42

.

mRecyclerView.setItemAnimator(null);
+19

, , .

, :

- setHasStableIds (true)
RecyclerView.Adapter setHasStableIds (true); true , . , , .

- getItemId (int position)
getItemId (int position), long . , .

.

+2

, .

  • .
  • animateChange() 3 ,

    final float prevAlpha = oldHolder.itemView.getAlpha();
    .
    .
    oldHolder.itemView.setAlpha(prevAlpha);
    .
    .
    newHolder.itemView.setAlpha(0);
    
  • .

//: , , - , , stackoverflow, - . , -.

+1

stableId .

adapter.setHasStableIds(true) getItemId(int position) .

Also, return some unique identifier from getItemId(int position)for each item. Do not just return the position.

0
source

Source: https://habr.com/ru/post/1670475/


All Articles