什么时候使用recyclerView.setHasFixedSize? [英] When do we use the recyclerView.setHasFixedSize?

查看:357
本文介绍了什么时候使用recyclerView.setHasFixedSize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是东西: 有人知道setHasFixedSize方法吗?有人说,如果所有项目的大小都相同,则它可以进行优化,并且在android.support.v7.widget的RecyclerView类中,它对此进行了评论:如果RecyclerView可以事先知道更改的内容,则可以执行多项优化. 适配器内容不能更改RecyclerView本身的大小.如果您对RecyclerView的使用属于此类,请将其设置为true.

那是什么意思?任何人都可以向我展示使用它的上下文,或者在上面解释有关此类别"的含义吗? 非常感谢.

解决方案

对于RecyclerView来说,知道其大小(宽度和高度尺寸)是否取决于适配器的内容是很有趣的,以避免昂贵的布局操作.如果RecyclerView事先知道其大小不依赖于适配器的内容,那么它将在每次添加或从适配器中删除项目时跳过其大小是否应该更改的检查.这尤其重要,因为插入删除元素可能经常发生.

如果RecyclerView的大小(RecyclerView本身)...

... 不依赖:适配器内容

mRecyclerView.setHasFixedSize(true);

... 取决于适配器内容:

mRecyclerView.setHasFixedSize(false);

如果您选中

It's interesting for the RecyclerView to know if its size (width and height dimensions) depends on the adapter content to avoid expensive layout operations. If the RecyclerView knows in advance that its size doesn't depend on the adapter content, then it will skip checking if its size should change every time an item is added or removed from the adapter. This is especially important because inserting an deleting elements can happen very often.

If the size of the RecyclerView (the RecyclerView itself)...

...doesn't depend on the adapter content:

mRecyclerView.setHasFixedSize(true);

...depends on the adapter content:

mRecyclerView.setHasFixedSize(false);

If you check the RecyclerView class you'll see it in more details because as of right now mHasFixedSize isn't used in that many places in that class.

Setting it as true doesn't mean that the RecyclerView size is fixed, just means it won't change because of change in the adapter content. For example the RecyclerView size can change because of a size change on its parent.

这篇关于什么时候使用recyclerView.setHasFixedSize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆