Change background colour of current listview item in adapter getView method

前端 未结 7 1238
迷失自我
迷失自我 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条回答
  •  旧时难觅i
    2020-12-03 19:58

    Please correct me if I am wrong but I do it this way:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
            convertView = ((LayoutInflater) this._ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(this._resource, parent, false);
    
        }
    
       switch(position % 3){
          case 0: convertView.setBackgroun....
              break;
          .... (case 1; case 2;)
    
       }
    
    return convertView;
    
    }
    

提交回复
热议问题