I'm developing an Android app with a left slide menu contains some LinearLayout
and a ExpandableListview
in the middle like this image
The child view of ExpandableListview
contains a TextView
and a ListView
below, ListView's visibility is gone in default. When click on the TextView
, ListView
will be visible.
The problem is that when click on the parent view of ExpandableListview
, the list's height also expand to wrap it's content. But when click on child view (also TextView
), the list'height doesn't change and I have to scroll down to see the last item.
To set height of ExpandableListview inside ScrollView, I used this code:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if(listItem instanceof SecondLevelExpandableListView)
Log.e("abc", "SecondLevelExpandableListView");
if (listItem instanceof ViewGroup) {
Log.e("abc", "ViewGroup");
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
Screenshot of problem
Please help me to solve this problem, I need the list always to wrap it's content.
Thanks in advance.