Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListAdapter submitList() - How to scroll to beginning

I have an issue with my RecyclerView and ListAdapter.

Through an API, I receive items in a ascending order from older to newer.

My list is then being refreshed by calling the submitList(items) method.

However, since everything is asynchronous, after receiving the first item, the RecyclerView remains on the position of the first item received and showed. Since in the ListAdapter class there is no callback when the submitList() method completed, I cannot find a way to scroll after the updates to one of the new items that has been added.

Is there a way to intercept when ListAdapter has been updated ?

like image 798
gbaccetta Avatar asked Nov 11 '18 12:11

gbaccetta


1 Answers

Not specific to ListAdapter, but it should work nonetheless:

Just use adapter.registerAdapterDataObserver(RecyclerView.AdapterDataObserver) and override the relevant onItemXXX method to tell your RecyclerView to scroll to whatever position.

As an example:

    adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {         override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {             (recycler_view.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(positionStart, 0)         }     }) 
like image 138
David Liu Avatar answered Oct 22 '22 01:10

David Liu