簡體   English   中英

如何為listview中的每一行設置不同的背景顏色?

[英]How can I set different background color for each row in listview?

我想在listview的每一行中設置不同的背景顏色? 我使用自定義適配器列表視圖 它應該出現在活動loads.static不同顏色的行時。

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;
}

如下所示制作一個數組作為列表項目的數量我想你有五個項目

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

以及如下所示在客戶端適配器的getView方法之后

 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);

    }

正如您所說,您已經使用自定義適配器進行列表視圖,那么您需要做的就是下面。 在適配器的getView方法中,您需要設置列表行xml的父視圖的背景顏色。

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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM