Currently I am using Android Architecture Components for App development everything is working along with paging library Now I want to remove recyclerview Item
I had same issue. My PagedList displayed items that DataSource factory fetched from server. I came up with two solutions how to remove item from list.
pagedList.dataSource.invalidate()
.
Downside of this solution is that whole list is cleared and then all items received from server. Not exactly what I was looking for.In DAO object
("SELECT * FROM YourModel")
fun getAll(): DataSource.Factory
@Delete
fun delete(item: YourModel)
To update database as user scrolls list I implemented BoundaryCallback. It is being called when there are no more items to show in DB, it can be called at the end of same page, so I ensured to not execute same request few times (In my case list's key is page number).
class YourModelBoundaryCallback(private val repository: Repository) : PagedList.BoundaryCallback() {
private val requestArray = SparseArray()
private var nextPage = 1
private var lastPage = 1
override fun onZeroItemsLoaded() {
if (requestArray.get(1) == null) {
requestArray.put(1, repository.getFirstPage()
.subscribe({
lastPage = it.total / PAGE_SIZE + if (it.total % PAGE_SIZE == 0) 0 else 1
nextPage++
}))
}
}
override fun onItemAtEndLoaded(itemAtEnd: YourModel) {
if (nextPage > lastPage) {
return
}
if (requestArray.get(nextPage) == null) {
requestArray.put(nextPage, repository.getNextPage(nextPage)
.subscribe({
lastPage = it.total / PAGE_SIZE + if (it.total % PAGE_SIZE == 0) 0 else 1
nextPage++
}))
}
}
override fun onItemAtFrontLoaded(itemAtFront: YourModel) {
// ignored, since we only ever append to what's in the DB
}
}
PagedList instance became this
private val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(3)
.setPageSize(PAGE_SIZE)
.build()
val pagedList = LivePagedListBuilder(yourModelDao.getAll(), pagedListConfig)
.setBoundaryCallback(YourModelBoundaryCallback(repository))
.build()
And finally to remove item from adapter I just call yourModelDao.remove(yourModel)