我使用Android Jetpack组件来开发我的聊天应用程序,我想在聊天日志屏幕中添加对分页的支持,因为在从我的数据源获取数据并在RecyclerView中呈现消息时花费了太多时间,所有教程都讨论了底层分页及其工作以及我的其他屏幕,如联系,但我没有找到任何顶级分页的实现,我试图使用PagedList.BoundaryCallback
编写我的实现,但它不起作用。
我的代码:
@MainThread
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
Log.d(MessageBoundaryCallback::class.java.simpleName, "onZeroItemsLoaded()")
helper.runIfNotRunning(PagingRequestHelper.RequestType.INITIAL) {
val db = FirebaseFirestore.getInstance()
db.collection(Constants.FIRE_STORE_MESSAGES_COLLECTION_NAME)
.whereEqualTo(Constants.CHAT_ID, chatId)
.orderBy(Constants.MESSAGE_TIME, Query.Direction.DESCENDING)
.limit(pageSize)
.get().addOnCompleteListener(createMessageCallback(it))
}
}
override fun onItemAtFrontLoaded(itemAtFront: Message) {
super.onItemAtEndLoaded(itemAtFront)
Log.d(MessageBoundaryCallback::class.java.simpleName, "onItemAtEndLoaded($itemAtFront: Message)")
helper.runIfNotRunning(PagingRequestHelper.RequestType.BEFORE) {
val db = FirebaseFirestore.getInstance()
db.collection(Constants.FIRE_STORE_MESSAGES_COLLECTION_NAME)
.whereEqualTo(Constants.CHAT_ID, chatId)
.whereLessThan(Constants.MESSAGE_TIME, itemAtFront.time)
.orderBy(Constants.MESSAGE_TIME, Query.Direction.DESCENDING)
.limit(pageSize)
.get().addOnCompleteListener(createMessageCallback(it))
}
}
我只是反转我的布局管理器
layoutManager.reverseLayout = true
一切都像魅力一样