Change background colour of current listview item in adapter getView method

前端 未结 7 1237
迷失自我
迷失自我 2020-12-03 19:20

I am building a custom adapter for a listview - I would like this adapter to give the listview alternating background colours.

boolean alternate = false;

@         


        
7条回答
  •  感动是毒
    2020-12-03 20:00

    You're not going in the right direction as if the views are re-used you might get unexpected results in that some recycled views will have a different colors, while others not.

    Instead of above, set the background based on position. Something like:

    if(position % 2 == 0) {
        // set some color
    } else {
        // set the other color
    }
    

提交回复
热议问题