How to stop blinking on recycler view with architecture components paging library

后端 未结 5 1275
囚心锁ツ
囚心锁ツ 2021-02-05 08:02

I have a chat-like activity where I am using a RecyclerView with a PagedListAdaper to load a bunch of messages. I\'m using a PositionalDataSource to load the data. The loading i

5条回答
  •  不要未来只要你来
    2021-02-05 09:03

    Short answer: make sure to call the callback of PositionalDataSource.loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) synchronously in loadInitial without wrapping it in some kind of asynchronous success handler.

    Explanation:

    Blinking can be caused by asynchronous loads in your initial load i.e. in PositionalDataSource.loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback).

    The following will happen when you do so:

    Your data source gets invalidated which leads to the creation of a new PagedList (when created with LivePagedListBuilder). This newly created paged list will be passed to your adapter but it is still empty because you didn't call the callback directly in your initial load. This will lead to an empty list for as long as it takes for your callback to be called. This ultimately results in a flicker effect.

提交回复
热议问题