ListView 與 GridView 在ScrollView中自適應高度
阿新 • • 發佈:2019-02-18
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);
}