2

I'm making an expandable list view for cities but it wont expand. I have another one almost exactly like this one but this one wont expand and I've spent a lot of time trying to see what's wrong and cant afford to waste any more can someone please point out the problem?

I also called expandGroup(0) but it still didnt't expanded (as if there is nothing to expand).

XML

<ExpandableListView
                        android:id="@+id/judet"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp"
                        android:choiceMode="singleChoice"
                        android:groupIndicator="@null"
                        android:smoothScrollbar="true" >
                    </ExpandableListView>

in my main class

final AdapterJudet adapterJudet = new AdapterJudet(getApplicationContext());
ExpandableListView judet = (ExpandableListView) findViewById(R.id.judet);
judet.setAdapter(adapterJudet);
judet.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                AdapterJudet.selected = AdapterJudet.judete[childPosition];
                judet.collapseGroup(0);
                return false;
            }
        });

Adapter class

public class AdapterJudet extends BaseExpandableListAdapter {

    Context ctx;
    static String [] judete = {"Alba","Arad","Arges","Bacau","Bihor","Bistrita-Nasaud","Botosani","Brasov","Braila","Buzau","Caras-Severin","Cluj","Constanta","Covasna","Dambovita","Dolj","Galati","Giurgiu","Gorj","Hargita","Hunedoara","Ialomita","Maramures","Mehedinti","Mures","Neamt","Olt","Prahova","Satu-Mare","Salaj","Sibiu","Teleorman","Timis","Tulcea","Valcea","Vaslui","Vrancea","Bucuresti","Ilfov","Calarasi","Iasi","Suceava"};
    static String selected = "Judet";
    static int judetID = -1;

    public AdapterJudet (Context ctx){
        this.ctx = ctx;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item_layout, parent, false);
        }

        TextView itemName = (TextView) v.findViewById(R.id.itemName);

        itemName.setText(judete[childPosition]);
        judetID = childPosition;
        return v;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return judete.length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.group_layout, parent, false);
        }

        TextView groupName = (TextView) v.findViewById(R.id.groupName);

        groupName.setText(selected);

        return v;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}
Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43

5 Answers5

19

I know it's been sometime since this question and I hope this helps someone looking for an answer. When you have clickable widgets (like button, checkbox), set the following property for them:

android:focusable="false"

I'm guessing what happens is that these widgets steal the focus (although this matters only if input is coming in from a keyboard or something like that) and automatically blocks the implicit click listeners (like group expand, collapse happen implicitly without setting a click listener).

If you still want to maintain the focusable property for the widget, the only alternative at this time seems to be to find the individual element in the getView() method of the adapter object and set an explicit click listener.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
4

I know it has been almost an year since you asked this question....

I was facing the same problem. Check to see if you have any "Clickable" widgets in the layout that you are expanding for the group view. I had a button in my group view layout and it was not passing the click to the group. Removing the button solve my problem.

Ruchi
  • 49
  • 3
  • 1
    yes, I had the same problem and it was because of a button in the group view. Now I am looking to keep the button on the group view, what I need to do to make it work ? – Chakib SAFAR Sep 01 '14 at 20:24
  • thank you Ruchi. I removed button from my layout and added Textview there. – SourabhMore Oct 06 '17 at 08:52
3

Use it for your ExpandableListView.

list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return  list.isGroupExpanded(groupPosition) ? list.collapseGroup(groupPosition) : list.expandGroup(groupPosition);
            }
        });
MediaPortal
  • 175
  • 6
2

work for me android:descendantFocusability="blocksDescendants" Add the above line in parent view

sharma_kunal
  • 2,152
  • 1
  • 28
  • 28
  • ucsunil's solution worked for me up until a recent update to one of the project dependencies. Now this solution is the only one working for me. – Auc Jan 20 '22 at 19:23
1

I remember having a similar problem a few months ago, Try changing

android:layout_height="wrap_content"

to

android:layout_height="match_parent"
Androidparanoid
  • 199
  • 1
  • 9