Is is possible to have race conditions between the notify*
methods of a RecyclerView.Adapter
and scrollToPosition
(and smoothScrollToPosition
) of the RecyclerView
itself? If so, how can I force the scroll
to happen strictly after the notify
has been applied?
In a bit more detail: I have a RecyclerView
with an adapter that frequently is updated with new items (which may or may not overlap with the previous items). Also, whenever I set new items I also want to set the scroll position to a specific item. To that end, I first update the items inside my Adapter
and then scroll the RecyclerView
.
However, more often than not the scroll position will be wrong after this process. Also, if I then issue another smoothScrollToPosition
command without changing the data, the scrolling is weird: It sometimes goes in the wrong direction, etc. After this second scrolling, the position is always correct however. So, it seems that something goes wrong the first time and the RecyclerView catches and corrects that error on the second scroll.
Also, the errors are slightly different when I use notifyDataSetChanged
from when I use DiffUtil
.
Now I've read in this response by Yigit that notify*
is basically asynchronous, so I suppose there can be a race condition between them and the subseqent scrollToPosition
- is that correct?
Finally what can I do to establish a strict ordering, so that the scroll is only called when all ViewHolder updates triggered by notify are done?