How to delete/remove PagedListAdapter item

拈花ヽ惹草 提交于 2021-01-21 00:09:20

问题


Currently I am using Android Architecture Components for App development everything is working along with paging library Now I want to remove recyclerview Item using PagedListAdapter to populate this we required to add a data source and from data source list is updating using LiveData no I want to remove a item from list notifyItemRemoved() is working from PagedList I am getting this exception:

java.lang.UnsupportedOperationException


java.util.AbstractList.remove(AbstractList.java:638)

回答1:


Since you cannot directly modify the data that you have in the PagedList as I believe its immutable, the key to implementing the removal of items in this situation is maintaining a backing dataset somewhere in your code that represents all the data that you have received so far. An ArrayList worked for me.

When you want to remove an item, remove it from your backing dataset and call invalidate on your data source. This will trigger the loadInitial() call, in which you can pass your backing dataset to the callback.onResult() function. Your PagedListAdapter will use the DiffUtil.ItemCallback you supplied to smartly determine that there has been an update to its data and will update itself accordingly.

This tutorial helped me understand the correct flow to use in order to properly delete an item while using the paging library: https://medium.com/@FizzyInTheHall/implementing-swipe-to-delete-with-android-architecture-components-a95165b6c9bd

The repo associated with the tutorial can be found here: https://gitlab.com/adrianhall/notes-app




回答2:


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<Int, YourModel>
@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<YourModel>() {
    private val requestArray = SparseArray<Disposable>()
    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)




回答3:


Temporary hide item in list by call notifyItemUpdated() base on flags set in Pojo object

if(data?.hasVisible == false) {
            itemBinding.root.visibility = View.GONE
            itemBinding.root.layoutParams = RecyclerView.LayoutParams(0, 0)
        }else{
            itemBinding.root.visibility = View.VISIBLE
            itemBinding.root.layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                RecyclerView.LayoutParams.WRAP_CONTENT)
        }



回答4:


Adapter.notifyItemRemoved(position); appDatabase.userDao().deleteUser(user); It's work for me!



来源:https://stackoverflow.com/questions/48173395/how-to-delete-remove-pagedlistadapter-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!