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
Android phone users should have a habit: when the...
“America’s auto dealers are getting people back t...
Review expert: Ran Hao, a well-known popular scie...
The May Day holiday is approaching, which means i...
Image source: Pixel Addict - CC BY 2.0 A mysterio...
This article will share with you the promotion ex...
Preface I have been recording system sound these ...
As an operator , you must have done this: invite ...
Today, the Changsha epidemic suddenly became a ho...
Recently, fresh food e-commerce has ushered in a ...
Under the influence of new consumption concepts, ...
The new generation of iPhone will have two screen...
Your browser does not support the video tag Septe...
Zhao Dongxuan: New Strategies for Customer Acquisi...
Copywriting with sufficient promotional power wil...