首页 > 代码库 > GridViewUtility
GridViewUtility
/**
* 重新计算GridView的高度
*/
public class GridViewUtility {
public static void setGridViewHeightOnChildren(GridView gridView){
ListAdapter adapter = gridView.getAdapter();
if (adapter == null) {
// pre-condition
return;
}
int rowCount = 0;
float numLine = adapter.getCount() /gridView.getNumColumns();
int numLineInt = adapter.getCount() /gridView.getNumColumns();
if (numLine - (float)numLineInt < 1.0){
rowCount = numLineInt + 1;
}else if(numLine - (float)numLineInt == 0.0){
// 整除
rowCount = numLineInt;
}
int totalHeight = 0;
for (int i = 0; i < rowCount; i++) {
View listItem = adapter.getView(i, null, gridView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight + (gridView.getVerticalSpacing() *(rowCount - 1));
gridView.setLayoutParams(params);
}
}
GridViewUtility