我正在使用ArrayAdapter使用viewholder填充列表视图。 我按照Android Alternate row Colors in ListView中的答案,但在第5行和第6行,第10行和第11行,第15行和第16行得到相同的颜色,依此类推。这是我在ArrayAdapter类
中的getView方法代码@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.vaccine_row, parent, false);
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.BLUE);
}
vh = new ViewHolder();
vh.brandnametv = (TextView) convertView.findViewById(R.id.vaccinebrand);
vh.genericnametv = (TextView) convertView.findViewById(R.id.vaccinegeneric);
vh.companytv = (TextView) convertView.findViewById(R.id.company);
vh.schedule1tv = (TextView) convertView.findViewById(R.id.schedule1);
vh.schedule2tv = (TextView) convertView.findViewById(R.id.schedule2);
vh.abbreviation = (TextView) convertView.findViewById(R.id.abbreviation);
vh.vaccinetypeandroute = (TextView) convertView.findViewById(R.id.vaccinetypeandroute);
convertView.setTag(vh);
} else vh = (ViewHolder) convertView.getTag();
vaccineItem di = itemsArrayList.get(position);
vh.brandnametv.setText(di.getBrandname());
vh.genericnametv.setText(di.getGenericname());
vh.companytv.setText(di.getCompany());
vh.schedule1tv.setText(di.getSchedule1());
vh.schedule2tv.setText(di.getSchedule2());
vh.abbreviation.setText(di.getAbbreviation());
vh.vaccinetypeandroute.setText(di.getVaccinetypeandroute());
return convertView;
}
static class ViewHolder {
private TextView brandnametv;
private TextView genericnametv;
private TextView companytv;
private TextView schedule1tv;
private TextView schedule2tv;
private TextView abbreviation;
private TextView vaccinetypeandroute;
}
答案 0 :(得分:0)
你需要这样做
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.BLUE);
}
在if (convertview == null)
块之后,否则在重用时,convertView背景颜色将不会被更改