ListView控件 - getView被称为太多时间 [英] ListView - getView is called too much times

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

问题描述

我知道有关于这个问题的几个问题getView叫几次,但我的问题是有点不同。

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

我有一个自定义的ListView自定义行(使用row_layout.xml)。 它通常效果很好。在开始的时候,我曾与多个呼叫的问题getView并将其固定用的,我看到这里的计算器的方法之一。 (使用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).

我会告诉我的code:

I will show my code:

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?

推荐答案

报价的Andr​​oid工程师的 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天全站免登陆