0

I have the following methods of the expandable list view implemented. Now, when the parent is clicked, the listView is collapsed. What I want to do, is set the list to collapse when the child is pressed (not only the group). Though I was searching, I can't understand which method I need to override/implement. Thanks!

expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
parent.collapseGroup(groupPosition);
return false;
    }
});
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

    @Override
    public void onGroupExpand(int groupPosition) {

    }
});
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

    @Override
    public void onGroupCollapse(int groupPosition) {

    }
});

expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
return false;
    }
});
sschale
  • 5,168
  • 3
  • 29
  • 36
user6456773
  • 381
  • 2
  • 10
  • 18

3 Answers3

4

All you need to do is add this line of code:

@Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            parent.collapseGroup(groupPosition);
            return false;
        }
VyaraG
  • 101
  • 11
1

Its Working Please See Screen shotenter image description here

 expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            // TODO Colapse Here Using this... in android
            int previousGroup = -1;
            boolean flag = false;

            @Override
            public void onGroupExpand(int groupPosition) {

                Log.e("keshav", "onGroupClick is -> " + groupPosition);

                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();

                if (groupPosition != previousGroup && flag) {
                    expListView.collapseGroup(previousGroup);
                }
                previousGroup = groupPosition;

                flag = true;

            }
        });

See here ExpandableLists in Android, I want to allow only one parent list to expand at a time

Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
1

Use this code to collapse ExpandableListView on child click

@Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            parent.collapseGroup(groupPosition);
            return false;
        }