gpt4 book ai didi

Android:创建 Activity 时系统调用两次getView

转载 作者:行者123 更新时间:2023-11-29 02:02:46 25 4
gpt4 key购买 nike

我有一个自定义列表,其中包含大约 20 个项目,每个项目三行。我用一个 Log 来跟踪 getView 方法是如何被调用的

public View getView(int position, View convertView, ViewGroup parent) {

Log.e("getView", "at position " + position);
View view = convertView;

if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(resourceId, parent, false);
}
// my code here

}

然后,当加载列表项时,我收到了这个日志

"getView", "在位置0";"getView", "在位置1";"getView", "在位置2";"getView", "位置3";"getView", "在位置0";"getView", "在位置1";"getView", "在位置2";"getView", "位置3";"getView", "在位置4";"getView", "在位置5";...............

谁能告诉我为什么 getView 方法从 0 到 3 调用了两次?

最佳答案

Can anyone tell me why getView method called twice from 0 to 3?

getView 方法被调用两次,因为 ListView 作为其 onMeasure 方法的一部分调用了 getView 适配器的方法获取行的 View 以查看它们有多大。我猜您有三个可见行,因此 ListView 将为前三个可见子级调用 getView 方法三次。

关于Android:创建 Activity 时系统调用两次getView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11703555/

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