Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A helper method that enables an Android GridView to be used inside a vertical ScrollView
/* Inspired by https://stackoverflow.com/a/27818661/3112139 */
public static void justifyListViewHeightBasedOnChildren (GridView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getVerticalSpacing() * adapter.getCount());
listView.setLayoutParams(par);
listView.requestLayout();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.