首页 > 代码库 > GridView动态计算高度

GridView动态计算高度

// 动态加载GridView 高度
public static void setListViewHeightBasedOnChildren(MyGridView myGridView) {
ListAdapter listAdapter = myGridView.getAdapter();
if (listAdapter == null) {
return;
}
int col = 5;
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i += col) {
View listItem = listAdapter.getView(i, null, myGridView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = myGridView.getLayoutParams();
params.height = totalHeight;
((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
myGridView.setLayoutParams(params);
}

GridView动态计算高度