0

in my app I'm currently using retrofit and getting a list of movies based on a search query from the user. From there, the user can swipe the movie from the list that comes up to add it to their watchlist. I had this working with a normal RecyclerView and using an ArrayList, but since trying to migrate to the Paging Library, I'm struggling to figure out a clean and easy way to do this.

For the normal recycler view, the code was essentially just:

searchAdapter.removeAt(viewHolder.bindingAdapterPosition)

Is there some kind of similar way to do this for PagingDataAdapter? Like maybe a quick function I can plop in and then call in the onSwiped method? The idea is to be able to swipe it out of the search list since it's now in the watchlist and the user doesn't need to see it anymore. Thank you for any help.

daniel-eh
  • 344
  • 3
  • 13
  • pagingDataAdapter data is immutable. You will have to apply your new list over it. For performance, implement DiffUtil so that the paging list doesn't redraw the row. – Extremis II Jan 05 '21 at 02:27

2 Answers2

0

When using Paging you essentially delegate item additions / removals to RV to the library, so doing this manually is going to cause certain things to break, and won't carry over through invalidation, refresh, etc.

Instead you should cache the data you fetch from network into a place such as Room and then remove the row from DB, relying on invalidation to surface the item deletion in RV.

dlam
  • 3,547
  • 17
  • 20
0
    val element = snapshot().find { it?.id == movieId }
    val index = snapshot().indexOf(element)
    index.let {
        notifyItemRemoved(it)
    }
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – helvete Jun 02 '22 at 14:59