1 private ScrollView scrollView; 2 private ListView listview; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 this.setContentView(R.layout.activity_show); 8 scrollView = (ScrollView) findViewById(R.id.scrollView1); 9 listview = (ListView) findViewById(R.id.listView1); 10 listview.setAdapter(new ArrayAdapter<String>(activity_show.this, 11 android.R.layout.simple_list_item_1, getData())); // 使用系统adapter 填充 12 ListAdapter adapter = listview.getAdapter(); // 通过反射获取 13 int count = adapter.getCount(); // 得到长度 14 int totalHeight = 0; //总的高度 15 int height=0; //分割线的高度 16 for (int i = 0; i < count; i++) { 17 View view = adapter.getView(i, null, listview); // 通过反射获取 18 view.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED); //设置样式 19 totalHeight += view.getMeasuredHeight(); //叠加高度 20 } 21 height=listview.getDividerHeight()*(count-1); //获取分割符的高度 22 totalHeight+=height; //总高度 23 //社会到listview view中 24 ViewGroup.LayoutParams params=listview.getLayoutParams(); 25 //设置高度 26 params.height=totalHeight; 27 listview.setLayoutParams(params); //添加listview 28 }
Android上下滑动,scollView和Listview结合使用,listview的优化.....
原文:http://www.cnblogs.com/tan520/p/5169778.html