当前位置:网站首页>Learn the recycling mechanism of recyclerview again
Learn the recycling mechanism of recyclerview again
2022-07-27 03:21:00 【Drink DDVP alone】
1. background
Previous pair RecyclerView Grasp the status of the recycling mechanism , It's always hazy , Know that there is a four level cache , I also know what level 4 cache is for , Also know where to take it first and then take it from , however , A little more , I don't know why , To put it bluntly, I still haven't learned to understand , So I spent time studying carefully
2. New harvest
There were several misunderstandings before , Including this time, there is a new understanding , I didn't understand until I finished this time , Make a note of
- Not all level 4 caches can be used , For example, you can't use it when sliding
scrapcache , When actually sliding , Generally, only two levels of cache are used mChangedScrapandmAttachedScrapSimilarities and differences
What they have in common is : Recycling without sliding , Save only when rearranging fromRecyclerViewThe separation ofViewHolder, There are screening conditionspositionperhaps id
The difference is :mChangedScrapWhat is kept is : Invalid 、 Not removed 、 Data changes ViewHolder, andmAttachedScrapWhat is kept is : Invalid 、 Not removed 、 The data has not changed ViewHoldermCachedViewsThe default capacity is 2, The screening criteria arepositionperhapsid- The default size of the cache pool is “ Two 5”, Five kinds
itemType, Each of theseitemTypeThere are five by default . UsedSparseArrayThis data type , as follows
public static class RecycledViewPool {
private static final int DEFAULT_MAX_SCRAP = 5;
static class ScrapData {
final ArrayList<ViewHolder> mScrapHeap = new ArrayList<>();
int mMaxScrap = DEFAULT_MAX_SCRAP;
}
SparseArray<ScrapData> mScrap = new SparseArray<>();
}
3. Summarize the recycling principle
3.1 Recovery principle ( Generally, there are two entrances )
stay RecyclerView Rearrange onLayoutChildren() Or fill in the layout fill() When , I'll take what's necessary first item Separate or remove from the screen , And mark , Save to list in , In rearranging , then ViewHolder Take it out and put it in a new position one by one .
(1) If it is RecyclerView Cache without scrolling ( For example, delete item), When you rearrange , Put... On the screen ViewHolder Separate from the screen , Store in Scrap in , That is, what has changed ViewHolder The cache to mChangedScrap in , It doesn't change ViewHolder Store in mAttachedScrap in ; be left over ViewHolder I will follow mCachedViews>RecycledViewPool The priority of is cached to mCachedViews perhaps RecycledViewPool in .
(2) If it is RecyclerVIew Caching in scrolling situations ( Like a slide list ), Fill the layout while sliding , First remove the slide out screen item, First level cache mCachedViews Cache these first ViewHolder, however mCachedViews The maximum capacity is 2, When mCachedViews When it's full , Will use the first in first out principle , Put the old ViewHolder Store in RecycledViewPool in Move back and get rid of , Make room , And then put the new ViewHolder Add to mCachedViews in , The last remaining ViewHolder Will be cached to the ultimate recycle pool RecycledViewPool in , It is based on itemType To cache different types of ArrayList<ViewHolder>, The maximum capacity is 5.
3.2 The principle of reuse ( It's usually 1 An entrance , namely fill() Method )
There are already five caches RecyclerView The pool of ,mChangedScrap、mAttachedScrap、mCachedViews、mViewCacheExtension、mRecyclerPool, except mViewCacheExtension The system is provided for developers to expand, not to use , There are also four pools involved in the reuse process , from fill Method tracking , The core is tryGetViewHolderForPositionByDeadline() Method
(1) When RecyclerView Take a reusable one ViewHolder when , If it's preloading , I'll go first mChangedScrap Find out exactly in ( Respectively according to the position and id) Corresponding ViewHolder, If there is, go back ;
(2) If not, go again mAttachedScrap and mCachedViews Find exactly in ( First position after id) Is it the original one ViewHolder, If it's a statement ViewHolder It was just removed ;
(3) If not , And finally go to mRecyclerPool look for , If itemType Type matching corresponds to ViewHolder, So back to the instance , Let it rebind the data ;
(4) If mRecyclerPool And didn't return ViewHolder Will call createViewHolder() Re create a .
Here we need to pay attention to : stay mChangedScrap、mAttachedScrap、mCachedViews From ViewHolder It's all precision matching , however mChangedScrap What's changed is , Need to call onBindViewHolder() Rebinding data ,mAttachedScrap and mCachedViews Nothing has changed , It's direct use , No need to rebind data , and mRecyclerPool Medium ViewHolder The content information has been erased , You need to rebind the data . So in RecyclerView When you scroll back and forth ,mCachedViews The cache pool is the most efficient .
in general :RecyclerView Focus on the optimization of caching and recycling in two scenarios ,
One is : When the data is updated , Use Scrap Make a partial update , Reuse the original as much as possible viewHolder, Reduce the work of binding data ;
Two is : When sliding , Reuse the original ViewHolder, Minimize duplicate creation ViewHolder And the work of binding data .
The ultimate idea is , You can't create without creating , You can't rebind without rebinding , Minimize duplication of unnecessary work .
4. Problems encountered
Due to the recycling and reuse mechanism , When item Too often, it will be based on itemType Reuse , So when Item In the layout of , One parent container is ConstraintLayout、 For example, the child control is TextVIew,TextView Of width by 0 That is, matching constraints , So when it comes to reuse item When ,width Or match constraints , But at this time, the width may not be enough to display all characters , Resulting in incomplete character display . The reason is , Is when the width is 0 When , Not trigger measure technological process , therefore , Change the width to wrap_content, This problem can be solved .
5. Reference article
( Learn with questions , Explain the recycling and reuse in the sliding scenario )https://www.jianshu.com/p/467ae8a7ca6e
( Very detailed , Highly recommended !!!)https://juejin.cn/post/6984974879296585764#heading-2
边栏推荐
猜你喜欢
随机推荐
Yilingsi T35 FPGA drives LVDS display screen
$128million! IQM, a Finnish quantum computing company, was supported by the world fund
Portraiture5 new and upgraded leather filter plug-in artifact
Boom 3D new 2022 audio enhancement app
Common events of window objects
OpenTelemetry 在服务网格架构下的最佳实践
“满五唯一”和“满二唯一”是什么?有什么不同?
关于OpenFeign的源码分析
2649: segment calculation
Jmeter分布式压测
【flask】服务端获取客户端请求的文件
Best practices of opentelemetry in service grid architecture
客户案例 | 关注老年用户体验,银行APP适老化改造要避虚就实
在线问题反馈模块实战(十五):实现在线更新反馈状态功能
安全员及环保员岗位职责
30 minutes to thoroughly understand the synchronized lock upgrade process
Localstorage and sessionstorage
围圈报数(北理工机试题)(DAY 83)
Activiti5.22.0扩展支持达国产数据库,以GBase据库为例
196. Delete duplicate email addresses
![Analysis of [paper] pointlanenet papers](/img/f6/8001be4f90fe15100e0295de02491f.png)








