This article is reprinted from the WeChat public account "Android Development Programming", the author is Android Development Programming. Please contact the Android Development Programming public account for reprinting this article. PrefaceLearning source code and studying source code programming ideas are the only way for programmers to advance Everyone knows that RecyclerView has a recycling and reuse mechanism, so how does the recycling and reuse mechanism work? Today we will use source code to explain and learn together 1. Recycler IntroductionRecyclerView is a cache managed by the internal class Recycler, so what is cached in Recycler? We know that RecyclerView can still slide smoothly when there is a lot of data, and RecyclerView itself is a ViewGroup, so it is inevitable to add or remove sub-Views when sliding (sub-Views are created through onCreateViewHolder in RecyclerView#Adapter). If the sub-Views have to be recreated every time they are used, it will definitely affect the smoothness of sliding. Therefore, RecyclerView caches ViewHolder (which contains sub-Views) through Recycler, so that sub-Views can be reused when sliding, and under certain conditions, the data bound to sub-Views can also be reused. So in essence, caching is to reduce the time of repeatedly drawing Views and binding data, thereby improving the performance of sliding.
Recycler caches ViewHolder objects at four levels, with the priorities from high to low being: 1. ArrayList mAttachedScrap --- Cache the ViewHolder of the visible range on the screen 2. ArrayList mCachedViews ---- caches the ViewHolder that will be separated from the RecyclerView when sliding, cached by the position or id of the child View, and stores up to 2 by default 3. ViewCacheExtension mViewCacheExtension --- Cache implemented by the developer 4. RecycledViewPool mRecyclerPool --- ViewHolder cache pool, which is essentially a SparseArray, where the key is ViewType (int type) and the value is ArrayList< ViewHolder>. By default, each ArrayList can store up to 5 ViewHolders. 2. Detailed Analysis of Cache MechanismWhen RecyclerView slides, onTouchEvent#onMove is triggered, and the recycling and reuse of ViewHolder will begin here. We know that when setting RecyclerView, we need to set LayoutManager. LayoutManager is responsible for the layout of RecyclerView, including the acquisition and reuse of ItemView. Taking LinearLayoutManager as an example, when RecyclerView is rearranged, the following methods will be executed in sequence: onLayoutChildren(): Entry method for layout of RecyclerView fill(): Responsible for continuously filling the remaining space, the calling method is layoutChunk() layoutChunk(): Responsible for filling the View, which is ultimately found by finding a suitable View in the cache class Recycler The entire call chain above: onLayoutChildren()->fill()->layoutChunk()->next()->getViewForPosition(), getViewForPosition() is to get the appropriate View from the RecyclerView recycling mechanism implementation class Recycler, The following mainly looks at the implementation of Recycler#getViewForPosition().
They will all execute the tryGetViewHolderForPositionByDeadline function and continue to follow it: //Get ViewHolder according to the passed position
The ViewHolder obtained through mAttachedScrap, mCachedViews and mViewCacheExtension does not need to recreate the layout and bind data; the ViewHolder obtained through the cache pool mRecyclerPool does not need to recreate the layout, but needs to rebind the data; if the target ViewHolder is not obtained in the above cache, Adapter#onCreateViewHolder will be called back to create the layout, and Adapter#onBindViewHolder will be called back to bind the data SummarizeThere are two structures involved in the recycling and reuse of RecyclerView in the sliding scenario:
|
<<: Google's Android beta device search feature exposed: supports network and car search platforms
It is learned that the ongoing global semiconduct...
Produced by: Science Popularization China Produce...
What is the price for developing Datong Jewelry M...
Do you have any medicine? Capsules, fish oil, mel...
In five years, Pinduoduo has increased the number...
IT Home reported on June 30 that according to Bei...
A lot has happened in the TV industry over the pa...
Before talking about how to improve the weight of...
New car-making forces that were once considered d...
In this article, the author will share with you s...
gossip We often hear this statement: WHO says wal...
Hollywood nude photo scandal is still brewing The...
When interviewing optimizers, I always ask, "...
Based on his actual work experience, the author o...
On October 17, Google released its third quarter ...