private void getListHeight(ListView mlistview) {
ListAdapter listAdapter = mlistview.getAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, mlistview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = mlistview.getLayoutParams();
params.height = totalHeight
+ (mlistview.getDividerHeight() * (listAdapter.getCount() - 1));
mlistview.setLayoutParams(params);
}
直接调用 传入listview对象即可