Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

android - How can I set different background color for each row in listview?

I want to set different background color in each row of listview ? I used custom adapter for listview. It should be appear when activity loads.static different color row.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

in getView(...) method

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

Update::

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

holder class

public class ViewHolder {

    public TextView title;
    public TextView description;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...