我看到的所有使用PagedList的例子都将其与RecyclerView结合使用。我有一个场景,我想使用分页库,但不需要UI。例如,我最初有一个Recyclerview,它通过使用绑定到PagedListAdapter的PagedList来检索数据列表。然后,用户可以删除列表中他们不想要的项目并保存列表。
问题是,列表可能很大,用户不一定要滚动整个列表。当用户去保存他们的选择时,我需要再次运行生成RecyclerView项目的查询,只有这一次,这些项目由我的ViewModel或其他业务对象层处理。用户可以点击后退按钮并离开他们开始保存的屏幕。他们可以在储蓄还没完成的时候这样做。因此,保存此数据不能绑定到UI,因为一旦活动或片段被破坏,分页数据将不再被接收。
我看到的所有示例都使用带有观察者的LiveData。由于根据要保存的项目数,保存可能需要很长时间,因此必须成批检索所有记录。根据PagedList文档,loadaround方法应该加载下一批记录。我没能让这个工作起来。所以我有两个问题。如何避免使用LiveData获取批处理记录,以及如何利用加载来加载下一批记录。以下是我的ViewModel中的代码:
fun getConnectionsFromDB(forSearchResults: Boolean): LiveData<PagedList<Connection>> {
return LivePagedListBuilder(connections.getConnectionsFromDB(forSearchResults), App.context.repository.LIST_PAGE_SIZE).build()
}
fun storeGroupConnections(groupId: String, connections: PagedList<Connection>) {
val groupConnections = mutableListOf<GroupConnection>()
connections.forEach { connection ->
if (connection != null)
groupConnections.add(GroupConnection(groupId = groupId, connectionId = connection.rowid))
}
val disposable = Observable.fromCallable { groups.storeGroupConnections(groupConnections) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ },
{ ex ->
App.context.displayErrorMessage(R.string.problem_storing_connections)
},
{
connections.loadAround(groupConnections.size - 1)
}
)
disposables.add(disposable)
}
在我的片段中:
fun storeGroupConnections(groupId: String) {
connectionsViewModel.getConnectionsFromDB(args.forSearchResults).observe(this, Observer { connections ->
groupsViewModel.storeGroupConnections(groupId, connections)
})
}