如何在ScrollView中如何嵌入ListView

如何在 ScrollView 中如何嵌入 ListView

在 ScrollView 添加一个 ListView 会导致 listview 控件显示不全,通常只会显示一条,这是因为两个控件的滚动
事件冲突导致。所以需要通过 listview 中的 item 数量去计算 listview 的显示高度,从而使其完整展示,如下提供一个
方法供大家参考。
如下图,在 ScrollView 中嵌套了 ListView,ListView 展现的是物流状态。



lv = (ListView) findViewById(R.id.lv);
adapter = new MyAdapter();
lv.setAdapter(adapter);
setListViewHeightBasedOnChildren(lv);

public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() *
(listAdapter.getCount() - 1));
params.height += 5;// if without this statement,the listview will be a
// little short
listView.setLayoutParams(params);
}


布局界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ScrollView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

注意:如果直接将 ListView 放到 ScrollView 中,那么上面的代码依然是没有效果的.必须将 ListVIew 放到
LinearLayout 等其他容器中才行。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ScrollView加入ListView会导致ListView无法滑动,因为ScrollView会拦截ListView的滑动事件。可以考虑不使用ScrollView,直接使用ListView,并将需要放入ScrollView的内容作为HeaderView或FooterView添加到ListView。如果非要使用ScrollView,可以将ListView的高度设置为固定值,让ScrollView不拦截ListView的滑动事件。具体实现方式如下: 1. 在布局文件,先定义一个ScrollView,然后在ScrollView再定义一个ListView,并将ListView的高度设置为固定值,例如: ```xml <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 这里定义一个ListView,并将其高度设置为固定值 --> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="500dp" /> </ScrollView> ``` 2. 在代码,先获取ListView的高度,然后将其设置为固定值,如下: ```java ListView listView = findViewById(R.id.list_view); int listViewHeight = listView.getLayoutParams().height; listView.getLayoutParams().height = 500; listView.requestLayout(); ``` 这样就可以在ScrollView加入ListView了。但是需要注意的是,这种做法可能会导致ListView的部分内容无法显示出来,因为ListView的高度被设置为了固定值。如果ListView的高度不够,可以在ScrollView再添加一个布局容器,将其作为ListView的FooterView,这样就可以显示完整的ListView内容了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值