Some of the pages in the ListView are just a small part of the entire page, need to swipe up and down the entire page, the ListView does not let itself slide, the default ListView will only display the first item. This time you need to reset the height of the listview. If the ListView item has TextView and the number of rows in the TextView is greater than 1, this time. Resetting the height of the ListView does not calculate the height of the TextView, and there is a case where TextView only displays one row. This time you need to use a custom textview, and do not set maxlines this property.
Set the code for the ListView height:
Public static void setheigth (listview list) { listadapter listadapter = list.getadapter (); if (listadapter == null) { return; } int totalHeight = 0; for (Int i = 0, len = listadapter.getcount (); i < len; i++) { View Listitem = listadapter.getview (i, null, list); // Listitem.measure (Linearlayout.layoutparams.match_parent,0); Listitem.measure (0,0); totalheight += listitem.getmeasuredheight (); } viewgroup.layoutparams params = list.getlayoutparams (); params.height = totalHeight+ (List.getdividerheight () * (Listadapter.getcount () - 1)); List.setlayoutparams (params); }
Custom TextView code:
public class mytextview extends textview { private Context context; public mytextview (Context context, attributeset attrs) { super (context, attrs); // TODO Auto-generated constructor stub this.context=context; } @ Override protected void onmeasure (int widthmeasurespec, int HEIGHTMEASURESPEC) { // todo auto-generated method stub super.onmeasure (widthMeasureSpec, HEIGHTMEASURESPEC); layout layout = getlayout (); if (layout != null) { int height = (int) Math.ceil (Getmaxlineheight ( This.gettext (). toString ()) + getcompoundpaddingtop () + Getcompoundpaddingbottom (); int width = getmeasuredwidth (); Setmeasureddimension (width, height); } } private float getmaxlineheight (String STR) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;float height = 0.0f; float Screenw = context.getresources (). Getdisplaymetrics () .widthpixels; float paddingLeft = ((LinearLayout) this.getparent ()). Getpaddingleft (); float paddingreft = ((LinearLayout) this.getparent ()). Getpaddingright (); //here specific This.getpaint () to pay attention to use, to see where your textview, this is to take TextView parent control padding, in order to more accurately calculate the line int line = (int) math.ceil ( (This.getpaint ( ). Measuretext (str)/(screenw-paddingleft-paddingreft)); height = (This.getpaint (). Getfontmetrics (). Descent-this.getpaint (). Getfontmetrics (). Ascent) *line; return height; } }
This article is from the "focus_000" blog, make sure to keep this source http://120806872.blog.51cto.com/8289253/1606199
Android ListView Height Issue