Judging each item height of a ListView

Source: Internet
Author: User

If the root view of this item must be LinearLayout

 PackageCom.bxg.news.view;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ListAdapter;ImportAndroid.widget.ListView; Public  class Utility {  Public Static void Setlistviewheightbasedonchildren(ListView ListView, View v) {//Get ListView corresponding AdapterListAdapter listadapter = Listview.getadapter ();if(ListAdapter = =NULL) {//Pre-condition   return; }intTotalheight =0; for(inti =0, Len = Listadapter.getcount (); i < Len; i++) {//Listadapter.getcount () returns the number of data itemsView ListItem = Listadapter.getview (i,NULL, ListView); Listitem.measure (0,0);//Calculate the width height of the child viewTotalheight + = Listitem.getmeasuredheight ();//Statistics total height of all children}if(V! =NULL) {totalheight + = V.getheight ();  } viewgroup.layoutparams params = Listview.getlayoutparams (); Params.height = Totalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1))    +Ten;//Listview.getdividerheight () Gets the height of the delimiter between children  //Params.height finally get the entire ListView full display required heightListview.setlayoutparams (params); }}

It is important to note that:
If TextView is a multi-line time. Need to override Onmeasure method for TextView

protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); Layout layout = GetLayout ();if(Layout! =NULL) {intHeight = (int) Floatmath.ceil (Getmaxlineheight ( This. GetText (). toString ()) + getcompoundpaddingtop () + Getcompoundpaddingbottom ();intwidth = Getmeasuredwidth ();  Setmeasureddimension (width, height); } }Private float Getmaxlineheight(String str) {floatHeight =0.0FfloatScreenw = (Activity) context). Getwindowmanager (). Getdefaultdisplay (). GetWidth ();floatPaddingleft = ((relativelayout) This. GetParent ()). Getpaddingleft ();floatPaddingreft = ((relativelayout) This. GetParent ()). Getpaddingright ();//Here specific This.getpaint () to pay attention to use, to see where your textview, this is to take the TextView parent control of padding, in order to more accurately calculate the line break  intLine = (int) Math.ceil ( This. Getpaint (). Measuretext (str)/(screenw-paddingleft-paddingreft)); Height = ( This. Getpaint (). Getfontmetrics (). Descent- This. Getpaint (). Getfontmetrics (). Ascent) * Line;returnHeight }

To determine each item height of a ListView

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.