Android ListView Height Issue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.