I know the question is quite old, but maybe it will be useful for someone. Basically, my answer is some combination of the answers of Amit Tumkur and user 2141833. After a lot of trial and error, the following code works for me:
First, we calculate the initial height of the list of the expandable list, that is, when all this is minimized
for (Integer i = 0; i < mAdapter.getGroupCount(); i++) { View groupItem = mAdapter.getGroupView(i, false, null, mExpandableListView); groupItem.measure(mExpandableListView.getWidth(), View.MeasureSpec.UNSPECIFIED); mInitialHeight += groupItem.getMeasuredHeight(); }
Then, when a group is clicked, set the height of the expanded list view to wrap the content
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
setListHeightToWrap is another method:
private void setListHeightToWrap() { LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mExpandableListView.getLayoutParams(); params.height = ViewGroup.LayoutParams.WRAP_CONTENT; mExpandableListView.setLayoutParams(params); mExpandableListView.refreshDrawableState(); mScrollView.refreshDrawableState(); }
Then in OnGroupExpandListener set the height of the expandable list in the form:
mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mStitchingWorksListView.getLayoutParams(); //The Logic here will change as per your requirements and the height of each of the children in the group if (mAdapter.getRealChildrenCount(groupPosition) > 6) { params.height = 9 * mInitialHeight; } else { params.height = 6 * mInitialHeight; } //For Last Group in the list and the number of children were less as compared to other groups if (groupPosition == mAdapter.getGroupCount() - 1) { params.height = 3 * mInitialHeight; } mExpandableListView.setLayoutParams(params); mExpandableListView.refreshDrawableState(); mExpandableListView.refreshDrawableState(); } });
Also the layout was an ExpandableListView inside a LinearLayout inside a ScrollView.
Hope this helps someone. :)
Varun ramani
source share