Android 快速上下滚动时,ListView备用行颜色错误
以下问题:在显示项目列表时,我使用自定义ArrayAdapter并实现ViewHolder模式。我也使用ConvertView。 加载时,一切正常,列表项以正确的交替颜色显示 但当我快速上下滚动时,颜色会改变,不再交替。。。 例如加载时:绿色,蓝色,绿色,蓝色。。。 滚动后:绿色,蓝色,绿色,绿色,绿色,绿色 下面是一些代码:Android 快速上下滚动时,ListView备用行颜色错误,android,android-listview,android-adapter,Android,Android Listview,Android Adapter,以下问题:在显示项目列表时,我使用自定义ArrayAdapter并实现ViewHolder模式。我也使用ConvertView。 加载时,一切正常,列表项以正确的交替颜色显示 但当我快速上下滚动时,颜色会改变,不再交替。。。 例如加载时:绿色,蓝色,绿色,蓝色。。。 滚动后:绿色,蓝色,绿色,绿色,绿色,绿色 下面是一些代码: public View getView(int position, View convertView, ViewGroup parent) { Layou
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
StoreTemplate curr = templates.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.template_store_rowlayout,
parent, false);
holder = new ViewHolder();
holder.name = (TextView) convertView
.findViewById(R.id.name);
// more attr skipped here...
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(curr.getName());
// more attr set here...
// this changes background
if ((position % 2) == 0) {
convertView.setBackgroundColor(context.getResources().getColor(
R.color.background_green));
}
// code skipped here
return convertView;
}
我如何保证,即使我滚动或搜索并重新加载列表,行始终是交替着色的 您忘记了更改行背景的
if语句的else
部分。您可能希望这样尝试:
// this changes background
if ((position % 2) == 0) {
convertView.setBackgroundColor(context.getResources().getColor(
R.color.background_green));
} else {
convertView.setBackgroundColor(context.getResources().getColor(
R.color.your_color));
}
我会用另一根树枝把你的非偶数行涂成蓝色。非常感谢。你救了我的时间!