我正在尝试为每一行获得不同的颜色
如果我滚得太快,那么我可能会连续3行(没有双关语)
public class TestAdapter extends ArrayAdapter<Enlevement> {
private final Activity context;
private boolean shouldCreateGreyBackground = false;
static class ViewHolder {
public TextView text;
public LinearLayout layout;
}
public TestAdapter(Activity context ) {
super(context, 0);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item_enlevement_grey, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.enlevementDate);
viewHolder.layout = (LinearLayout) rowView.findViewById(R.id.layoutEnlevementItemGrey);
rowView.setTag(viewHolder);
ViewHolder holder = (ViewHolder) rowView.getTag();
Enlevement entry = getItem(position);
holder.text.setText(String.valueOf(entry.i));
if(shouldCreateGreyBackground){
holder.layout.setBackgroundResource(R.drawable.selector_listview_grey);
shouldCreateGreyBackground = false;
}
else{
holder.layout.setBackgroundResource(R.drawable.selector_listview_white);
shouldCreateGreyBackground = true;
}
return rowView;
}
}
我已经尝试了几个修复和方法,但没有运气。 任何想法?
答案 0 :(得分:1)
if(position%2 == 0){
holder.layout.setBackgroundResource(R.drawable.selector_listview_grey);
}
else{
holder.layout.setBackgroundResource(R.drawable.selector_listview_white);
}
答案 1 :(得分:0)
if (position%2 == 0) {
view.setBackgroundColor(Color.WHITE);
} else {
view.setBackgroundColor(Color.BLACK);
}