Change ListView Item background color but keep the default selector style?

后端 未结 4 561
暗喜
暗喜 2020-12-18 01:24

I am trying to programmatically set a custom background color in my ListView Adapter but I want to keep the default Listview Selector style from Android.

I am settin

4条回答
  •  猫巷女王i
    2020-12-18 02:14

    Try write your custom adapter and set rowView BackgroundColor like that(if position odd number BLACK, even RED). also you can send a color list to adapter and set different color for every rowView

    public View getView(int position, View convertView, ViewGroup parent) {
                View rowView = convertView;
            enter code here
    
                if(rowView == null)
                {
                    if((position % 2)==1){ 
                    rowView.setBackgroundColor(Color.BLACK)
                        //colorList :setBackgroundColor( colorList.get(position) )
                    }
                     else{
                        rowView.setBackgroundColor(Color.RED)
                    }
                 }           
                return rowView;
            }
    

提交回复
热议问题