Skip to content Skip to sidebar Skip to footer

Alternating Colors In Listview But Needs To Have A Starting Color

I have a listview that looks like this. | Header | ---------- | Data 1 | ---------- | Data 2 | ---------- | Data 3 | ---------- | Header | ---------- | Data 1 | ---------- | Header

Solution 1:

Easy, when you build your data , add a new attribute like 'order', then build like this:

Assuming your class model:

        DataEntry {

          int order;
          ... 
        }


        addHeader 1
        addDataEntry(order: 1)
        addDataEntry(order: 2)
        addDataEntry(order: 3)
        addDataEntry(order: ... n)

        addHeader 2
        addDataEntry(order: 1)
        addDataEntry(order: 2)
        addDataEntry(order: 3)
        addDataEntry(order: ... n)

        ...

    public View getView(int position, View convertView, ViewGroup parent) {
      /* Alternating Colors*/DataEntryentry= list.get(position);
                        LinearLayoutline_others= v.findViewById(R.id.line_others);
                        if (entry.position % 2 == 0) {
                            line_others.setBackgroundResource(R.color.red);
                        } else {
                            line_others.setBackgroundResource(R.color.alt_gray);
                        }
    }

Post a Comment for "Alternating Colors In Listview But Needs To Have A Starting Color"