Created
January 17, 2017 15:13
-
-
Save ptrstovka/bb6b26cccb01b7b98a1452b3f9b94ae2 to your computer and use it in GitHub Desktop.
how to set fixed size on GridView
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
// 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