I am using the Android Arch PagedListAdapter
class. The issue I have run into is that since my app is a chat style app, I need to scroll to position 0 when an item is inserted. But the PagedListAdapter finds the diffs and calls necessary methods on the background thread so it seems that there is no dependable way to call layoutManager.scrollToPosition(0)
If anyone has any ideas on how I could scroll at the right time, it would be greatly appreciated. Thanks!
Here is my Room Dao:
@Dao
abstract class MessagesDao {
@Query("SELECT ...")
abstract fun getConversation(threadId: String): DataSource.Factory<Int, Conversation>
}
and then the LivePagedListBuilder:
LivePagedListBuilder(database.messagesDao.getConversation(threadId), PagedList.Config.Builder()
.setInitialLoadSizeHint(20)
.setPageSize(12)
.setEnablePlaceholders(true)
.build()).build()
It turns out that there is this beautiful thing called AdapterDataObsever which lets you observe the PagedListAdapter item calls. Here is how I was able to use it to solve the problem.
conversationAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
manager.scrollToPosition(0)
}
}
})
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