Last active
August 29, 2015 14:06
-
-
Save SehoNoh/5638005fa27763fd21f8 to your computer and use it in GitHub Desktop.
For ListView in ScrollView, (http://www.jiramot.info/android-listview-in-scrollview-problem)
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
public static void setListViewHeightBasedOnChildren(ListView listView) { | |
ListAdapter listAdapter = listView.getAdapter(); | |
if (listAdapter == null) { | |
// pre-condition | |
return; | |
} | |
int totalHeight = 0; | |
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); | |
for (int i = 0; i < listAdapter.getCount(); i++) { | |
View listItem = listAdapter.getView(i, null, listView); | |
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); | |
totalHeight += listItem.getMeasuredHeight(); | |
} | |
ViewGroup.LayoutParams params = listView.getLayoutParams(); | |
params.height = 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