Paging3-PagingDataAdapter,如何删除item? [重复]

问题描述 投票:0回答:1

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

请帮忙

android pagination
1个回答
3
投票

当前更新支持数据集的唯一方法是调用

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()
© www.soinside.com 2019 - 2024. All rights reserved.