2012-05-07 34 views

Trả lời

3

Như bạn nói rằng bạn có sử dụng Tuỳ chỉnh adapter cho listview sự n những gì bạn cần làm là dưới đây. trong phương thức getView của bộ điều hợp, bạn cần đặt màu nền của chế độ xem gốc của hàng trong danh sách xml của bạn.

+0

Tôi không hiểu bạn đang nói gì. –

+0

tôi nghĩ bạn cần kiểm tra cập nhật @Samir và bạn sẽ nhận được câu trả lời của mình. – Herry

+0

anh ta (Samir) đã trả lời bằng mã. – Herry

13

trong getView(...) method

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

Cập nhật ::

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

lớp giữ

public class ViewHolder { 

    public TextView title; 
    public TextView description; 
} 
+0

này không làm việc cho tôi. Nó không hoạt động ở tất cả: – cesards

+0

các công trình của nó tốt cho tôi .... –

+0

Kiểm tra xem nó ra: S http://imageshack.us/photo/my-images/99/listrow.png/ – cesards

5

Thực hiện một mảng như đưa ra dưới đây như là không có các mục trong danh sách tôi giả sử u có năm mặt hàng

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

và sau khi làm trong phương pháp getView ur adapter custome như sau

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

Cảm ơn rất nhiều vì gợi ý có giá trị của bạn. –

+0

Cảm ơn bạn, Nếu có nhiều hơn thì 5 mục và màu sắc chỉ là 5, làm thế nào nó có thể? –

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

} 
Các vấn đề liên quan