我有一个从数据库内容填充的列表视图,为此我编写了一个自定义的arrayadapter。我需要列表视图中的每个项目在一侧都具有独特的颜色,如功能区。与下一张照片的下半部分相同
Flat Design

但我的清单有20个项目,因此已滚动。当我上下滚动时,颜色的位置会发生变化。这是在rowItem.getIds()等于0或1或2的情况下第一次进入切换/大小写时发生的。我每次都只看到第一个System.out.println带有“ UP”字符串滚动,但其他(不带“ UP”的其他system.out.println)仅滚动了一次。

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


RowItemClass:

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()”,因为我知道位置取决于上下移动的变化,并且是显示在设备上的项目的位置。

最佳答案

此问题是由于ListView导致的视图回收。每当将项目滚动到屏幕外时,它们的视图就会放在回收站中,并作为getView()参数再次传递到适配器的convertView。这是性能优化。

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

简而言之,您应始终更改img的背景颜色,而不仅是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 ...

09-10 22:59