In Android programming, when ScrollView nests ListView, the size of ListView cannot be calculated correctly. There are two solutions:
Solution 1: Directly remove the ScrollView control containing the ListView control from the layout file and leave the ListView control. This is the easiest and fastest solution. If you must include the ListView in the ScrollView, please refer to Solution 2:
Remove the XML before Scroll:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/test_listView" android:layout_width="match_parent" android:layout_height="match_parent" android:fadingEdge="vertical"/> </LinearLayout> </ScrollView> </LinearLayout>
XML after removing ScrollView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/test_listView" android:layout_width="match_parent" android:layout_height="match_parent" android:fadingEdge="vertical" /> </LinearLayout> </LinearLayout>
Solution 2: Through code, calculate the height of the list based on the current ListView's list children:
public class MainActivity extends Activity { private ListView listView; private String[] adapterData = new String[] { "Title 1", "Title 2", "Title 3"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.mainActivity); listView = (ListView) findViewById(R.id.test_listView); listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData)); fixListViewHeight(listView); } public void fixListViewHeight(ListView listView) { // If no data adapter is set, the ListView has no children, return. ListAdapter listAdapter = listView.getAdapter(); int totalHeight = 0; if (listAdapter == null) { return; } for (int index = 0, len = listAdapter.getCount(); index < len; index++) { View listViewItem = listAdapter.getView(index , null, listView); // Calculate the width and height of the child View listViewItem.measure(0, 0); // Calculate the height and sum of all children totalHeight += listViewItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); // listView.getDividerHeight() gets the height of the divider between children // params.height sets the height required for the ListView to fully display params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } }
Original link: http://jingyan.baidu.com/article/afd8f4de4695af34e386e969.html