一个ArrayAdapter的getView()方法的说明 [英] Explanation of the getView() method of an ArrayAdapter

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

问题描述

你能解释的 getView()方法的 ArrayAdapter

我阅读文档,它有三个参数:

  • 位置:该项目适配器的数据集内的位置 我们希望其查看的项目。
  • convertView :老观点重用,如果可能的话。注意:您应该检查这个观点是在使用前非空一个合适的类型和。如果它是不能转换该视图以显示正确的数据,该方法可以创建新的视图。
    异构列表可以指定自己的视图类型数量,因此,这种观点是正确的类型始终(见getViewTypeCount()和getItemViewType(INT))。
  • :父,这种观点最终会被连接到

我明白了位置参数。就像他们说,这意味着该项目的位置,谁查看已要求。

在哪里 convertView 从何而来。我见过很多例子,他们检查 convertView 为空。如果是为null,它们膨胀的排布置一个新的实例,填充它,然后返回。我想我已经得到了我的头周围的太多,但有一件事还是令我感到困惑。什么是通过 convertView 参数传入的布局。如果是<一个href="http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20int%29">the 资源参数传递初始化的 ArrayAdapter 时?是由 getView()

返回的最后一个布局的缓存副本

和最后。什么是参数做。我还没有看到利用这种例子太多了。他们大多简单地重用/充气行布局并返回。

(我问,因为我有一个上单击动画在我的的ListView 具体的这一项,旨在复制Spotify的下拉快捷操作菜单的。我的动画已经有点低迷。一会儿诊断这个问题后,我'已经意识到,这是由于我的 getView()方法需要一点时间来完成的,我在充气每次迭代一个新的行布局的事实。有人建议缓存在行布局 ViewHolder ,而其他的例子指出,即重复使用 convertView 参数只充气行布局如果 convertView 为空。)

解决方案
  

时是getView()?返回的最后一个布局的缓存副本

convertView 是一排的左边屏幕(所以它不是由 getView 法)。例如,该列表首先显示,在这种情况下 convertView ,没有行的观点是previously建成并离开屏幕。如果向下滚动,行0将离开屏幕(将不可见了),当这种情况发生的的ListView 可以选择保留这一观点在缓存以后使用它(这是有道理的,作为一个的ListView 的行一般只用数据是不同的相同的布局)。保持高速缓存的一些看法,并随后将其原因是因为 getView 方法可每次调用了很多次(用户滚动向上/向下和新行出现在屏幕上)。如果每行的时间视图将需要重新创建这将导致大量的正在创建这是一件好事,以避免物体。在你的 getView 方法,你会检查 convertView 来看看是不是。如果是那么您必须建立一个新的行视图和数据填充它,如果它不是的ListView 提供了你一个previous视图。有了这个previous视图意味着你不需要建立一个新的行布局,而不是您必须用正确的数据填充,作为缓存的观点有仍连接在其旧的数据(你会看到很多关于计算器的问题,用户问他们的的ListView为何行当他们向下滚动正在复制)。

  

这是什么parent参数做。我还没有看到太多的例子   利用这一点。他们大多简单地重用/充气连续布局,   返回。

它应该被用来以获得正确的的LayoutParams 为新充气/建行。例如,如果膨胀的布局具有 RelativeLayout的为根,你不要使用来获得的LayoutParams 你可以有一些问题,该行布局。要利用家长在考虑应该这样做:

  convertView = getLayoutInflater()膨胀(R.layout.row_layout,父母,假)。
 

Could you explain the getView() method of an 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

I understood the position parameter. Like they said, it's imply the position of the item, who view has been requested.

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()?

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.

(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.)

解决方案

Is is a cached copy of the last layout returned by 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 must build 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 must populate 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).

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 :

convertView = getLayoutInflater().inflate(R.layout.row_layout, parent, false);

这篇关于一个ArrayAdapter的getView()方法的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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