To make whole screen scrollable when screen having more then one listview or more then one ExpandableListView.
First we need to measure height of whole list and apply same height to that list. we need to calculate height for all list separately.
Following code will use to calculate height of Expandable list
you need to call this function at the time of "setAdapter","notifydatasetChanged", "ongroupClick" for expandableListView.
Whenever any view related operation performed then we need to call this function to calculate actual height of list.
For ListView We may use following function. call it at time of Notifydatasetchaged, setAdapter etc
First we need to measure height of whole list and apply same height to that list. we need to calculate height for all list separately.
Following code will use to calculate height of Expandable list
public static boolean setListViewHeightBasedOnItems(ExpandableListView listView) { ExpandableJunkListAdapter listAdapter = (ExpandableJunkListAdapter) listView.getExpandableListAdapter(); if (listAdapter != null) { int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY); int numberOfItems = listAdapter.getGroupCount(); // Get total height of all items. int totalItemsHeight = 0; for (int itemPos = 0; itemPos < numberOfItems; itemPos++) { View item = listAdapter.getGroupView(itemPos, listView.isGroupExpanded(itemPos), null, listView); View childItem = null; int noOfChilds = listAdapter.getRealChildrenCount(itemPos); int childPos = 0; if (listView.isGroupExpanded(itemPos)) { for (childPos = 0; childPos < noOfChilds; childPos++) { if (childPos == (noOfChilds - 1)) { childItem = listAdapter.getRealChildView(itemPos, childPos, true, null, listView); } else { childItem = listAdapter.getRealChildView(itemPos, childPos, false, null, listView); } childItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalItemsHeight += childItem.getMeasuredHeight(); } } item.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalItemsHeight += item.getMeasuredHeight(); } // Get total height of all item dividers. int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1); // Set list height. ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalItemsHeight + totalDividersHeight; // Log.i("height", "" + params.height); Log.e("height", "" + params.height); listView.setLayoutParams(params); listView.requestLayout(); return true; } else { return false; } }
you need to call this function at the time of "setAdapter","notifydatasetChanged", "ongroupClick" for expandableListView.
Whenever any view related operation performed then we need to call this function to calculate actual height of list.
For ListView We may use following function. call it at time of Notifydatasetchaged, setAdapter etc
public static void getTotalHeightofListView(ListView listView) { ListAdapter mAdapter = listView.getAdapter(); int totalHeight = 0; for (int i = 0; i < mAdapter.getCount(); i++) { View mView = mAdapter.getView(i, null, listView); mView.measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); totalHeight += mView.getMeasuredHeight(); Log.w("HEIGHT" + i, String.valueOf(totalHeight)); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (mAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout(); }