Skip to content

Instantly share code, notes, and snippets.

@moltak
Created April 11, 2014 11:05
Show Gist options
  • Save moltak/10458814 to your computer and use it in GitHub Desktop.
Save moltak/10458814 to your computer and use it in GitHub Desktop.
ExpandableListView in Scrollview
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.expandable, container, false);
ExpandableAdapter adapter = new ExpandableAdapter();
expandableListView.setAdapter(adapter);
setExpandableListViewHeight(expandableListView, -1);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
setExpandableListViewHeight(parent, position);
return false;
}
});
return view;
}
@franklinway2smile
Copy link

Please share me the setExpandableListViewHeight(expandableListView,-1) Method codes., Thanks in Advance

@shobishani
Copy link

shobishani commented Sep 18, 2018

private void setExpandableListViewHeight(ExpandableListView listView,
                               int group) {
    ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
            View.MeasureSpec.EXACTLY);
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
 
        totalHeight += groupItem.getMeasuredHeight();
 
        if (((listView.isGroupExpanded(i)) && (i != group))
                || ((!listView.isGroupExpanded(i)) && (i == group))) {
            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                View listItem = listAdapter.getChildView(i, j, false, null,
                        listView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
 
                totalHeight += listItem.getMeasuredHeight();
 
            }
        }
    }
 
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    int height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    if (height < 10)
        height = 200;
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();
 
}

@shobishani
Copy link

Please share me the setExpandableListViewHeight(expandableListView,-1) Method codes., Thanks in Advance

This works really fine below method

@anantshah93
Copy link

anantshah93 commented Feb 9, 2019

Perfectly Worked.

@O1derman
Copy link

awesome, thank you!

@Scienticious
Copy link

Why use this line setExpandableListViewHeight(expandableListView, -1); ?
Because when I use this my ExpandabeListView is showing in scroll initially but it set normal when i expanded any item

@legionsally
Copy link

remains lots of space before click on any group,after clicking everything ok

@legionsally
Copy link

give me solution

@Arturo0021
Copy link

Excelente lo de shobishani , me funciono al 100

@krzysztof90
Copy link

I would only add after children loop

totalHeight += (listView.getDividerHeight() * (listAdapter.getChildrenCount(i) - 1));

@zeynep-turker
Copy link

zeynep-turker commented Jun 6, 2024

I used this method on my extendedListView but at first unnecessary extra space remains in my activity. Then I clicked a group, and it fixed. How to fix this problem?

@zeynep-turker
Copy link

zeynep-turker commented Jun 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment