分页库:如何按需重新加载部分数据?

dev*_*ant 8 pagination android android-architecture-components android-paging

我使用Paging Library来分页我的数据集.我要做的是刷新RecyclerView我的数据库中的后数据已被更改.

我有这个LiveData:

val listItems: LiveData<PagedList<Snapshot>> = object : LivePagedListProvider<Long, Snapshot>() {
        override fun createDataSource() = SnapshotsDataSource()
    }.create(null, PagedList.Config.Builder()
        .setPageSize(PAGE_SIZE)
        .setInitialLoadSizeHint(PAGE_SIZE)
        .setEnablePlaceholders(false)
        .build()
    )
Run Code Online (Sandbox Code Playgroud)

而且DataSource:

class SnapshotsDataSource : KeyedDataSource<Long, Snapshot>(), KodeinGlobalAware {

    val db: FirebaseDb = instance()

    override fun getKey(item: Snapshot): Long = item.timestamp

    override fun loadInitial(pageSize: Int): List<Snapshot> {
        val result = db.getSnapshotsTail(pageSize)
        return result
    }

    override fun loadAfter(key: Long, pageSize: Int): List<Snapshot> {
        val result = db.getSnapshotsTail(key, pageSize)
        return result.subList(1, result.size)
    }

    override fun loadBefore(key: Long, pageSize: Int): List<Snapshot> {
        return emptyList()
    }

}
Run Code Online (Sandbox Code Playgroud)

Adapter是直截了当的,所以我在这里省略.

我在修改数据库时尝试这样做:

fun reload(position) {
    listItems.value!!.loadAround(position)
}
Run Code Online (Sandbox Code Playgroud)

但它没有帮助.

小智 5

试着listItems.value!!.datasource.invalidate() 不直接打电话DataSource#invalidate()