ListView Background Color in Zebra

2

I have a layout with two EditText and an ImageView that will be added to the ListView via BaseAdapter. I wanted to toggle the background color of the ListView in zebra. The problem is that if the ListView has more than 9 lists the zebra effect starts to work wrong. To change the color of the ListView I am using the formula:

if(position % 2 == 0){
    layout.setBackgroundColor(Color.YELLOW);
}
    
asked by anonymous 02.05.2015 / 22:13

1 answer

3

I think you are using the View holder pattern and you are reusing Views using LayoutInflater only when the convertView of method getView() of Adapter is null .

So, since you only assign the color to backgroud when the position is even, when you reuse a View with%.

For color to work in these cases, you must also assign the color to 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 this should be the number of lines that fit on the screen. When scroll the Views of the lines that are no longer visible, they can be reused by the Adapter     

03.05.2015 / 00:00