How remove/add items from RecyclerView that uses Paging(androidx) library? - android

The problem comes when recycler uses Androidx paging library.
I have implemented drag and swipe to remove, but when user removes some item from list and next User scrolls down and next scroll up deleted items appear again.
How could I remove item from items source? is it possible?
Do I need always call to backend -> remove -> update list?
I have created a wrapper object to manage deleted items, so when user removes any item I mark into the wrapper object deleted=true so when bind function is called I set visibility=GONE if this flag is true an vice-verse.

The only way to remove items in Paging at the moment is to go through an invalidate loop where you essentially ask Paging to reload from last scroll position.
If you are loading directly from network you might be interested in doing this in a layered way with a local cache in DB to make this less expensive via the RemoteMediator API.
For self-updating pages, there is an open bug you may follow for that feature request here: https://issuetracker.google.com/issues/160232968

Related

Refreshing LazyPagingItems when one item state has changed

Basically I have a paging data flow collected in a composable function:
val list = state.listFlow.collectAsLazyPagingItems()
Each item on the list has a call-to-action button that will enable/disable the view and update the UI of the corresponding item.
My question is, how can we update the visual state of the item without the need for calling refresh on the PagingData and therefore re-querying the API/database for updated data?
Also if user has scrolled through 5 pages or more I don't want to reload the whole content, changes can be local.
Any clues on how to achieve this?
This is almost impossible for Paging3, which requires immutable lists. See https://issuetracker.google.com/issues/160232968.
This problem bothered me for months and finally I decided to abandon Paging3 library and write my own paged list to avoid all these weird features. Most of the complexity of Paging3 is because it wants to encapsulate the paged list in one library, which needs the view layer and the model layer to work together.
This is the paged list I implemented: https://gist.github.com/FishHawk/6e4706646401bea20242bdfad5d86a9e
If your project does not have a deep dependency on the paging3 library, I highly recommend you try doing this.

How to add multiple item in PagingData of paging 3 library android?

I would like to add some item in current paging data which already contains items. And shown in the list adapter.
I have tried to insertHeaderItem. But, when adding second item. Header is getting replaced by second item. I want to keep all item in the list. and update that list every time when i add new item.
paingData
.insertHeaderItem(item = sampleViewEvents.comment)
.let {
getCommentList.value = it
}
I doesn't want to call an API to refresh the whole list. It can leads a performance issue.
Thanks in advance
The correct way to add / modify items with Paging is to update the backing dataset and .invalidate(), refreshing the list. This maintains a single source of truth and allows Paging to be aware of the changes you've made.
For performance, you can cache your data into a middle DB / in-memory layer, and load items from network via RemoteMediator. If you do this (which you should if you are concerned about performance), the reload becomes quite trivial. See: https://developer.android.com/topic/libraries/architecture/paging/v3-network-db
If you are interested in updating a specific page, you can follow this FR: https://issuetracker.google.com/160232968.
To be clear, you MUST go through the invalidate loop to update the items loaded by Paging. Not doing it this way is currently completely unsupported. It is the only way to make Paging aware of the changes you've made.

Don't clean up RecyclerView on PagedList invalidation

I am using the Paging library from Android Jetpack to have a paged loading behavior in my RecyclerView. I'm loading directly from network so I don't have any intermediate in-memory cache or database. Whenever something changes I call invalidate() on DataSource (in my case PositionalDataSource) so that the list is refreshed.
What I need is very simple thing - once I call invalidate() the recycler view is fully cleaned up and shows empty data. I need the recycler view to keep the old data and update normally once the new data comes in. In most of the cases the update might be very small, like a button color change in couple of rows, it looks ugly when the RecyclerView shows empty content for couple of seconds while I'm loading data from backend.
Can this somehow be done or is there a conceptual limitation of the current Paging library architecture forcing me to implement my own caching?
calling notifyDataSetChanged instead of invalidate may help as a simple solution.

How to use requestedInitialKey in ItemKeyedDataSource.LoadInitialParams

I am learning android paging library and everything works perfectly. But whenever I invalidate the DataSource, the list is jumping to start position whenever a new item is added to the top of the list. I want to the list to add the item to top. But the list should stay in its current scrolling position. I think I should use LoadInitialParams.requestedInitialKey which is not null when invalidating. But am not sure how to use this key to load data to avoid jumping of list to start position.
I am using firestore database as data source. I found that the requestedInitialKey supplied to the callback is actually the last visible item in the recyclerview in current scrolling position. I have already tried the following to fetch data around the requestedInitialKey
.get().startAt(requestedIntitalKey).limit(25)
.get().endBefore(requestedIntitalKey).limit(25)
.get().startAfter(requestedIntitalKey).limit(25)
.get().endAt(requestedIntitalKey).limit(25)
where 25 is my page size. None of the above solved the problem. Can someone provide me with an example so that the list will stay at its current scrolling position only updating the content without jumping to start?

How to create a Listview which refreshes along with scrolling in android?

I want to create a ListView which gets information from a database using VOLLEY. I want to call the Volley in a loop which will allow the data to come one by one. Is there any way to make a ListView which adds items whenever volley gets a item returned from the database?
You can use some libraries way cooler to top your ListView from android-arsenal.com . here is a list of libraries that are even new https://android-arsenal.com/tag/78?sort=created
You can see a detailed video by Vivz from Slidenerd Pull to refresh tutorial
Add items to your item set, and simply notify the adapter (adapter.notifyDataSetChanged();) that items been changed.
If you going to insert them one by one you might want to look into RecyclerView and use its notify methods like notifyItemInserted. It is specially designed for that and comes with animations to represents an item being inserted/updated/moved/deleted.

Categories

Resources