我正尝试在自定义列表视图中显示详细的产品信息,每行有两个TextViews用于键/值对。数据显示正确。我也为每一条第二条线着色不同。Android - 自定义ListView查看者
还有我的问题。如果我上下滚动,不同颜色的行会改变它们的颜色并保持这种状态。数据不受此问题影响。只是TextViews的背景颜色。我使用ViewHolder模式,但这并没有改变任何东西。我添加了适配器的代码。我觉得那够了。你有什么想法吗?
截图的问题:
代码:
public class ProductDetailAdapter extends BaseAdapter {
private LinkedHashMap<String,String> list;
private Context context;
public ProductDetailAdapter(Context c, LinkedHashMap<String,String> list){
super();
this.context = c;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ProductDetailAdapter.ViewHolder viewHolder;
if(convertView == null){
convertView=inflater.inflate(R.layout.product_detail_data_row,null);
viewHolder = new ViewHolder();
viewHolder.textViewKey = (TextView) convertView.findViewById(R.id.productDataKey);
viewHolder.textViewValue = (TextView) convertView.findViewById(R.id.productDataValue);
convertView.setTag(viewHolder);
}else {
viewHolder = (ProductDetailAdapter.ViewHolder) convertView.getTag();
}
viewHolder.textViewKey.setText((String)list.keySet().toArray()[position]);
viewHolder.textViewValue.setText(list.get(list.keySet().toArray()[position]));
if(position % 2 == 0){
viewHolder.textViewKey.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2));
viewHolder.textViewValue.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2));
}
return convertView;
}
private static class ViewHolder {
public TextView textViewKey;
public TextView textViewValue;
public ViewHolder(){};
}
}
工作图片网址是坏了。请编辑你的问题。谢谢! – aldakur
image url:https://i.stack.imgur.com/OS3Op.png对我来说很好用 – Mike
尝试在if(位置%2 == 0)内创建新的'ViewHolder instance'' – aldakur