1

我有一个从数据库内容填充的列表视图,为此我编写了一个自定义数组适配器。我需要列表视图中的每个项目在一侧都有独特的颜色作为丝带。与以下照片平面设计的部分相同

但我的清单有 20 项因此滚动。当我向下和向上滚动时,颜色的位置会发生变化。当 rowItem.getIds() 等于 0 或 1 或 2 时,它只是第一次进入 switch/case 时发生这种情况。我只是看到第一个 System.out.println 每次都带有“UP”字符串滚动,但其他(其他 system.out.println 没有“UP”)只有一次。为了清除,我附上了我的代码:

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
String fontType = "";
float fontSize;
int resourceID;

int colour = 0;
int dif = 0;

public CustomListViewAdapter(Context context, int resourceId,
        List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
    resourceID = resourceId;

}

private class ViewHolder {
    TextView txtTitle;
    View img;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    System.out.println("UP " + rowItem.getIds() + "  " + rowItem.getTitle());

    Typeface type;

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.indexitem_row, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView
                .findViewById(R.id.textOfRowWithImage);

        if (colour == 0)
            switch (rowItem.getIds()) {
            case 0:
                holder.img = convertView.findViewById(R.id.sidebar);
                holder.img.setBackgroundColor(Color.rgb(52, 73, 92));
                System.out.println(rowItem.getIds() + "  "
                        + rowItem.getTitle());
                break;
            case 1:
                holder.img = convertView.findViewById(R.id.sidebar);
                holder.img.setBackgroundColor(Color.rgb(244, 179, 80));
                System.out.println(rowItem.getIds() + "  "
                        + rowItem.getTitle());
                break;
            case 2:
                holder.img = convertView.findViewById(R.id.sidebar);
                holder.img.setBackgroundColor(Color.rgb(92, 151, 191));
                System.out.println(rowItem.getIds() + "  "
                        + rowItem.getTitle());
                break;

            }
        else
            holder.img.setBackgroundColor(colour);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();



    holder.txtTitle.setTextSize(fontSize);

    holder.txtTitle.setText(rowItem.getTitle());

    return convertView;
}



public void setSidebarColor(int i) {
    colour = i;
}
   }

行项目类:

public class RowItem {
    private String title;
    private String desc;
    int numbers;

    public RowItem(String title, int i) {
        this.title = title;
        this.numbers = i;
    }

    public RowItem(String title, String desc) {
        this.title = title;
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getTitle() {
        return title;
    }

    public Integer getIds() {
        return numbers;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        if (desc != null)
            return title + "\n " + desc;
        else
            return title;
    }

}

在主类中,我将 rowItem 如下

rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < poemtype.size(); i++) {
            RowItem item = new RowItem(poemtype.get(i), i);
            rowItems.add(item);
        }

我在使用 rowItem.getIds() 之前使用了位置,但结果相同,我尝试了“rowItem.getIds()”,因为我理解位置取决于上下移动获取变化,并且是显示在设备上的项目的位置。

4

1 回答 1

2

此问题是由于视图回收ListView. 每当项目被滚动出屏幕时,它们的视图被放置在回收器中,并再次getView()作为convertView参数传递给适配器。这是性能优化。

这样做的结果是您必须“完全重置”您在 中自定义的任何视图属性getView()。否则,将显示回收视图中的属性。

简而言之,您应该更改img always的背景颜色,而不仅仅是 when convertView == null。像这样的东西:

if (convertView == null)
{
    convertView = mInflater.inflate(R.layout.indexitem_row, null);
    holder = new ViewHolder();
    holder.img = convertView.findViewById(R.id.sidebar);
    // ... assign other holder fields.
    convertView.setTag(holder);
}
else
    holder = (ViewHolder) convertView.getTag();

int color = getColorForRow(position);
holder.img.setBackgroundColor(color);
// continue ...
于 2014-06-29T08:04:51.467 回答