Android - ListView with 2 different Colors

后端 未结 5 971
情深已故
情深已故 2020-12-19 09:10

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

5条回答
  •  無奈伤痛
    2020-12-19 09:59

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

提交回复
热议问题