ListView 只展示第一行

本文讨论了在Android应用开发中遇到的问题,即ScrollView与内部嵌套的ListView导致只展示ListView的第一个视图的情况。通过重新遍历ListView子元素并调整高度,最终成功解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了一个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);
    }

 

测试成功。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值