的`convertView`中的ListView适配器的目的是什么?目的、适配器、convertView、ListView

2023-09-07 11:29:18 作者:泪未流〃心先碎

在Android中,我通常使用 MyAdapter扩展ArrayAdapter 以创建视图的的ListView ,并作为一个结果,我要重写功能

In android, I usually use MyAdapter extends ArrayAdapter to create view for the ListView, and as a result, I have to override the function

public View getView(int position, View convertView, ViewGroup parent) {
    // somecode here
}

不过,我不知道到底是什么 convertView 做的!有没有人有一个建议?更多的细节,更多的帮助!谢谢!

However, i don't know exactly what convertView and parent do! Does anyone have a suggestion? More detail, more help! Thanks!

推荐答案

从文档,

convertView - 旧视图重用,如果可能的话。注意:您应该检查这个观点是在使用前非空一个合适的类型和。如果它是不能转换该视图以显示正确的数据,该方法可以创建新的视图。

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.

在换句话说,此参数严格用于增加你的适配器的性能。当的ListView 使用了适配器来填补其行,查看 s时,适配器通过调用填充用查看对象中的每个项目 getView()的每一行上。适配器使用 convertView 作为回收旧的方式使用视图对象不再。以这种方式,在的ListView 可以发送适配器老,被显示,而不是每次适配器想要显示实例化一个全新的对象,该对象不再是再生的查看对象一个新的列表项。这是 convertView 参数的目的。

In other words, this parameter is used strictly to increase the performance of your Adapter. When a ListView uses an Adapter to fill its rows with Views, the adapter populates each list item with a View object by calling getView() on each row. The Adapter uses the convertView as a way of recycling old View objects that are no longer being used. In this way, the ListView can send the Adapter old, "recycled" view objects that are no longer being displayed instead of instantiating an entirely new object each time the Adapter wants to display a new list item. This is the purpose of the convertView parameter.