Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for ListAdapter.submitList() to execute and call RecyclerView.smoothScrollToPosition(0);

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.

like image 996
Matt Avatar asked Dec 11 '18 19:12

Matt


People also ask

What is listadapter in recyclerview?

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.

Does listadapter update list items that have changed?

It only update items which has changed (with animation shown). NOTE: Beware of some ListAdapter Caveats.

What is the difference between listadapter and call notifydatasetchanged ()?

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.

How do I get data from a list adapter?

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.


2 Answers

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
        }
    });
like image 135
Chintan Soni Avatar answered Sep 22 '22 09:09

Chintan Soni


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.

like image 37
IdAndro Avatar answered Sep 24 '22 09:09

IdAndro