Problema: Queremos colocar un listview dentro de un scrollview (por ejemplo para mostrar comentarios en una ficha de un producto), y aparece cortado al final.
Solución
private void setListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if(listAdapter == null)
return;
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
int totalChildren = 0;
for (int j = 0; j < listAdapter.getCount(); j++) {
View listItem = listAdapter.getView(j, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
totalChildren++;
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * listAdapter.getCount() );
if (height < 10)
height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}