滚动背景和图像在Listview中显示后

时间:2014-12-02 05:50:00

标签: java android android-listview

当我滚动我的动态数据时,就像洗牌一样。任何方式我都可以阻止它。我也尝试过视图持有人,但没有运气。我做错了。一些帮助会很好。谢谢你提前

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

1 个答案:

答案 0 :(得分:0)

使用持有者并将convertView的标记设置为else语句

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