ListView - getView 被调用太多次 [英] ListView - getView is called too much times

查看:27
本文介绍了ListView - getView 被调用太多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道关于多次调用 getView"的问题很少有问题,但我的问题几乎没有什么不同.

I know there are few questions regarding this issue of 'getView called few times' but my problem is little different.

我有一个带有自定义行的自定义列表视图(使用了 row_layout.xml).它通常运作良好.一开始我遇到了多次调用 getView 的问题,它通过使用我在 stackoverflow 中看到的方法之一来修复.(使用usedPositions"数组).

I have a custom listView with custom row ( used a row_layout.xml). It generally works well. At the beginning I had a problem with multiple calls to getView and it was fixed by using one of the methods I saw here in stackoverflow. ( using the 'usedPositions' array).

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

now, I see in the logs this scenario: getView pos 0, getView pos 1, getView pos 0 , getView pos 1. This caused my rows to be doubled. It happens only when I call a new activity that covers the current activity and then close that activity. ( for example, open the camera activity and then close it).

我会展示我的代码:

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;
}

你能告诉我有什么问题吗?

Can you tell me what's wrong?

推荐答案

引用 android 工程师 RomainGuy

Quoting android engineer RomainGuy

这不是问题,绝对不能保证在将调用哪个 getView() 或调用多少次.

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

因此,您可以处理的最好方法是重新使用现有视图(行布局).

So the best you can handle is re-using the existing views (row layouts) properly.

这是另一个好帖子.

这篇关于ListView - getView 被调用太多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆