您好我有一个列表视图,我想为行提供替代颜色,但无法实现它,特别是当我使用基础适配器时。
下面是我的getView()方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
int sectionnum = 0;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;
// check if position inside this section
if(position == 0) return headers.getView(sectionnum, convertView, parent);
if(position < size) return adapter.getView(position - 1, convertView, parent);
// otherwise jump into next section
position -= size;
sectionnum++;
}
if(position % 2 == 0){
view.setBackgroundColor(Color.BLUE);
}else{
view.setBackgroundColor(Color.WHITE);
}
return view;
}
答案 0 :(得分:2)
在自定义适配器的getView()中添加以下代码,以使listview具有替代颜色
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row_layoutan, null);
holder = new ViewHolder();
holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position %2 == 0){
convertView.setBackgroundColor(Color.parseColor("your choice color"));
}else{
convertView.setBackgroundColor(Color.parseColor("your choice color"));
}
return convertView;
并在您的自定义适配器中声明一个静态内部类,如此
static class ViewHolder {
TextView tv1;
TextView tv2;
TextView tv2;
}
答案 1 :(得分:1)
View view = adapter.getView(position - 1, convertView, parent);
然后设置此视图的背景
然后返回此视图
你必须写上面的行而不是
return adapter.getView(position - 1, convertView, parent);