How to delete/remove PagedListAdapter item

前端 未结 4 719
既然无缘
既然无缘 2021-02-12 18:09

Currently I am using Android Architecture Components for App development everything is working along with paging library Now I want to remove recyclerview Item

4条回答
  •  青春惊慌失措
    2021-02-12 19:00

    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.

    1. Reload whole list. Make API call to remove item from server and then call 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.
    2. Store results in Room. Then PagedList will get items directly from Room and PagedListAdapter will manage removing/adding items itself.

    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)

提交回复
热议问题