Reputation: 430
RecyclerView implementations usually have the following approach:
This uses the power of RecyclerView and ViewHolder to display huge datasets efficiently. However, it does not solve the issue of storing the dataset in memory which is what my question is about.
Imagine I have 1 million items in my dataset and have implemented pagination+endless scroll. If I'm on the last page, in order for the RecyclerView to be able to display the items we would have to store all 1 million items in a list in the adapter and use that list with the relevant RecyclerAdapter methods.
Is there a more efficient way of doing that? My initial thoughts were to have some sort of sliding window based approach where we'd keep a fixed number of pages in memory and have a two-way endless scroll implementation where the next/prev page window gets updated as the scroll "viewport" changes while the user scrolls. Unsure if that would affect scrolling performance...
Upvotes: 1
Views: 4204
Reputation: 8191
consider android's paging library
It works in terms of loading data as pages and not just loading everything into memory. When scrolling, it'll load the appropriate pages you tell it to, so it wouldn't have to keep everything in memory, you could pass in appropriate keys for where to retrieve data and the pagination library will handle that for you.
It supports reading from a database as well as to read from the network or a hybrid of the two, so it should be fine for whichever you need
I suppose it's worth saying that, if you already have your own implementation going, you could also try to mimic this behavior. If you have a way of knowing that a user has reached a certain threshold within the data you're displaying, you could load the next/previous page yourself, then just manage the amount of data you keep in memory yourself. one page up/down might be sufficient, anything more you could clear out your own local memory
Upvotes: 3