了解RecyclerView setHasFixedSize [英] Understanding RecyclerView setHasFixedSize

查看:189
本文介绍了了解RecyclerView setHasFixedSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解setHasFixedSize()时遇到了一些麻烦.我知道从文档中RecyclerView的大小不变时,它用于优化.

I'm having some trouble understanding setHasFixedSize(). I know that it is used for optimization when the size of RecyclerView doesn't change, from the docs.

那是什么意思呢?在大多数情况下,ListView几乎总是具有固定大小.在什么情况下不是固定大小?这是否意味着它在屏幕上实际占用的房地产会随着内容的增长而增加?

What does that mean though? In most common cases a ListView almost always has a fixed size. In what cases would it not be a fixed size? Does it mean that the actual real estate that it occupies on screen grows with the content?

推荐答案

RecyclerView的非常简化版具有:

A very simplified version of RecyclerView has:

void onItemsInsertedOrRemoved() {
   if (hasFixedSize) layoutChildren();
   else requestLayout();
}

此链接描述了为什么调用requestLayout可能是昂贵的.基本上,无论何时插入,移动或删除项目,RecyclerView的大小(宽度和高度)都可能发生变化,从而视图层次结构中任何其他视图的大小也可能发生变化.如果经常添加或删除项目,这将特别麻烦.

This link describes why calling requestLayout might be expensive. Basically whenever items are inserted, moved or removed the size (width and height) of RecyclerView might change and in turn the size of any other view in view hierarchy might change. This is particularly troublesome if items are added or removed frequently.

在更改适配器的内容而不更改其高度或宽度时,通过将setHasFixedSize设置为true可以避免不必要的布局传递.

Avoid unnecessary layout passes by setting setHasFixedSize to true when changing the contents of the adapter does not change it's height or the width.

更新:JavaDoc已更新,可以更好地描述该方法的实际作用.

Update: The JavaDoc has been updated to better describe what the method actually does.

如果RecyclerView可以知道 提前说明RecyclerView的大小不受适配器影响 内容. RecyclerView仍可以根据其他更改其大小 因素(例如其父母的身高),但此尺寸计算无法 取决于子项的大小或适配器的内容(除 适配器中的项目数).

如果您使用RecyclerView 属于此类别,请将其设置为{@code true}.它将允许 RecyclerView避免当其适配器出现时使整个布局无效 内容更改.

RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent's size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).

If your use of RecyclerView falls into this category, set this to {@code true}. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.

@param hasFixedSize如果适配器更改不能影响的大小,则为true RecyclerView.

@param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.

这篇关于了解RecyclerView setHasFixedSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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