0

It seems impossible to scroll to the bottom of a scrollview

I have searched a bunch of other SO posts with no luck.

Some solutions completely disable animations which is not what I want, I want to be able to update some cell sizes with animation, so that solution doesn't work

I don't want the scroll to animation so I can't use smoothScrolling either

val layoutManager = LinearLayoutManager(context)
binding.recyclerView.layoutManager = layoutManager

viewModel.liveData.observe(viewLifecycleOwner, Observer { result ->
    result.fold({ conversations ->
        binding.recyclerView.adapter = ConversationsAdapter(conversations)
       
        // Does not work
        binding.recyclerView.scrollToPosition(conversations.count() - 1)

        // Does not work either
        layoutManager.scrollToPosition(conversations.count() - 1)
    },{
        print(it)
    })
})

EDIT: Also tried

binding.recyclerView.viewTreeObserver.addOnGlobalLayoutListener {
    layoutManager.scrollToPosition(conversations.count() - 1)
}
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • Try to call recyclerview.scrollToPosition() instead of layoutmanger.scrollToPosition() method. And remove the listener before scrollToPosition() – AbrahamCuautle Mar 07 '21 at 00:30
  • Is your `RecyclerView` inside `nestedScrollView`? – ADM Mar 07 '21 at 05:20
  • It's not in a nested scrollview – aryaxt Mar 07 '21 at 16:10
  • Move `binding.recyclerView.scrollToPosition(conversations.count() - 1)` inside `binding.recyclerView.post()`. That should work if i understand your problem clearly. – ADM Mar 08 '21 at 06:11

1 Answers1

0

What if you wait for RecyclerView finishes laying down items?

Then scroll to bottom inside of afterMeasured:

viewModel.liveData.observe(viewLifecycleOwner, Observer { result ->
    result.fold({ conversations ->
        binding.recyclerView.adapter = ConversationsAdapter(conversations)
       
        recyclerView.afterMeasured {
          binding.recyclerView.scrollToPosition(conversations.count() - 1)
        }  

    },{
        print(it)
    })
})
AbrahamCuautle
  • 149
  • 1
  • 4