How to delete/remove PagedListAdapter item

前端 未结 4 942
终归单人心
终归单人心 2021-02-12 18:15

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 18:49

    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

    0 讨论(0)
  • 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<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)

    0 讨论(0)
  • 2021-02-12 19:00

    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)
            }
    
    0 讨论(0)
  • 2021-02-12 19:04

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

    0 讨论(0)
提交回复
热议问题