10

リストビューの各行に異なる背景色を設定したいですか?リストビューにはカスタムアダプタを使用しました。アクティビティがloads.staticの異なる色の行のときに表示されるはずです。

4

4 に答える 4

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;
}
于 2012-05-07T08:45:53.277 に答える
5

リストアイテムがないので、以下のように配列を作成します。5つのアイテムがあると思います。

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

そして、以下のようにカスタムアダプタのurgetViewメソッドで実行した後

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

    }
于 2012-05-07T09:13:33.827 に答える
4

リストビューにカスタムアダプタを使用しているとおっしゃったように、次の手順を実行する必要があります。アダプタのgetViewメソッドでは、リスト行xmlの親ビューの背景色を設定する必要があります。

于 2012-05-07T08:47:31.117 に答える
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);

}
于 2015-07-16T10:07:45.720 に答える