Paging3-PagingDataAdapter,How to remove item?

Viewed 5566

Android Paging3 alpha03, How to remove or update item?

Help,please

1 Answers

The only way to update the backing dataset currently is to call PagingSource.invalidate to trigger another REFRESH, which then reloads from current position via PagingSource.getRefreshKey(state).

e.g.,

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()
Related