Java 何时调用 onBindViewHolder 以及它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37523308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When onBindViewHolder is called and how it works?
提问by Shubh
I am a beginner and I am having trouble with understanding a piece of code. Can someone please explain me when this function evoke and what is it for?
我是初学者,我在理解一段代码时遇到了麻烦。有人可以解释一下这个功能何时唤起以及它是做什么的吗?
Here is my code :
这是我的代码:
public void onBindViewHolder(myViewHolder holder, int position) {
RecViewHolder currentdata = data.get(position);
holder.favChecker = currentdata.getFavChecker();
holder.serialID = currentdata.getSerialID();
holder.theClassName = currentdata.getTheClassName();
}
采纳答案by Marcin Koziński
Let me start with just a little bit of background (which you may already understand, but it's needed to explain onBindViewHolder()
).
让我从一点背景开始(您可能已经了解,但需要解释一下onBindViewHolder()
)。
RecyclerView
is designed to display long lists (or grids) of items. Say you want to display 100 rows of something. A simple approach would be to just create 100 views, one for each row and lay all of them out. But that would be wasteful, because most of them would be off screen, because lets say only 10 of them fit on screen.
RecyclerView
旨在显示项目的长列表(或网格)。假设你想显示 100 行的东西。一种简单的方法是只创建 100 个视图,每行一个视图并将它们全部布置。但这会很浪费,因为它们中的大多数会在屏幕外,因为可以说只有 10 个适合屏幕。
So RecyclerView
instead creates only the 10 views that are on screen. This way you get 10x better speed and memory usage. But what happens when you start scrolling and need to start showing next views?
因此,RecyclerView
只创建屏幕上的 10 个视图。通过这种方式,您可以获得 10 倍更好的速度和内存使用。但是当您开始滚动并需要开始显示下一个视图时会发生什么?
Again a simple approach would be to create a new view for each new row that you need to show. But this way by the time you reach the end of the list you will have created 100 views and your memory usage would be the same as in the first approach. And creating views takes time, so your scrolling most probably wouldn't be smooth.
同样,一个简单的方法是为您需要显示的每个新行创建一个新视图。但是这样当您到达列表末尾时,您将创建 100 个视图,并且您的内存使用量将与第一种方法相同。创建视图需要时间,因此您的滚动很可能不会顺畅。
This is why RecyclerView
takes advantage of the fact that as you scroll and new rows come on screen also old rows disappear off screen. Instead of creating new view for each new row, an old view is recycledand reused by binding new data to it.
这就是为什么RecyclerView
利用这样一个事实,即当您滚动并且新行出现在屏幕上时,旧行也会从屏幕外消失。不是为每个新行创建新视图,而是通过将新数据绑定到旧视图来回收和重用旧视图。
This happens exactly in onBindViewHolder()
. Initially you will get new unused view holders and you have to fill them with data you want to display. But as you scroll you'll start getting view holders that were used for rows that went off screen and you have to replace old data that they held with new data.
这恰好发生在onBindViewHolder()
. 最初你会得到新的未使用的视图持有者,你必须用你想要显示的数据填充它们。但是当您滚动时,您将开始获得用于离开屏幕的行的视图持有者,您必须用新数据替换它们持有的旧数据。
回答by Pradeep Gupta
It is called by RecyclerView
to display the data at the specified position. This method is used to update the contents of the itemView to reflect the item at the given position.
调用它RecyclerView
以在指定位置显示数据。此方法用于更新 itemView 的内容以反映给定位置的项目。
for more info check RecyclerView.Adapter#onBindViewHolder
欲了解更多信息,请检查 RecyclerView.Adapter#onBindViewHolder