I have got a ListView and I want to change the Backgroundcolor of it. It should go like this. 1.Item = grey; 2. Item; white; 3. Item = grey; 4. Item = white etc. So it shoul
I believe you can do this based on the position
if (position == 0)
{
view.SetBackgroundColor(Android.Graphics.Color.gray);
}
else if (position == 1)
{
view.SetBackgroundColor(Android.Graphics.Color.white);
}
and so on depending on how many positions you have.
This is another way to change the background using selector switcher. Using this method will preserve the hover and focus colors of the selector.
public View getView(int position, View convertView, ViewGroup parent) {
/* remainder is unchanged */
convertView.setBackgroundResource(position % 2 == 0 ? R.drawable.list_selector_first : R.drawable.list_selector_second);
return convertView;
}
You can do this easily by setting the background inside the getView function of your custom adapter.
Try this code:
if(position % 2 == 0)
convertView.setBackgroundColor(Color.GREY);
else
convertView.setBackgroundColor(Color.WHITE);
Don't use that for
loop to set the background color after the fact. Do it in your getView
method of your adapter. Try this:
public View getView(int position, View convertView, ViewGroup parent) {
/* remainder is unchanged */
convertView.setBackgroundColor(position % 2 == 0 ? Color.WHITE : Color.GREY);
return convertView;
}
You can return different views from getView based on the passed in item position.