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
- Public void setlistviewheightbasedonchildren (listview ){
- // Obtain the adapter corresponding to the listview
- Listadapter = listview. getadapter ();
- If (listadapter = NULL ){
- Return;
- }
- Int totalheight = 0;
- For (INT I = 0, Len = listadapter. getcount (); I <Len; I ++ ){
- // Listadapter. getcount () returns the number of data items
- View listitem = listadapter. getview (I, null, listview );
- // Calculate the width and height of the subitem View
- Listitem. Measure (0, 0 );
- // Calculate the total height of all subitems
- Totalheight + = listitem. getmeasuredheight ();
- }
- Viewgroup. layoutparams Params = listview. getlayoutparams ();
- Params. Height = totalheight + (listview. getdividerheight () * (listadapter. getcount ()-1 ));
- // Listview. getdividerheight () obtains the height occupied by separators between subitems.
- // Params. height the final height required for the full display of the entire listview
- Listview. setlayoutparams (Params );
- }
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
- @ Override
- Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
- Super. onmeasure (widthmeasurespec, heightmeasurespec );
- Layout layout = getlayout ();
- If (layout! = NULL ){
- Int Height = (INT) floatmath. Ceil (getmaxlineheight (this. gettext (). tostring ()))
- + Getcompoundpaddingtop () + getcompoundpaddingbottom ();
- Int width = getmeasuredwidth ();
- Setmeasureddimension (width, height );
- }
- }
- Private float getmaxlineheight (string Str ){
- Float Height = 0.0f;
- Float screenw = (activity) Context). getwindowmanager (). getdefaultdisplay (). getwidth ();
- Float paddingleft = (linearlayout) This. getparent (). getpaddingleft ();
- Float paddingreft = (linearlayout) This. getparent (). getpaddingright ();
- // 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.
- 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 ()