To update the list in RecyclerView I know three ways.

  1. notifyDataSetChanged ()

  2. notifyItemChanged ()

  3. Use diffutils.

Who can give advice in what cases, what method is more effective to use. And if there are any other ways.

    1 answer 1

    notifyDataSetChanged() - forces you to update all the elements of the list, in fact, redraws them all, even if there were no changes to the currently visible items on the screen. Usually used when there are many changed items in the list and the list itself is quite large.

    notifyItemChanged() is the event of changing a specific item in the list, that is, in theory, only the item whose position is passed in the argument is redrawn. The preferred method is when we know exactly which elements in the list have been changed, especially logically, if the element is not currently displayed on the screen, then there will be no redrawing once again.

    DiffUtils - in many sources it is advised to use it instead of notifyDataSetChanged() , but I somehow did not see a big win, on the contrary, the performance dropped. Perhaps not so implemented, I do not argue. But here, in theory, it depends on the volume of the list itself, with a large list, I think it is preferable to notifyDataSetChanged() , since in DiffUtils you still need to compare elementwise.

    • And I didn’t see the difference between notifyItemChanged() and notifyDataSetChanged() at all. If an element is not on the screen, then it will not be redrawn right away - Andrey Mihalev February