1.给ScrollView设置属性:android:fillViewport="true",如果ListView加载的数据不多的话,确实可以解决,,但是当ListView加载的数据较多的时候,仍旧无法显示完全,并且这个时候ListView自身也无法滚动了。
2.计算listview高度
附:整理了网上的一种解决方法,实际测试效果还不错。
01 | public class ListViewTestActivity extends Activity { |
04 | public void onCreate(Bundle savedInstanceState) { |
05 | super .onCreate(savedInstanceState); |
06 | setContentView(R.layout.sub_menu2); |
08 | ListView listView = (ListView) findViewById(R.id.sub_menu_list); |
09 | MyAdapter adapter = new MyAdapter( this , addMapToList()); |
10 | listView.setAdapter(adapter); |
11 | setListViewHeightBasedOnChildren(listView); |
17 | private void setListViewHeightBasedOnChildren(ListView listView) { |
19 | ListAdapter listAdapter = listView.getAdapter(); |
20 | if (listAdapter == null ) { |
25 | for ( int i = 0 ; i < listAdapter.getCount(); i++) { |
26 | View listItem = listAdapter.getView(i, null , listView); |
27 | listItem.measure( 0 , 0 ); |
28 | totalHeight += listItem.getMeasuredHeight(); |
31 | ViewGroup.LayoutParams params = listView.getLayoutParams(); |
32 | params.height = totalHeight |
33 | + (listView.getDividerHeight() * (listAdapter.getCount() - 1 )); |
34 | listView.setLayoutParams(params); |
43 | private List<HashMap<String, String>> addMapToList() { |
45 | List<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>(); |
46 | HashMap<String, String> map; |
48 | for ( int i = 0 ; i < 18 ; i++) { |
49 | map = new HashMap<String, String>(); |
50 | map.put( "name" , "Item-" + (i + 1 )); |
发现有时出现scrollview默认位置不在最上方的情况。在scrollview外添加一个edittextview加上 <requestFocus />原理不明。 隐藏不行,宽高设为0