简体   繁体   English

adapter.setHasStableIds(true) 为什么在 RecyclerView 中默认不启用?

[英]adapter.setHasStableIds(true) why it is not enabled by default in RecyclerView?

I can see setting adapter.我可以看到设置适配器。

setHasStableIds(true); setHasStableIds(true);

will improved RecyclerView performance, since it is improving lots of performance, why it is not default enabled in RecyclerView , Is there any limitations while setting setHasStableIds(true) ?将提高 RecyclerView 的性能,因为它提高了很多性能,为什么在RecyclerView中没有默认启用它,在设置setHasStableIds(true)时是否有任何限制?

The reason is when we are going to just displaying the data in recycler-view that case no need to unique id of each item and no animation required.原因是当我们只在回收站视图中显示数据时,不需要每个项目的唯一 ID,也不需要 animation。 so it's default its false .所以它默认为 false

But when we are going to perform some action in the dataset and update the recycler-view that case its need unique id for particular item and refreshing animation (blinking) it need.so we set as true但是当我们要在数据集中执行一些操作并更新回收器视图时,它需要特定项目的唯一 ID 并刷新 animation(闪烁)它需要。所以我们设置为true

The reason why it is not the default is because the data backing the adapter might not have stable Id's and the RecyclerView does not know this.它不是默认值的原因是因为支持适配器的数据可能没有稳定的 Id,而 RecyclerView 不知道这一点。

Yes there is a limitation one using setHasStableIds(true) - your data has to have stable Id's是的,使用setHasStableIds(true)有一个限制 - 您的数据必须具有稳定的 ID

You either need override getItemId(int position) to return a unique Id that would represent each Data Item, simply return the HashCode if you cannot think of anything better.您要么需要覆盖 getItemId(int position) 以返回代表每个数据项的唯一 ID,如果您想不出更好的方法,只需返回 HashCode。

Or或者

If using something like a CursorAdapter this automatic has stable Id's as it uses the database table _id column.如果使用像CursorAdapter这样的东西,这个自动具有稳定的 ID,因为它使用数据库表 _id 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM