Scrollview nested listview displays only one row of calculated height incorrect solution ()

Source: Internet
Author: User
Scrollview nested listview displays only one row of calculated height incorrect solution category: Android Application Development 1045 people read comments (3) favorite reports androidscrollview nested listview shows a row of height incorrect

1. Preface

The scrollview nested listview found by Google shows only one row of solution. I believe many people have met it, and most of them are solved by this blogger.

At the beginning, I used this solution. First of all, I would like to thank this brother for his private dedication and paste the address.

Http://blog.csdn.net/p106786860/article/details/10461015

2. Core code

 

[HTML]View plaincopy
  1. Public void setlistviewheightbasedonchildren (listview ){
  2. // Obtain the adapter corresponding to the listview
  3. Listadapter = listview. getadapter ();
  4. If (listadapter = NULL ){
  5. Return;
  6. }
  7. Int totalheight = 0;
  8. For (INT I = 0, Len = listadapter. getcount (); I <Len; I ++ ){
  9. // Listadapter. getcount () returns the number of data items
  10. View listitem = listadapter. getview (I, null, listview );
  11. // Calculate the width and height of the subitem View
  12. Listitem. Measure (0, 0 );
  13. // Calculate the total height of all subitems
  14. Totalheight + = listitem. getmeasuredheight ();
  15. }
  16. Viewgroup. layoutparams Params = listview. getlayoutparams ();
  17. Params. Height = totalheight + (listview. getdividerheight () * (listadapter. getcount ()-1 ));
  18. // Listview. getdividerheight () obtains the height occupied by separators between subitems.
  19. // Params. height the final height required for the full display of the entire listview
  20. Listview. setlayoutparams (Params );
  21. }
This Code allows the control to calculate the height of the listview and then set the height of the listview.

 

But there is a problem in this code, that is, when there are multiple lines of textview in your listview, the height of the listview will be calculated incorrectly, and it will only calculate the height of a row of textview,

This issue is outlined in so as follows:

Http://stackoverflow.com/questions/14386584/getmeasuredheight-of-textview-with-wrapped-text

3. Ultimate Solution

After a headache for a while, I found out that it is better to rewrite the onmeasure method of textview.

The Code has

 

[Java]View plaincopy
  1. @ Override
  2. Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
  3. Super. onmeasure (widthmeasurespec, heightmeasurespec );
  4. Layout layout = getlayout ();
  5. If (layout! = NULL ){
  6. Int Height = (INT) floatmath. Ceil (getmaxlineheight (this. gettext (). tostring ()))
  7. + Getcompoundpaddingtop () + getcompoundpaddingbottom ();
  8. Int width = getmeasuredwidth ();
  9. Setmeasureddimension (width, height );
  10. }
  11. }
  12. Private float getmaxlineheight (string Str ){
  13. Float Height = 0.0f;
  14. Float screenw = (activity) Context). getwindowmanager (). getdefaultdisplay (). getwidth ();
  15. Float paddingleft = (linearlayout) This. getparent (). getpaddingleft ();
  16. Float paddingreft = (linearlayout) This. getparent (). getpaddingright ();
  17. // Here, this. getpaint () must be used. It depends on the position of your textview. This is based on the padding of the parent control of textview, in order to calculate the line feed more accurately.
  18. Int line = (INT) math. ceil (this. getpaint (). measuretext (STR)/(screenw-paddingleft-paddingreft); Height = (this. getpaint (). getfontmetrics (). descent-this.getPaint (). getfontmetrics (). ascent) * line; return height ;}


 

The above code is better. When listview starts measurement and textview is measured, our onmeasure method is called, we can measure the total width of the font and the size of the screen with the margin removed to calculate the number of lines of text to display, and then measure the font height * number of lines to get the total height of the font, then, the top and bottom margins are the true height of textview, and setmeasureddimension can be used to calculate the correct value.

Scrollview nested listview displays only one row of calculated height incorrect solution ()

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.