When we have a ListView expandable inside ScrollView, we must make this in our layout, always expandable listview must be the last view in our layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | android:id = "@+id/activity_expandable_scroll_view" android:layout_width = "match_parent" android:layout_height = "match_parent" android:fillViewport = "true" > < LinearLayout android:id = "@+id/LinearLayout1" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" > <!-- your code ---> < ExpandableListView android:id = "@+id/activity_expandable_list_view" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:background = "@color/white" /> </ LinearLayout > </ ScrollView > |
In your onCreate
1 2 3 4 5 6 7 8 9 10 11 12 13 | mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view); MyExpandableListAdapter adapter = new MyExpandableListAdapter( this , mGroups); mListView.setAdapter(adapter); mListView.setOnGroupClickListener( new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { setListViewHeight(parent, groupPosition); return false ; } }); |
You need this function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | private void setListViewHeight(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(); } |