Created
June 6, 2024 11:12
-
-
Save zeynep-turker/ee0f5917a4f230a8d1f424efa5834648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
////////////////////// | |
ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(this, dgResults); | |
binding.expandableList.setAdapter(expandableListAdapter); | |
binding.expandableList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | |
@Override | |
public void onGlobalLayout() { | |
setExpandableListViewHeight(binding.expandableList, -1); | |
binding.expandableList.getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
} | |
}); | |
binding.expandableList.setOnGroupClickListener((parent, v, position, id) -> { | |
setExpandableListViewHeight(parent, position); | |
return false; | |
}); | |
} | |
} | |
private void setExpandableListViewHeight(ExpandableListView listView, int group) { | |
ExpandableListAdapter listAdapter = 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment