我想用唯一的背景图像识别在我的列表视图中添加的新行,并且旧记录采用默认背景。我正在使用 listadaper,我怎样才能做到这一点?
我的部分代码如下:
if(position==0){
holder.row.setBackgroundResource(R.drawable.layer_selected);
holder.text.setTextColor(Color.BLACK);
} else if(position>0){
holder.row.setBackgroundResource(R.drawable.layer_unselected);
holder.text.setTextColor(Color.WHITE);
}
public View getView(final int position, View convertView, ViewGroup parent) {[
final ViewHolder holder;[
if (convertView==null) {
convertView=newView(position, parent);
holder = new ViewHolder();
holder.remove=(ImageView)convertView.findViewById(R.id.remove);
holder.row=(ImageView)convertView.findViewById(R.id.row);
holder.text=(TextView)convertView.findViewById(R.id.text);
holder.layer=
(RelativeLayout)convertView.findViewById(R.id.layer);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(String.valueOf(position+1));
holder.remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("position remove: "+ position+1);
main.removeItem(position+1);
}
});
if(position+1>main.items.size()-1)
{
holder.row.setBackgroundResource(R.drawable.layer_unselected);
holder.text.setTextColor(Color.WHITE);
}
pos = position+1;
holder.row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
main.selectItem(position+1);
holder.row.setBackgroundResource(R.drawable.layer_selected);
holder.text.setTextColor(Color.BLACK);
}
});
return(convertView);
}
private View newView(int position, ViewGroup parent) {
return(main.getLayoutInflater().inflate(R.layout.layer_row, parent, false));
}
class ViewHolder {
ImageView remove, row;
TextView text;
RelativeLayout layer;
}
其中“row”是我的 ImageView,“text”是以 imageview 为中心的文本。