Search code examples
androidexpandablelistviewindicator

Expandablelistview custom indicator not visible


I have a expandablelistview with a custom indicator but somehow the indicator does not get shown in the expandablelistview.

Did i perhaps overlooked something? Thank you very much for helping me out.

Edit: all works now, below the correct code.

custom_arrow.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_empty="true" android:drawable="@drawable/arrow_down_black"/>
   <item android:state_expanded="true" android:drawable="@drawable/arrow_up_black"/>
   <item android:drawable="@drawable/arrow_down_black"/>
</selector>

Frame layout with expandablelistview:

<LinearLayout
    android:id="@+id/expandablelv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_bg"
    android:orientation="vertical">

    <ExpandableListView
        android:transcriptMode="alwaysScroll"
        android:id="@+id/exp"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_marginLeft="5dp">

    </ExpandableListView>
</LinearLayout>

My code:

 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
//
lv = (ExpandableListView) view.findViewById(R.id.exp);
  lv.setAdapter(new ExpandableListAdapter(groups, children)); 
   lv.setGroupIndicator(this.getResources().getDrawable(R.drawable.custom_arrow));
//
}

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private final LayoutInflater inf;
    private String[] groups;
    private String[][] children;

    public ExpandableListAdapter(String[] groups, String[][] children) {
        this.groups = groups;
        this.children = children;
        inf = LayoutInflater.from(getActivity());
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

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

        ViewHolder holder;
        if (convertView == null) {
            convertView = inf.inflate(R.layout.child_layout, parent, false);
            holder = new ViewHolder();

            holder.text = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getChild(groupPosition, childPosition).toString());

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inf.inflate(R.layout.parent_layout, parent, false);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getGroup(groupPosition).toString());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ViewHolder {
        TextView text;
    }
}

Solution

  • This worked for me:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_empty="true" android:drawable="@drawable/arrow_down_black"/>
        <item android:state_expanded="true" android:drawable="@drawable/arrow_up_black"/>
        <item android:drawable="@drawable/arrow_down_black"/>
    </selector>
    

    A little more info can be found in this (somewhat) related question.