Skip to content

Instantly share code, notes, and snippets.

@ptrstovka
Created January 17, 2017 15:13
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 ptrstovka/bb6b26cccb01b7b98a1452b3f9b94ae2 to your computer and use it in GitHub Desktop.
Save ptrstovka/bb6b26cccb01b7b98a1452b3f9b94ae2 to your computer and use it in GitHub Desktop.
how to set fixed size on GridView
// working only when gridview is in ScrollView
private void setFixedHeightOnGridView() {
int fixedHeight = 0;
int columnCount = 2;
int rows = 0;
for (int i = 0; i < mNavigationView.getAdapter().getCount(); i = i + columnCount) {
int highestItemInRow = 0;
for (int j = i; j < i + columnCount; j++) {
View item = mNavigationView.getAdapter().getView(i, null, mNavigationView);
if (item instanceof ViewGroup) {
item.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
item.measure(0, 0);
if (item.getMeasuredHeight() > highestItemInRow) {
highestItemInRow = item.getMeasuredHeight();
}
}
fixedHeight += highestItemInRow;
rows++;
}
// only if you want vertical spacing
int verticalSpacing = getResources().getDimensionPixelOffset(R.dimen.nav_menu_vertical_spacing);
fixedHeight += (rows) * verticalSpacing;
ScrollView.LayoutParams layoutParams = (ScrollView.LayoutParams)
mNavigationView.getLayoutParams();
layoutParams.height = fixedHeight;
mNavigationView.setLayoutParams(layoutParams);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment