If we use DiffUtil.Callback
, and do
adapter.setItems(itemList);
diff.dispatchUpdatesTo(adapter);
how can we make sure that ad
There's an easy way to do this that also preserves the user's scroll position if items are inserted outside the viewable area:
import android.os.Parcelable;
Parcelable recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
// apply diff result here (dispatch updates to the adapter)
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
Using this approach, the new items are made visible if they are inserted where the user can see them -- but the user's viewpoint is preserved if the items are inserted outside of view.