I have a RecyclerView recyclerView
which is linked to a ListAdapter adapter
. The 'list' gets new data from Firebase database and the 'list' gets sorted based on certain object properties. I then call adapter.submitList(list)
to update the recyclerView - this works perfectly!
But, I want the recyclerView to scroll to the top of the list after the new data is added. But I see that adapter.submitList(list)
runs on a separate thread. So how would I go about knowing when it is done submitting and call recyclerView.smoothScrollToPosition(0);
Let me know if I should provide more details.
ListAdapter is a RecyclerView adapter that displays a list. This is available in RecyclerView 27.1+, and the same function also exists in the AsyncListDiffer class if you cannot extend the adapter. ListAdapter helps you to work with RecyclerViews that change the content over time.
It only update items which has changed (with animation shown). NOTE: Beware of some ListAdapter Caveats.
Calling notifyDataSetChanged () is an option, but it redraws the entire view, even the unchanged parts, which is an expensive operation. ListAdapter handles addition and removal without the need to redraw the entire view, and even animate those changes.
ListAdapter gets data using a method called submitList (), which submits a list to be diffed against the current list and displayed. This means you no longer have to override getItemCount () because ListAdapter manages the list. In the Activity class, call submitList () on the Adapter and pass in the data list.
ListAdapter has got another overloaded version of submitList() ListAdapter#submitList(@Nullable List<T> list, @Nullable final Runnable commitCallback)
that takes second argument as Runnable.
As per the documentation:
The commit callback can be used to know when the List is committed, but note that it * may not be executed. If List B is submitted immediately after List A, and is * committed directly, the callback associated with List A will not be run.
Usage:
listAdapter.submitList(*your_new_list*, new Runnable() {
@Override
public void run() {
// do your stuff
}
});
You can use an AdapterDataObserver on your recyclerview adapter, that will happen after submit finish :
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
when {
positionStart == 0 && itemCount != 0 -> {
mRecyclerView.smoothScrollToPosition(itemCount)
}
positionStart > 0 -> {
mRecyclerView.smoothScrollToPosition(adapter.itemCount)
}
}
}
})
Be careful with positionStart, item count value. There is also other callbacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With