我正在使用房间和实时数据在适配器中显示数据,但我想在没有旧消息的情况下从服务器下载数据。我想知道如何在实时数据或pagedList中添加setBoundryCallback
答案 0 :(得分:0)
您可能已经解决了,但是这里是您可能遇到的问题的解决方案:
MyBoundaryCallback.kt
class MyBoundaryCallback(val context: Context): PagedList.BoundaryCallback<Object>(){
// This is called when you reach the end of the list currently in the database.
// You should make the call to the server here
override fun onItemAtEndLoaded(itemAtEnd: Object) {
Toast.makeText(context, "Reached the end of the list", Toast.LENGTH_SHORT).show()
}
// This is called when no data is in the database
override fun onZeroItemsLoaded() {
Toast.makeText(context, "0 items loaded", Toast.LENGTH_SHORT).show()
}
}
您的存储库.kt
// somewhere in the code
val factory: DataSource.Factory<Int, Object> = myDao.getObjects()
val pagedListBuilder: LivePagedListBuilder<Int, Object> = LivePagedListBuilder<Int, Object>(factory, 10)
.setBoundaryCallback(JokeBoundaryCallback(context!!))
.build()