在pagedListAdaper中添加边界回调

时间:2017-12-15 10:51:30

标签: android

我正在使用房间和实时数据在适配器中显示数据,但我想在没有旧消息的情况下从服务器下载数据。我想知道如何在实时数据或pagedList中添加setBoundryCallback

1 个答案:

答案 0 :(得分:0)

您可能已经解决了,但是这里是您可能遇到的问题的解决方案:

  1. 为列表中的元素定义一个BoundaryCallback(在这种情况下,我将其命名为Object)

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()
   }
}
  1. 在您的存储库或定义了页面列表的任何地方,将setBoundary添加到您的呼叫中

您的存储库.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()