Android:使用基本适配器时列表视图的替代颜色
Android: Alternate colours for list-view when using Base Adapter
您好,我有一个列表视图,我想为行提供替代颜色,但无法实现,尤其是当我使用基本适配器时。
下面是我的 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;
}
View view = adapter.getView(position - 1, convertView, parent);
然后设置此视图的背景
然后return这个视图
你必须写上面的行而不是
return adapter.getView(position - 1, convertView, parent);
在自定义适配器的 getView() 中添加以下代码,使列表视图具有不同的颜色
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;
并像这样在您的自定义适配器中声明一个静态内部 class
static class ViewHolder {
TextView tv1;
TextView tv2;
TextView tv2;
}
您好,我有一个列表视图,我想为行提供替代颜色,但无法实现,尤其是当我使用基本适配器时。
下面是我的 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;
}
View view = adapter.getView(position - 1, convertView, parent);
然后设置此视图的背景
然后return这个视图
你必须写上面的行而不是
return adapter.getView(position - 1, convertView, parent);
在自定义适配器的 getView() 中添加以下代码,使列表视图具有不同的颜色
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;
并像这样在您的自定义适配器中声明一个静态内部 class
static class ViewHolder {
TextView tv1;
TextView tv2;
TextView tv2;
}