gpt4 book ai didi

java - ListView - getView 被调用太多次

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:09 24 4
gpt4 key购买 nike

我知道关于“getView 被调用了几次”这个问题几乎没有问题,但我的问题有点不同。

我有一个带有自定义行的自定义 listView(使用了 row_layout.xml)。它通常运作良好。一开始我遇到了多次调用 getView 的问题,后来使用我在 stackoverflow 中看到的其中一种方法解决了这个问题。 (使用“usedPositions”数组)。

现在,我在日志中看到这种情况:getView pos 0、getView pos 1、getView pos 0、getView pos 1。这导致我的行数增加了一倍。只有当我调用一个涵盖当前 Activity 的新 Activity 然后关闭该 Activity 时,它才会发生。 (例如,打开相机 Activity ,然后将其关闭)。

我将展示我的代码:

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
Toast toast = Toast.makeText(this.context, "getView " + position, 1000);
toast.show();
String pos = Integer.toString(position);
if (!usedPositions.contains(pos)) {

CardHolder holder = null;

if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new CardHolder();
//holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtCouponTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtBusinessName = (TextView)row.findViewById(R.id.txtBusinessName);
row.setTag(holder);
}
else
{
holder = (CardHolder)row.getTag();
}

Card card = data.get(position);
holder.txtCouponTitle.setText(card.couponTitle);
holder.txtBusinessName.setText(card.businessName);
//holder.imgIcon.setImageResource(card.icon);

TableLayout table = (TableLayout)row.findViewById(R.id.imagesTable);
for (int r=1; r<=1; r++){
TableRow tr = new TableRow(this.context);
for (int c=1; c<=10; c++){
ImageView im = new ImageView (this.context);
im.setImageDrawable(this.context.getResources().getDrawable(c<= card.numberOfStamps ? R.drawable.stamp_red :R.drawable.stamp_grey));
im.setPadding(6, 0, 0, 0); //padding in each image if needed
//add here on click event etc for each image...
//...
tr.addView(im, 40,40);
}
table.addView(tr);
}

// Your code to fill the imageView object content
usedPositions.add(pos); // holds the used position
}
else
usedPositions.remove(pos);

return row;
}

你能告诉我哪里出了问题吗?

最佳答案

Quoting安卓工程师RomainGuy

This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times.

所以你能处理的最好的是re-using the existing views (行布局)正确。

Here is another好帖子。

关于java - ListView - getView 被调用太多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9157523/

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