简体   繁体   English

对于ListView适配器,getView()如何调用?

[英]For a ListView adapter, how is getView() called?

I've looked around quite a bit but haven't found a real answer to this question. 我已经看了很多,但没有找到这个问题的真正答案。 I'm just starting to learn about android adapters and had a few questions. 我刚刚开始学习Android适配器,并有几个问题。 Firstly, do I need to call getView() manually or does it get called automatically? 首先,我需要手动调用getView()还是自动调用? If so, when is it actually called? 如果是这样,它何时被称为? Lastly, I noticed there's a parameter for "position" with in the list. 最后,我注意到列表中有“position”的参数。 Is that something I have to increment through iteration or does it get incremented internally somehow? 这是我必须通过迭代递增还是以某种方式在内部递增?

thanks! 谢谢!

Firstly, do I need to call getView() manually or does it get called automatically? 首先,我需要手动调用getView()还是自动调用?

No, it get called automatically. 不,它会被自动调用。

If so, when is it actually called? 如果是这样,它何时被称为?

It will be called when user do something to your ListView , like when the ListView displaying a new item when you scrolling it. 当用户对ListView执行某些操作时将调用它,例如当ListView在滚动时显示新项目时。

Lastly, I noticed there's a parameter for "position" with in the list. 最后,我注意到列表中有“position”的参数。 Is that something I have to increment through iteration or does it get incremented internally somehow? 这是我必须通过迭代递增还是以某种方式在内部递增?

No, it will be your list item's position automatically. 不,它会自动成为您列表项的位置。 Its the same for other params like (the default) View convertView , it will be the View of current item (row of ListView ). 对于其他类似于(默认) View convertView它,它将是当前项的ViewListView行)。

It's part of how the adapter works. 它是适配器工作方式的一部分。 You don't need to call any of those methods, since it's called implicitly through your Listview. 您不需要调用任何这些方法,因为它是通过Listview隐式调用的。

Have a look at this, 看看这个,

http://www.vogella.com/tutorials/AndroidListView/article.html http://www.vogella.com/tutorials/AndroidListView/article.html

It's a good article on how to use a Listview. 这是一篇关于如何使用Listview的好文章。

Adapter is a kind of connection between AdapterView (ListView, GridView etc.) and arrays of data. Adapter是AdapterView(ListView,GridView等)和数据数组之间的一种连接。 Adapter generates vies that would be displayed in the AdapterView. 适配器生成将在AdapterView中显示的vies。 So when ListView needs to display elements it first calls getCount() method of adapter, after some measurements it makes a few getView() calls to fill visible part of the listView with items. 因此,当ListView需要显示元素时,它首先调用适配器的getCount()方法,经过一些测量后,它会调用一些getView()来填充listView的可见部分。 And then during the scrolling ListView will call getView() from the adapter for the next positions. 然后在滚动期间,ListView将从适配器调用getView()以用于下一个位置。 It is important not to make any assumptions on the order of the getView() calls - it's not obligatory that listview will call getView() in the same order as your data ordered. 重要的是不要对getView()调用的顺序做出任何假设 - listview不一定要按照你订购的数据的顺序调用getView()。 So you don't have to worry about incrementing of position index - this argument is used for situation, for example, when you need to display two different backgrounds for even and for odd rows - and you can use position to make a decision which colour you need to use. 因此您不必担心增加位置索引 - 此参数用于情境,例如,当您需要为偶数行和奇数行显示两个不同的背景时 - 您可以使用position来决定哪种颜色你需要使用。 There is a good presentation about anatomy of the ListView 关于ListView的解剖结构有一个很好的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM