【问题标题】:Alternate item colors in a ListViewListView 中的备用项目颜色
【发布时间】:2011-12-08 22:13:07
【问题描述】:

我正在尝试更改 android 中列表视图的项目颜色,但无法使其工作。我希望颜色交替变化...我制作了自己的适配器,在 getView 方法中我在返回列表视图之前更改了颜色,但它不起作用我不知道为什么...

这是我的代码:

public class EventoAdapter extends ArrayAdapter<Evento>{
    Context context;
    int layoutResourceId;
    ArrayList<Evento> evento = null;


public EventoAdapter(Context context, int textViewResourceId,
        ArrayList<Evento> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub

    this.layoutResourceId = textViewResourceId;
    this.context = context;
    this.evento = objects;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView;
    EventoHolder holder = null;

    if(row == null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new EventoHolder();
        holder.nombre = (TextView)row.findViewById(R.id.nombreEvento);
        holder.total = (TextView)row.findViewById(R.id.totalEvento);  
        holder.moneda = (TextView)row.findViewById(R.id.moneda);
        holder.fecha = (TextView)row.findViewById(R.id.fecha);
        row.setTag(holder);

    }else{
        holder = (EventoHolder)row.getTag();
    }


    Evento ev = evento.get(position);

    holder.nombre.setText(ev.getNombre());
    holder.total.setText(Integer.toString(ev.getTotal()));
    holder.moneda.setText("$");
    holder.fecha.setText("20/12/11");



    if(position%2==0){
        row.setBackgroundColor(R.color.colorPar);
    }else{
        row.setBackgroundColor(R.color.colorImpar);
    }

    return row;
}



static class EventoHolder{
    TextView nombre;
    TextView total;
    TextView moneda;
    TextView fecha;
}
}

当然,我已经在自己的 colors.xml 资源中定义了 colorPar 和 colorImpar。

知道为什么这不起作用吗?

谢谢!

【问题讨论】:

  • 如果你使用 setBackgroundResource() 代替它是否有效?
  • “不起作用”是什么意思?
  • @gwa 它完美地显示了列表,但始终使用相同的颜色。它不会改变背景颜色...
  • @jave 它与 setBackgroundResource() 一起使用,我不知道有什么区别,但我知道它是正确的!非常感谢!
  • 我会把它作为答案发布,并添加一些解释。

标签: android android-layout


【解决方案1】:

看起来默认的视图背景是透明的。试试这个,它应该可以工作..

if(position%2==0){
    row.setBackgroundColor(new 
       ColorDrawable(context.getResources().getColor(R.color.colorPar)));
}else{
    row.setBackgroundColor(new 
       ColorDrawable(context.getResources().getColor(R.color.colorImpar)));
}

【讨论】:

    【解决方案2】:

    使用setBackgroundResource() 而不是setBackgroundColor()。

    setBackgroundResource() 将整数资源索引作为参数,并加载索引指向的任何资源(例如;可绘制对象、字符串或在您的案例一种颜色)。

    setBackgroundColor(),但是需要一个表示颜色的整数。也就是说,不是 color-resource,而是直接的十六进制 rgba 值 (0xAARRGGBB)。

    因此,当您使用资源索引(例如 7f050001,这是第一个颜色索引)调用 setBackgroundColor() 时,您始终将颜色设置为 a:127 r:5 g:0 b:1。

    【讨论】:

    • setBackgroundColor(getResources().getColor(R.color.mycolor));
    【解决方案3】:

    如果您希望更改一个或两个以上的属性以用于替代或任何其他附加布局,下面的帖子描述了如何根据项目索引应用分离的布局。 只需覆盖getViewTypeCount 和getViewType 方法(或在Xamarin 中ViewTypeCount 属性和GetViewType 方法)

    链接:

    Android ListView with multiple layouts

    https://developer.xamarin.com/api/property/Android.Widget.BaseAdapter.ViewTypeCount/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      相关资源
      最近更新 更多