0

我正在尝试为每一行获得不同的颜色

如果我滚动得太快,那么我可能会连续得到 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;
      }
    } 

我已经尝试了几种修复方法和方法来解决这个问题,但都没有成功。有任何想法吗?

4

2 回答 2

1
if(position%2 == 0){
    holder.layout.setBackgroundResource(R.drawable.selector_listview_grey);
}
else{
    holder.layout.setBackgroundResource(R.drawable.selector_listview_white);
}
于 2013-03-19T11:44:31.280 回答
0
if (position%2 == 0) {
     view.setBackgroundColor(Color.WHITE);

} else {
     view.setBackgroundColor(Color.BLACK);
}
于 2013-03-19T11:43:05.983 回答