如何为listview中的每一行设置不同的背景颜色?

时间:2012-05-07 08:42:58

标签: android listview

我想在listview的每一行设置不同的背景颜色?我为listview使用了自定义适配器。它应该出现在活动loads.static不同的颜色行时。

4 个答案:

答案 0 :(得分:13)

getView(...) method

中的

if (position == 0) {
    view.setBackgroundResource(R.drawable.bg_list_even);
} else if (position == 1) {
    view.setBackgroundResource(R.drawable.bg_list_odd);
} else...

<强>更新::

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.row, null);

        holder = new ViewHolder();
        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.title = (TextView) view.findViewById(R.id.txttitle);
    holder.description = (TextView) view.findViewById(R.id.txtdesc);

    holder.title.setText("Title" + position);
    holder.description.setText("Desc" + position);

    //here set your color as per position

    if (position == 0) {
        view.setBackgroundResource(R.drawable.bg_list_even);
    } else if (position == 1) {
        view.setBackgroundResource(R.drawable.bg_list_odd);
    }
    return view;
}

持有人类

public class ViewHolder {

    public TextView title;
    public TextView description;
}

答案 1 :(得分:5)

如下所示制作一个数组,如果没有列表项,我想你有五个项目

 int[] color_arr={Color.BLUE,Color.CYAN,Color.DKGRAY,Color.GREEN,Color.RED};

以及在你的getConnector方法之后执行如下的

 public View getView(int position, View convertView, ViewGroup parent)
     {

     LayoutInflater inflater = getLayoutInflater();
     View row=convertView;

     row = inflater.inflate(R.layout.listview_custome, parent, false);
     row.setBackgroundColor(color_arr[position]);// this set background color

     TextView textview = (TextView) row.findViewById(R.id.tv_list);
     ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);

     textview.setText(data_text[position]);
     imageview.setImageResource(data_image[position]);

     return (row);

    }

答案 2 :(得分:4)

正如您所说,您已经使用自定义适配器进行列表视图,那么您需要做的就是下面的内容。 在适配器的getView方法中,您需要设置列表行xml的父视图的背景颜色。

答案 3 :(得分:1)

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = getLayoutInflater();
    View rowView = convertView;

    rowView = inflater.inflate(R.layout.listview_custome, parent, false);
    rowView.setBackgroundColor(color_arr[position]);// this set background color

    TextView textview = (TextView) rowView.findViewById(R.id.tv_list);
    ImageView imageview = (ImageView) rowView.findViewById(R.id.iv_list);

    textview.setText(data_text[position]);
    imageview.setImageResource(data_image[position]);
    if (position == 0) {
        rowView.setBackgroundColor(Color.BLUE);
    }
    else if (position % 2 == 1) {
        rowView.setBackgroundColor(Color.RED);
    }
    else if (position % 2 == 0) {
        rowView.setBackgroundColor(Color.BLUE);
    }
    return (rowView);

}