In Android programming, when a ListView is nested in ScrollView, the size of the ListView is not calculated correctly. The solution is as follows:
(Non-original, online search solution)
Public classMainactivityextendsActivity {PrivateListView ListView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView=(ListView) Findviewbyid (R.ID.LISTVIEW1); String[] Adapterdata=NewString[] {"Afghanistan", "Albania",... ... "Bosnia"}; Listview.setadapter (NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1,adapterdata)); Setlistviewheightbasedonchildren (ListView); } Public voidSetlistviewheightbasedonchildren (ListView listview) {//get the adapter for the ListViewListAdapter ListAdapter =Listview.getadapter (); if(ListAdapter = =NULL) { 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); //Calculate the width height of a child viewListitem.measure (0, 0); //to count the total height of all childrenTotalheight + =listitem.getmeasuredheight (); } viewgroup.layoutparams params=Listview.getlayoutparams (); Params.height= totalheight+ (Listview.getdividerheight () * (Listadapter.getcount ()-1)); //Listview.getdividerheight () Gets the height of the delimiter between children//Params.height finally get the entire ListView full display required heightlistview.setlayoutparams (params); } }
Nested listview in Android ScrollView displays only one row of the workaround