Address: http://blog.csdn.net/cs_li1126/article/details/12906203
I.The method used on the Internet to dynamically change the height of the listview is only applicable when the item layout is linearlayout. It cannot be other layout (such as relativelayout) onmeasure () is not overwritten, so an exception is thrown during onmeasure. Therefore, the usage is limited.
[Java]View plaincopy
- Public class utility {
- Public static void setlistviewheightbasedonchildren (listview ){
- // Obtain the adapter corresponding to the listview
- Listadapter = listview. getadapter ();
- If (listadapter = NULL ){
- // Pre-condition
- Return;
- }
- Int totalheight = 0;
- For (INT I = 0, Len = listadapter. getcount (); I <Len; I ++) {// number of data items returned by listadapter. getcount ()
- View listitem = listadapter. getview (I, null, listview );
- Listitem. Measure (0, 0); // calculate the width and height of the subitem view.
- Totalheight + = listitem. getmeasuredheight (); // calculate the total height of all sub-items
- }
- 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 );
- }
- }
II.There is a post on the Internet saying that the Android: fillviewport = "true" attribute is added to scrollview, so that the listview can be displayed in full screen. Test on my machine failed.
III:Override listview and gridview (recommended)
Override listview:
[Java]View plaincopy
- Public class mylistview extends listview {
- Public mylistview (context ){
- // Todo auto-generated method stub
- Super (context );
- }
- Public mylistview (context, attributeset attrs ){
- // Todo auto-generated method stub
- Super (context, attrs );
- }
- Public mylistview (context, attributeset attrs, int defstyle ){
- // Todo auto-generated method stub
- Super (context, attrs, defstyle );
- }
- @ Override
- Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
- // Todo auto-generated method stub
- Int expandspec = measurespec. makemeasurespec (integer. max_value> 2,
- Measurespec. at_most );
- Super. onmeasure (widthmeasurespec, expandspec );
- }
- }
Rewrite the gridview:
[Java]View plaincopy
- /**
- * Custom gridview solves the problem of abnormal display of nested gridview in scrollview (1 row)
- */
- Public class mygridview extends gridview {
- Public mygridview (context, attributeset attrs ){
- Super (context, attrs );
- }
- Public mygridview (context ){
- Super (context );
- }
- Public mygridview (context, attributeset attrs, int defstyle ){
- Super (context, attrs, defstyle );
- }
- @ Override
- Public void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
- Int expandspec = measurespec. makemeasurespec (integer. max_value> 2,
- Measurespec. at_most );
- Super. onmeasure (widthmeasurespec, expandspec );
- }
- }