Created
May 2, 2013 12:33
-
-
Save paddyzab/5501898 to your computer and use it in GitHub Desktop.
ListViewHeight based on Height and count of Children.
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
protected static void doSetListViewHeightBasedOnChildren(ListView listView, int extraPixels) { | |
if (null != listView) { | |
final ListAdapter listAdapter = listView.getAdapter(); | |
if (null != listAdapter) { | |
int totalHeight = 0, desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST); | |
for (int i = listAdapter.getCount(); --i >= 0; ) { | |
View listItem = listAdapter.getView(i, null, listView); | |
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); | |
totalHeight += listItem.getMeasuredHeight(); | |
} | |
ViewGroup.LayoutParams params = listView.getLayoutParams(); | |
params.height = extraPixels + totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); | |
listView.setLayoutParams(params); | |
listView.requestLayout(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment