DroidT
DroidT

Reputation: 3293

ExpandableListView expanded view not showing

I have an ExpandableListView that I want to customize with a different view when the item is expanded. The problem is the image I have assigned to the state_expanded attribute is never shown. I've tried almost all other attribute for the state but the only one that does anything is state_pressed and it just briefly shows a different image when the state is pressed. How do I get my list_item_expanded drawable to stay shown when the item is expanded? Any ideas or help would be appreciated.

Here is my selector.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/list_item_expanded" android:state_expanded="true"></item>
  <item android:drawable="@drawable/list_item"></item>
</selector>

Here is my where I style my ExpandableListView

<style name="customExpandableList" parent="@android:style/Widget.ExpandableListView">
    <item name="android:groupIndicator">@null</item>
    <item name="android:dividerHeight">5dp</item>
    <item name="android:divider">@android:color/transparent</item>
    <item name="android:listSelector">@android:color/transparent</item>
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:childDivider">@android:color/transparent</item>
</style>

I set the selector as a drawable in the adapter like so:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.trans_sum_row, null);
    }
    view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.list_item_selector));
}

Upvotes: 3

Views: 3133

Answers (2)

3c71
3c71

Reputation: 4511

Experiencing the same issue, I needed something reliable doing exactly what I expected of the state_expanded flag (and optionally the state_last flag for items), eg expand the background of the group to its children.

I ended-up doing something like this allowing me to reuse that for all ExpandableListView:

The idea is to derive from this adapter class and implement the 2 abstract method it defines (instead of the originals). Then you'll have to create the 4 backgrounds for each state (I personally used shapes but I'm sure 9-patches will do very well here too).

The group_padding is used to make sure the group view is not on top of the left indicator.

public abstract class ccc71_expandable_list_adapter extends BaseExpandableListAdapter
{
protected Context context;
private int group_padding;

protected abstract View getChildView(int groupPosition, int childPosition, View convertView);

protected abstract View getGroupView(int groupPosition, View convertView, View parent);

public ccc71_expandable_list_adapter(Context ctx)
{
    super();

    context = ctx.getApplicationContext();

    TypedArray ta = context.getTheme().obtainStyledAttributes(new int[] { android.R.attr.expandableListPreferredItemPaddingLeft });

    group_padding = (int) ta.getDimension(0, 10f);

    ta.recycle();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    View v = getGroupView(groupPosition, convertView, parent);

    if (isExpanded)
    {
        v.setBackgroundResource(R.drawable.drop_shadow_expanded);
    }
    else
    {
        v.setBackgroundResource(R.drawable.drop_shadow_collapsed);
    }
    v.setPadding(group_padding, v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom());

    return v;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
    View v = getChildView(groupPosition, childPosition, convertView);

    if (isLastChild)
    {
        v.setBackgroundResource(R.drawable.drop_shadow_child_last);
    }
    else
    {
        v.setBackgroundResource(R.drawable.drop_shadow_childs);
    }

    return v;
}
}

Upvotes: 0

DroidT
DroidT

Reputation: 3293

There doesn't seem to be a way to do what I was trying to do. The android:state_expanded attribute is only applicable to the group indicator and not the ExpandableListView itself. What I ended up doing was creating one selector for the ELV and another for the group indicator like so

<style name="customExpandableList" parent="@android:style/Widget.ExpandableListView">
  <item name="android:groupIndicator">@drawable/custom_group_selector</item>
  <item name="android:background">@drawable/custom_elv_selector</item>
  <item name="android:dividerHeight">5dp</item>
  <item name="android:divider">@android:color/transparent</item>
  <item name="android:listSelector">@android:color/transparent</item>
  <item name="android:cacheColorHint">@android:color/transparent</item>
  <item name="android:childDivider">@android:color/transparent</item>
</style>

After that I was able to get the same effect I wanted by positioning the group indicator where I wanted by using the SO solution here.

Positioning the group indicator this way hasn't proven the best solution for me because I'm seeing different results on some phones but that is a different post in itself.

Upvotes: 1

Related Questions