Android RecyclerView Caching Explained
The caching mechanism of RecyclerView mainly consists of three types of caches:
- View caching: RecyclerView will maintain an internal View cache pool to store already created Item Views, so that they can be quickly retrieved and reused when needed to be displayed again. Different types of Views can be reused by distinguishing their types through the getItemViewType() method.
- Scrap Cache: The scrap cache is a temporary storage space used to hold onto item views that have scrolled out of the screen but have not yet been removed. When an item view scrolls off the screen, the RecyclerView places it in the scrap cache so it can be easily retrieved and reused when needed again.
- ViewHolder cache: ViewHolder cache is an important caching mechanism of RecyclerView, which is used to store the ViewHolder objects that have already been bound with data. When an Item View scrolls off the screen, RecyclerView caches the corresponding ViewHolder object so that when the same data needs to be displayed in another Item View, it can quickly retrieve the ViewHolder object that is already bound with the data, avoiding redundant data binding.
By combining these three caching mechanisms, RecyclerView can efficiently manage and display a large number of Item Views while maintaining a smooth scrolling effect. Developers can manually control the caching mechanism through some methods of RecyclerView, such as the setRecycledViewPool() method which can set the size of the View cache pool, and the setMaxRecycledViews() method which can set the maximum number of ViewHolders to be cached.