Paging3-PagingDataAdapter,如何删除项目?

时间:2020-08-07 15:48:50

标签: android paging

Android Paging3 alpha03,如何删除或更新项目?

帮助,请

1 个答案:

答案 0 :(得分:0)

当前更新后备数据集的唯一方法是调用PagingSource.invalidate来触发另一个REFRESH,然后再通过PagingSource.getRefreshKey(state)从当前位置重新加载。

例如

val backingDataSet = mutableListOf(1, 2, 3)
class MyPagingSource : PagingSource<Int, Int> {
    override suspend fun load(...) = Page(
        data = backingDataset,
        nextKey = null,
        prevKey = null,
        itemsBefore = 0,
        itemsAfter = 0
    )

    override fun getRefreshKey(state: PagingState<Int, Int>) = state.anchorPosition
}

...

var currentPagingSource: MyPagingSource? = null
val pager = Pager(...) {
    MyPagingSource().also{
        currentPagingSource = it
    }
}

...

// To remove an item
backingDataSet.removeAt(0)
currentPagingSource.invalidate()