java ArrayAdapter 的 getView() 方法说明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12400338/
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
Explanation of the getView() method of an ArrayAdapter
提问by Mridang Agarwalla
Could you explain the getView()
method of an ArrayAdapter
.
你能解释getView()
的方法ArrayAdapter
。
I read the docs and it has three parameters:
我阅读了文档,它有三个参数:
position
: The position of the item within the adapter's data set of the item whose view we want.convertView
: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)).parent
: The parent that this view will eventually be attached to
position
: 项目在我们想要查看的项目的适配器数据集中的位置。convertView
: 旧视图重用,如果可能的话。注意:在使用之前,您应该检查此视图是否为非空且类型合适。如果无法转换此视图以显示正确的数据,则此方法可以创建一个新视图。
异构列表可以指定其视图类型的数量,以便此视图始终是正确的类型(请参阅 getViewTypeCount() 和 getItemViewType(int))。parent
:此视图最终将附加到的父级
I understood the position
parameter. Like they said, it's imply the position of the item, who view has been requested.
我理解position
参数。就像他们说的那样,这意味着已请求查看的项目的位置。
Where does convertView
come from. I've seen many examples where they check if convertView
is null. If is is null, they inflate a new instance of the row layout, populate it and return it. I guess I've gotten my head around that too but one thing still baffles me. What is the layout that is passed in through the convertView
parameter. Is if the resource
parameter that is passed in when initialising the ArrayAdapter
? Is is a cached copy of the last layout returned by getView()
?
哪里convertView
来的。我看过很多例子,他们检查是否convertView
为空。如果 is 为空,它们会膨胀行布局的一个新实例,填充它并返回它。我想我也明白了这一点,但有一件事仍然让我感到困惑。通过convertView
参数传入的布局是什么。是否是初始化时传入的resource
参数ArrayAdapter
?是getView()
?返回的最后一个布局的缓存副本?
And lastly. What does the parent
parameter do. I haven't seen too many examples utilising this. Most of them simply reuse/inflate a row layout and return it.
最后。什么是parent
参数做。我还没有看到太多使用它的例子。他们中的大多数只是重用/膨胀行布局并返回它。
(I'm asking because I have an on-click animation in my ListView
. Specifically this one that aims to replicate Spotify's dropdown quick action menu. My animation has been a little sluggish. After diagnosing this issue for a while, I've realized that this has been due to the fact that my getView()
method takes a bit of time to complete as I'm inflating a fresh row layout in every iteration. Someone suggested caching the row layout in a ViewHolder
while other examples point to reusing the convertView
parameter i.e. only inflating a row layout if convertView
is null.)
(我问,因为我有一个上点击动画在我的ListView
。特别是这一次,旨在复制Spotify的下拉快捷操作菜单。我的动画已经有点呆滞。诊断这个问题一段时间后,我已经意识到这是因为我的getView()
方法需要一些时间来完成,因为我在每次迭代中都在膨胀一个新的行布局。有人建议缓存行布局,ViewHolder
而其他示例指向重用convertView
参数,即只膨胀一个如果convertView
为空,则行布局。)
回答by Luksprog
Is is a cached copy of the last layout returned by getView()?
是 getView() 返回的最后一个布局的缓存副本吗?
The convertView
is the view of a row that left the screen(so it isn't the last view returned by the getView
method). For example, the list is first shown, in this case convertView
is null
, no row view was previously built and left the screen. If you scroll down, row 0 will leave the screen(will not be visible anymore), when that happens the ListView
may choose to keep that view in a cache to later use it(this makes sense, as the rows of a ListView
generally have the same layout with only the data being different). The reason to keep some views in a cache and later use them is because the getView
method could be called a lot of times(each time the user scrolls up/down and new rows appear on the screen). If each time the row view would need to be recreated this would have resulted in a lot of objects being created which is something to avoid. In your getView
method you would check convertView
to see if it is null
. If it's null
then you mustbuild a new row view and populate it with data, if it isn't null
, the ListView
has offered you a previous view. Having this previous view means you don't need to build a new row layout, instead you mustpopulate it with the correct data, as that cached view has the old data still attached to it(you would see a lot of questions on stackoverflow where users ask why the rows of their ListView
are duplicating when they scroll down).
本convertView
是一排屏幕左侧(所以它是不会被返回的最后一个视图的视图getView
方法)。例如,首先显示的列表,在这种情况下convertView
是null
,没有行视图以前建成并离开屏幕。如果向下滚动,第 0 行将离开屏幕(将不再可见),当发生这种情况时,ListView
可能会选择将该视图保留在缓存中以供以后使用(这是有道理的,因为 a 的行ListView
通常具有相同的布局,只有数据不同)。将某些视图保留在缓存中并在以后使用它们的原因是getView
方法可以被调用很多次(每次用户向上/向下滚动并且屏幕上出现新行时)。如果每次都需要重新创建行视图,这将导致创建大量对象,这是需要避免的。在你的getView
方法中,你会检查convertView
它是否是null
. 如果是,null
那么您必须构建一个新的行视图并用数据填充它,如果不是null
,则为ListView
您提供了以前的视图。拥有这个先前的视图意味着您不需要构建新的行布局,而是必须使用正确的数据填充它,因为该缓存视图仍然附加了旧数据(您会在 stackoverflow 上看到很多问题用户问为什么他们的行ListView
向下滚动时正在重复)。
What does the parent parameter do. I haven't seen too many examples utilising this. Most of them simply reuse/inflate a row layout and return it.
父参数有什么作用。我还没有看到太多使用它的例子。他们中的大多数只是重用/膨胀行布局并返回它。
It should be used to get the correct LayoutParams
for the newly inflated/built row. For example, if you inflate a layout which has a RelativeLayout
as the root and you don't use the parent
to get the LayoutParams
you could have some problems with the row layout. To take the parent in consideration you would use :
它应该用于LayoutParams
为新膨胀/构建的行获取正确的值。例如,如果您对以 aRelativeLayout
为根的布局进行膨胀,并且不使用parent
来获取LayoutParams
,则行布局可能会出现一些问题。要考虑到父母,您将使用:
convertView = getLayoutInflater().inflate(R.layout.row_layout, parent, false);
回答by Xono
My understanding of convertView
is that it's essentially views that have been recycled because they're not being used at the moment - for example, you scroll down the list, the ones at the top aren't on the screen, so they get passed into this parameter for use when you need a new view (so you don't have to create a whole new one while having unused ones sitting around idle). iOS has a similar method called dequeueReusableCellWithIdentifier
. If each row of your listview has the same structure, it's safe to cast this to the appropriate type and just update the information in it - text, images, etc. It will be a View that was previously returned by a getView()
call for the same list.
我的理解convertView
是,它本质上是被回收的视图,因为它们目前没有被使用——例如,你向下滚动列表,顶部的那些不在屏幕上,所以它们被传递到这个当您需要一个新视图时使用的参数(因此您不必在闲置未使用的视图时创建一个全新的视图)。iOS 有一个类似的方法叫做dequeueReusableCellWithIdentifier
. 如果列表视图的每一行都具有相同的结构,则可以安全地将其转换为适当的类型并更新其中的信息 - 文本、图像等。这将是之前通过getView()
调用相同列表返回的视图.
My best guess (and it is admittedly a guess) with parent
is that it's the view that this adapter's list is a child of. It gives you a route back to the rendering system if you need a context, access to the resource system, to pass information to or receive information from the list's parent view.
我最好的猜测(并且无可否认是猜测)parent
是该适配器的列表是其子项的观点。如果您需要上下文、访问资源系统、将信息传递到列表的父视图或从列表的父视图接收信息,它会为您提供返回渲染系统的路由。