Created
May 22, 2018 14:03
-
-
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
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
/* 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