2012-05-07 22 views
9

Liste görünümünün her bir satırında farklı arka plan rengi ayarlamak istiyorum? listview için özel adaptör kullandım. Etkinlik loads.tatic farklı renk satırı olduğunda görünmelidir. getView(...) methodListe görünümünde her satır için farklı arka plan rengini nasıl ayarlayabilirim?

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

Güncelleme ::

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

tutucu sınıfı

public class ViewHolder { 

    public TextView title; 
    public TextView description; 
} 

cevap

3

yılında

+0

ne söylüyorsun ben söylüyorsun. –

+0

Sanırım @Samir güncellemesini kontrol etmeniz gerekiyor ve cevabınızı alacaksınız. – Herry

+0

o (Samir) zaten kod ile cevap ver. – Herry

13

Eğer liste görünümü için kullanım Özel adaptörü olup olmadığını söylediği gibi n yapmanız gereken aşağıda. getView bağdaştırıcınızın yöntemi, xml'in ana görünümünde liste sıranızın arka plan rengini ayarlamanız gerekir. Liste öğesinin hiçbir şekilde aşağıda verildiği gibi

+0

bu aşağıda custome adaptörünün ur GetView yönteminde yaptıktan sonra benim için çalışmıyor. Hiç işe yaramıyor: – cesards

+0

benim için iyi çalışıyor… –

+0

Şuna bak: S http://imageshack.us/photo/my-images/99/listrow.png/ – cesards

5

bir dizi olun iu beş ürün

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

var ve diyelim

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

    } 
+0

Değerli öneriniz için çok teşekkürler. –

+0

Teşekkürler, Varsa daha fazla 5 ürün ve renk sadece 5, nasıl mümkün olabilir? –

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

} 
İlgili konular