Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@muety
Created May 22, 2018 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muety/b89d5ef39351e267cd13adc080c5c7a1 to your computer and use it in GitHub Desktop.
Save muety/b89d5ef39351e267cd13adc080c5c7a1 to your computer and use it in GitHub Desktop.
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