Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging3-PagingDataAdapter,How to remove item? [duplicate]

Android Paging3 alpha03, How to remove or update item?

Help,please

like image 274
mumu Avatar asked Mar 10 '26 08:03

mumu


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()
like image 67
dlam Avatar answered Mar 12 '26 23:03

dlam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!