gpt4 book ai didi

android - android getView() 如何在 BaseAdapter 中工作?

转载 作者:太空狗 更新时间:2023-10-29 13:56:32 26 4
gpt4 key购买 nike

我读过一些文章并看过video为了理解 ListView 是如何工作的。因此,假设我有包含 5 个项目的列表(所有项目都在屏幕上可见)。我的自定义适配器中有以下代码,它继承自 BaseAdapter:

@Override
public View getView(int position, View view, ViewGroup parent) {
System.out.println("item position: " + position + " view " + view);

if (view == null)
{
view = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
}
return view;
}

当我运行这段代码时,我在控制台中看到以下输出:

08-26 14:52:40.882: item position: 0 view null
08-26 14:52:40.883: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.883: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{88bc756 V.E...C.. ......I. 0,0-1080,65 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{1264e60 V.E...C.. ......I. 0,68-1080,133 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{36b089 V.E...C.. ......I. 0,136-1080,201 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{e1a12bb V.E...C.. ......I. 0,204-1080,269 #7f0e00b8 app:id/item_container}
08-26 14:52:40.885: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}

I getView() viewnull 仅对于 0 位置。观看之后,我认为前 5 次调用的 null(直到所有可见项目 View 未初始化)。为什么只针对第一个?我缺少什么?

最佳答案

ListView 回收 View ,因此第一次运行它将是 null 因为您必须膨胀 View 。膨胀 View 后,您应该重用相同的 View 并仅更新内容以提高性能。我建议观看以下视频 https://www.youtube.com/watch?v=wDBM6wVEO70 ,他们很好地解释了 ListView 的工作原理。

关于android - android getView() 如何在 BaseAdapter 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com