{ January 14, 2011... I have given up to use setListViewHeightBasedOnChildren(ListView listView},
instead, I don't put my listview in a scrollview, and then just put other contents
into a listview by using ListView.addHeaderView() and ListView.addFooterView().
http://dewr.egloos.com/5467045 }
ViewGroup (el ViewGroup está conteniendo TextViews que tienen texto largo excepción de avance de línea caracteres) .getMeasuredHeight devuelve valor incorrecto ... que es más pequeño que la altura real.ViewGroup {TextView, ...}. GetMeasuredHeight da valor incorrecto es menor que la altura real de
cómo deshacerse de este problema?
aquí es el código de Java:
/*
I have to set my listview's height by myself. because
if a listview is in a scrollview then that will be
as short as the listview's just one item.
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int count = listAdapter.getCount();
for (int i = 0; i < count; i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
y aquí está la list_item_comments.xml:
Eso realmente me ha ayudado. ¡Muchas gracias! – myforums
@ user711058 tengo el mismo problema pero esta solución no funciona para mí – anshul
Esta respuesta me ayudó a resolver un problema en el que he estado trabajando durante un tiempo. Mi problema vino de la falta de comprensión de cómo funcionaba la medida. Mi vista secundaria usaba match_parent para su ancho, pero mi llamada de medida usa MeasureSpec.makeMeasureSpec (0, MeasureSpec.UNSPECIFIED) para el parámetro de ancho. Al cambiar el parámetro de ancho a MeasureSpec.makeMeasureSpec (getWidth(), MeasureSpec.EXACTLY), me permitió obtener la altura correcta de la vista al llamar a view.getMeasuredHeight(). ¡Gracias! – DalvikDroid