Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView drawing issues (flicker)

I have Activity1 with a fragment which has a RecyclerView in it. Selecting an item on the list starts Activity2 which modifies the item. Returning back to Activity1 after Activity2 finishes I observe that the RecyclerView first draws the pre-Activity2 state (saved old item state), then in onActivityResult I do restartLoader() which delivers an updated set of data and then RecyclerView redraws itself again with the new data.

The problem with this behavior is that the RecyclerView flickers because it originally draws old data then redraws itself with the new data. Assuming I always want to use refreshLoader() in Activity1.onActivityResult(), is there a good way to avoid such flickering?


Additional complication is that I use empty view to show when recycleview is empty... This adds to flickering even more...

like image 708
Califf Avatar asked Apr 05 '16 20:04

Califf


1 Answers

To remove update flickering on recycler view you can use:

 RecyclerView.ItemAnimator animator = mRecyclerView.getItemAnimator();
    if (animator instanceof SimpleItemAnimator) {
        ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
    }

To update an items in RecyclerView you should use

    notifyItemChanged(int position)

- Notify any registered observers that the item at position has changed.

instead of notifyDataSetChanged()

like image 186
Bogdan Ustyak Avatar answered Oct 06 '22 09:10

Bogdan Ustyak