【问题标题】:After Scroll background and images are shuflling in Listview在 Listview 中滚动背景和图像后
【发布时间】:2015-01-30 07:19:06
【问题描述】:

当我滚动我的动态数据时,我的动态数据正在随机播放。任何方式我都可以阻止它。我也尝试过视图持有者,但没有运气。我做错了什么。一些帮助会很好。在此先感谢

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

    TextView title, title_sub1, title_sub2, sub1, sub2;

       ImageView img, lock;
    boolean isenable = false;
    try
    {
        if (convertView == null)
        {

            convertView = RelativeLayout.inflate(context, R.layout.protection_home, null);

        }
        int label;
        int icon;
        if (mFeatures != null)
        {
            label = mFeatures.get(position).label;
            icon = mFeatures.get(position).icon;
            isenable = mFeatures.get(position).isenable;
        } else
        {
            label = item[position];
            icon = itemimg[position];
        }

        title = (TextView) convertView.findViewById(R.id.pro_title);
        title_sub1 = (TextView) convertView.findViewById(R.id.sub_title1);
        title_sub2 = (TextView) convertView.findViewById(R.id.sub_title2);
        img = (ImageView) convertView.findViewById(R.id.icon);
        sub1 = (TextView) convertView.findViewById(R.id.title1_status);
        sub2 = (TextView) convertView.findViewById(R.id.title2_status);
        lock = (ImageView) convertView.findViewById(R.id.lock);
        title.setText(label);
        img.setImageResource(icon);
        title_sub1.setText(Setsubtitle1(label));
        title_sub2.setText(Setsubtitle2(label));

        sub1.setText("OFF");
        sub2.setText("ON");
        if (isenable == false)
        {
            convertView.setBackgroundResource(R.color.layout_default_bg_color_gray);
        } else
        {
            convertView.setBackgroundResource(R.color.layout_default_bg_color_white);
        }
        convertView.setId(label);
    } catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return convertView;
}

【问题讨论】:

标签: java android android-listview


【解决方案1】:

在 else 语句中使用 holder 并将 convertView 的标签设置为 holder 对象

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView title, title_sub1, title_sub2, sub1, sub2;
        ImageView img, lock;
        boolean isenable = false;
        Holder holder = null;

        try
        {
            if (convertView == null)
            {
                convertView = RelativeLayout.inflate(context, R.layout.protection_home, null);
                holder = new Holder();
                holder.title = (TextView) convertView.findViewById(R.id.pro_title);
                holder.title_sub1 = (TextView) convertView.findViewById(R.id.sub_title1);
                holder.title_sub2 = (TextView) convertView.findViewById(R.id.sub_title2);
                holder.img = (ImageView) convertView.findViewById(R.id.icon);
                holder.sub1 = (TextView) convertView.findViewById(R.id.title1_status);
                holder.sub2 = (TextView) convertView.findViewById(R.id.title2_status);
                holder.lock = (ImageView) convertView.findViewById(R.id.lock);

               convertView.setTag(holder);

            }  else {
                holder = (Holder) convertView.getTag();
            }

            int label;
            int icon;
            if (mFeatures != null)
            {
                label = mFeatures.get(position).label;
                icon = mFeatures.get(position).icon;
                isenable = mFeatures.get(position).isenable;
            } else
            {
                label = item[position];
                icon = itemimg[position];
            }


            holder.title.setText(label);
            holder.img.setImageResource(icon);
            holder.title_sub1.setText(Setsubtitle1(label));
            holder.title_sub2.setText(Setsubtitle2(label));

            holder.sub1.setText("OFF");
            holder.sub2.setText("ON");
            if (isenable == false)
            {
                convertView.setBackgroundResource(R.color.layout_default_bg_color_gray);
            } else
            {
                convertView.setBackgroundResource(R.color.layout_default_bg_color_white);
            }
            convertView.setId(label);
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return convertView;
    }
    }

【讨论】:

  • 已编辑答案..抱歉错过了里面的 convertView.setTag(holder) if (convertView == null) {}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多