32

I am getting the new list with new item at the start in my live data and then using its data to update the adapter

viewModel.myLiveData.observe { this, Observer { myList -> 
        adapter.submitList(myList) 
        recyclerView.scrollToPosition(0)
}
0

5 Answers 5

68

submitList does its work on a background thread, so there will always be race conditions that delays can't solve. Fortunately, we can use a RecyclerView.AdapterDataObserver callback to be informed when the list calculations are complete:

yourRecyclerViewAdapter.registerAdapterDataObserver(object: RecyclerView.AdapterDataObserver() {
    override fun onChanged() {
        recycler_view_list.scrollToPosition(0)
    }
    override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
        recycler_view_list.scrollToPosition(0)
    }
    override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
        recycler_view_list.scrollToPosition(0)
    }
    override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
        recycler_view_list.scrollToPosition(0)
    }
    override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
        recycler_view_list.scrollToPosition(0)
    }
    override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
        recycler_view_list.scrollToPosition(0)
    }
})

viewModel.myLiveData.observe { this, Observer { myList -> 
        adapter.submitList(myList) 
}
3
  • 2
    Nice solution, thanks! But I had to skip the first call of onItemRangeInserted() to avoid losing scroll position on rotation.
    – gmk57
    Apr 1, 2020 at 14:46
  • This is genius man Thanks .Is there any better solution like optimized one to do the same . Aug 1, 2021 at 11:17
  • This worked better than the other solutions.
    – M.Ed
    Jan 24, 2022 at 7:41
4

Use this way

viewModel.myLiveData.observe { this, Observer { myList -> 
    adapter.submitList(myList) // Assuming you are notifying adapter by notifydatasetchanged()
    recyclerView.post { recyclerView.scrollToPosition(0) }
}

Here post is giving UI Thread some time to populate Recycler with new data and then call scrollToPosition.

3
  • 7
    some time -> the post is telling android, add this work to the UI thread work queue. It's not "giving it some time" that is just a side-effect of you posting the work on a list that is already doing things. That's why it may work like this, but be careful, if the data is long or the diff util of the submit list takes longer, this is not going to always behave as you expect. Oct 22, 2018 at 22:47
  • Perfect! Worked out for me.
    – M.Ed
    Jan 24, 2022 at 5:04
  • This is not the solution as it sometimes works and other times it doesn't.
    – Minas Mina
    Jul 27, 2022 at 19:49
2

Here is another option if you are looking to add a smoothscroll.

The ListAdapter class has a commit callback (Runnable) which will delay the post.

public void submitList(@Nullable List<T> list, @Nullable final Runnable commitCallback) {
    mDiffer.submitList(list, commitCallback);
}

This is how I update my list and smoothscroll to a position

viewModel.list.observe(viewLifecycleOwner, {
    adapter.submitList(it) {
        recycler.post { recyclerJobs.smoothScrollToPosition(0) }
    }
})
1
0

You can also use

recyclerView.smoothScrollToPosition(0);

viewModel.myLiveData.observe { this, Observer { myList -> 
        adapter.submitList(myList) 
         recyclerView.smoothScrollToPosition(0);
}
0

Thank you everyone for response. But the only work that did worked for me was to place a 1 second delay after executing submitList. Even though not appropriate one, But works for the following reason:

adapter.submitList() will always take the same execution time (within milliseconds)

I just called

Handler().postDelayed ({
  recycler.scrollToPosition(0)
}, 1000)
2
  • 3
    Instead of One second delay recyclerView.post { recyclerView.scrollToPosition(0) } this will be better. You can try this. Also this will not create Handler() each time your data update. It will queue scroll task to same queue RecyclerView using for it's update. Check My answer, This way you will be saving a small amount of memory(To save each Handler created) and Computation(Creating Handler Object) also nearly no visible delay on scrolling.
    – Randheer
    Aug 17, 2018 at 6:48
  • 1
    Don't forgot to accept and upvote answer if it helps.
    – Randheer
    Aug 17, 2018 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.