Vallout
Vallout

Reputation: 60

changing Backgroundcolor of a single View in a ListView despite scrolling

I have an Activity which is supposed to show one big ListView. Now I want to set the background colors of single lines. I tried to do it with getChildAt but then I can only mark those lines that are visible. So it doesnt work anymore, when the List is bigger than the screen. I could not figure out a working alternative, so I would be really glad if you could give me some advice.

Upvotes: 0

Views: 117

Answers (1)

Andy Res
Andy Res

Reputation: 16043

This can be achieved from within the adapter of your ListView. In the getView(...) method check whether the current item matches your criteria of a single line, and if so then set the specific background color for the current row, otherwise set the default color.

Something like this:

public class MyCustomAdapter extends BaseAdapter{

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      // ....
      convertView = inflater.inflate(R.layout.item_layout, null);
      // ...

      Item item = items.get(position);
      if(item.isSingleLine()){
         convertView.setBackgroundColor(SINGLE_LINE_BG_COLOR);
      }else{
         convertView.setBackgroundColor(DEFAULT_BG_COLOR);
      }
   }

}

Upvotes: 1

Related Questions