您的位置:首页 > 其它

ListView 与 GridView 在ScrollView中自适应高度

2016-03-28 17:12 363 查看
ListView

public static void setListViewHeightBasedOnChildren(ListView listView) {
if (limitTimeAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < limitTimeAdapter.getCount(); i++) {
View listItem = limitTimeAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = listView.getLayoutParams();

params.height = totalHeight
+ (listView.getDividerHeight() * (limitTimeAdapter.getCount() - 1));
listView.setLayoutParams(params);
}


GridView

public static void setGridViewHeightBasedOnChildren(GridView gridView) {
if (choicenessAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < choicenessAdapter.getCount(); i += 2) {
View listItem = choicenessAdapter.getView(i, null, gridView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gridview listview