Background Color of Listview in zebra

Asked

Viewed 1,074 times

2

I have a layout with two Edittext and an Imageview that will be added to Listview via Basedapter.
I wanted to change the background color of Listview in zebra.
The problem is that if Listview has more than 9 lists the zebra effect starts to work wrong.
To switch the Listview color I’m using the formula:

if(position % 2 == 0){
    layout.setBackgroundColor(Color.YELLOW);
}

1 answer

3


I think you’re using the so-called "View Holder Pattern" and is reusing the Views using the Layoutinflater only when the parameter convertView of the method getView() of Adapter is null.

So, how do you only assign the color to the backgroud when the position is even, when it reuses a View that previously was even in an odd position this keeps the color YELLOW.

In order for the color toggle to work in these cases you must also assign the color to the background when the position is odd:

if(position % 2 == 0){
    layout.setBackgroundColor(Color.YELLOW);
}
else{
    layout.setBackgroundColor(Color.DasLinhasÍmpares);
}

Note:
The problem only occurs when there are more than 9 lines because that must be the number of lines that fit on the screen. When doing scroll as Views lines which are no longer visible may be reused by the Adapter and the problem begins.

  • Hello ramaral! That was the problem. It worked. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.