3

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?

Community
  • 1
  • 1
david.mihola
  • 12,062
  • 8
  • 49
  • 73
  • Partial answer: there is no race condition as everything related to the UI is run on the main/UI thread. – clemp6r Oct 05 '16 at 09:42
  • Thanks for the comment! However, Yigit's comment leads me to think that, even though everything happens on the main thread, *the order in which things happen* might still be undefined. Or can I be sure that all scrolling will happen after the View updates scheduled by `notify*` are carried out? – david.mihola Oct 05 '16 at 13:13

0 Answers0