0

We cache the views in ViewHolder so that too many findViewById() calls could be avoided. In the same way, whether data can be cached in RecyclerView Adapter?

I heard that views are cached in ViewHolder but I am not sure whether data is cached when we use Adapter? If data caching is not provided by RecyclerView Adapter, application can still be crashed if too many calls to data sources occurs?

halfer
  • 19,824
  • 17
  • 99
  • 186
Rahul Raj
  • 3,197
  • 5
  • 35
  • 55
  • Views are recycled. check this https://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works. Data has to be got from some source either from network or locally. Your adapter is a bridge between your data and reyclerview. You can lazy load the data – Raghunandan Sep 14 '17 at 17:17
  • @Raghunandan, Thanks for the response. I do have a question though. Who takes the responsibility of lazyloading? Is that Adapter itself? If yes, how it is done? – Rahul Raj Sep 14 '17 at 17:20
  • read https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView – Raghunandan Sep 14 '17 at 17:22
  • Thanks again for the much useful link. One last question. As of my understanding, lazy loading refers to load the data only when it is really required. So, if we have enormous data to be displayed, it makes sense to use lazy loading and display only few of them at a time. But my concern is -> I want to reduce data source calls as much as possible by caching already retrieved data (some of them) – Rahul Raj Sep 14 '17 at 17:30
  • 2
    if you want some caching then use for example `android.util.LruCache`, the docs say: *"A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection."* – pskink Sep 14 '17 at 17:32
  • @pskink Alright, so caching implementation would depend on kind of data, right? lazy loading along with a cache implementation should be the best way to go, right? – Rahul Raj Sep 14 '17 at 17:36
  • yes, if you have enormous data... – pskink Sep 14 '17 at 17:37
  • 1
    yes. you can implement your own caching mechanism or use third party libs. If your getting data from network you could also paginate the same. – Raghunandan Sep 14 '17 at 17:37

2 Answers2

0

What exactly you mean my by cache if you meant in the Sense of Image you can Use library like Picasso and perform like this and cache Images.
and if you Mean cache in the sense of data your List or Array is already Working as cache.
Only visible Views in ListView have Data Rest is Imaginary only data reside within the List or array!

phpdroid
  • 1,642
  • 1
  • 18
  • 36
-2

No You can't

In Android RecyclerView, only the number of views which will be visible are created. It is handled by the onCreateViewHolder() method.

And when the user scrolls the list, the views which go out of the visible area are reused by populating it with new data. This operation is carried out in the onBindViewHolder(ViewHolder viewholder, int position) method.

As views are recycled for new data, there is no point of caching the data in viewholders.

By the way if you look at onCreateViewHolder(ViewGroup parent, int viewType), you will notice that there is no position parameter. which means you will not even be able to link your list item data, because the ViewHolder is going to be used for multiple positions in the list.

AbdulAli
  • 555
  • 3
  • 9
  • **No You can't** - so you are saying that **data** cannot be cached? notice that OP did not mention anything on caching the data in viewholders – pskink Sep 14 '17 at 18:00
  • How will you know which data is to be cached in the viewholder, when you dont know the position of the viewholder when it is created – AbdulAli Sep 14 '17 at 18:14
  • nobody wants to cache the data in viewholder, read it again: `I heard that views are cached in ViewHolder but not sure whether data is cached when we use Adapter?` – pskink Sep 14 '17 at 18:15
  • viewholder is just like any other java class, and you can store any data you want. But I'm saying what's the point when you don't know for which position the viewholder is going to be used for when it is created. – AbdulAli Sep 14 '17 at 18:20
  • sure i can store any data but neither *i* nor *OP* wants to do that - again: he does not want to cache any data in viewholder - he is asking if the data is automagically cached in the **adapter** – pskink Sep 14 '17 at 18:22
  • @AbdulAli I talk about RecyclerView Adapters, nothing on ViewHolders. ViewHolder just recycle the views and data is coming through **adapter**, but that was not my concern. – Rahul Raj Sep 14 '17 at 18:28
  • Please not that viewholder contains the reference of views. So when you set data to a view for eg setting a textview, the data is stored in the textview. So indirectly viewholder stores the data set on the view – AbdulAli Sep 14 '17 at 18:30
  • @RahulRaj Remember viewholder is the backbone of recyclerview and its adapter. – AbdulAli Sep 14 '17 at 18:31
  • @AbdulAli What a viewholder does, thats not my concern, please take your time to read my question properly. – Rahul Raj Sep 14 '17 at 18:32
  • @RahulRaj Well adapter is normally initialized with the list data. So the data is cached in the adapter. – AbdulAli Sep 14 '17 at 18:35
  • 2
    @RahulRaj i mentioned that you dont want to cache the data in viewholders 3 times already but AbdulAli does not seem to listen... ;-( – pskink Sep 14 '17 at 18:35