scrollview 中嵌套多个listview的最好解决办法

时间:2021-12-04 19:30:50

在scrollview中嵌套多个listview的显示问题.

只需要调用如下的方法传入listview和adapter数据即可.

/**
* 动态设置ListView组建的高度
*/
public static void setListViewHeightBasedOnChildren(ListView listView,Adapter adapter) {
Adapter listAdapter = (Adapter) listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}