ListView 只展示第一行

写了一个Layout文件,最外层是一个 ScrollView  ,其中包含了两个 ListView  和其他的组件。

但运行时发现 :只展示了 listview 的 index = 0 的view

   <ListView 

       android:id="@+id/vedioUrlListView"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:divider="@null"></ListView>

The vertically scrolling ScrollView should not contain another vertically scrolling widget (ListView)

解决方法:在 代码中重新 遍历 listview 的子   ,根据子的高度值,重新设置一下 listview 的高度值。

vedioListView.setAdapter(vedioUrlAdapter);

setListViewHeightBasedOnChildren(vedioListView);

	public  void setListViewHeightBasedOnChildren(ListView listView) {
		// get the list view adapter, so this function must be invoked after set the adapter.
		BaseAdapter listAdapter = (BaseAdapter) listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		
		int totalHeight = 0;
		// get the ListView count
		int count = listAdapter.getCount();
		for (int i = 0; i < count; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			// measure the child view
			listItem.measure(0, 0);
			// calculate the total height of items
			totalHeight += listItem.getMeasuredHeight();
		}
		
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		// get divider height for all items and add the total height
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
    }

测试成功。

猜你喜欢

转载自zhangjie0919.iteye.com/blog/2078216