fersarr fersarr - 2 months ago 5
Android Question

Expandable List: different background colors for ListGroup Items

I'm trying to have these two colors for the ListGroup items in my list: #E1E1E1 and #C2C2C2. The first for the even items and the other for the odd ones. It's 'partially' working. When the activity that opens the list is opened, I get the correct result but ONLY FOR THE VISIBLE PART of the list. If I scroll down, the backgrounds are all of the default color. If I scroll down and flip my phone into landscape mode, the activity gets created again and, again, I get the correct result only for the visible part of the list. I hope someone can help me a bit :)

EDIT:

what you see as white in the image is gray15p. The darker is gray30p which is the default in the XML. android:background="@color/gray30p". If I use, if-else it does work, but I can't understand why the gray15p (looks like white) is not being applied using the IF alone.

Here's how it looks:

list

In my expandable List Adapter:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}


...
...

//set background color in odd items
if(groupPosition%2==0){
LinearLayout groupLayout=(LinearLayout)convertView.findViewById(R.id.groupLinearLayout);
groupLayout.setBackgroundResource(R.color.gray15p);
}

return convertView;


and the layout file list_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/groupLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray30p"
android:orientation="horizontal" >
<!-- android:padding="8dp"
android:background="#000000"> -->

<ImageView
android:id="@+id/lblListHeaderImage"
android:layout_width="90dp"
android:layout_height="90dp"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:src="@drawable/defaultimg"
android:scaleType="fitCenter"
android:contentDescription="@string/app_name"/>


<!--level 1 of expandable list -->
<TextView
android:id="@+id/lblListHeader"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:paddingLeft="10dp"
android:textSize="22sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="center"
android:textColor="#000000" />

</LinearLayout>

Answer

I think whats missing is to set the color for the even items. The Problem is that you adapter loads the layout with the white background and changes it for the odd items to grey, but when you scroll down, the views gets reused. What happen is that your views background is already set to grey.

just adjust your code to something like that

    //set background color in odd items
    LinearLayout groupLayout=(LinearLayout)convertView.findViewById(R.id.groupLinearLayout)
    if(groupPosition%2==0){
        groupLayout.setBackgroundResource(R.color.gray15p);
    } else {
        groupLayout.setBackgroundResource(R.color.gray30p); // <-- important part.
    }
Comments