解决ScrollView嵌套ListView问题

本文聚焦ScrollView嵌套ListView问题,指出常见解决办法多无效。分析了ListView显示异常的原因在于scroll事件处理和高度设定。提出手动设置ListView高度的解决方案,给出代码示例,并提醒使用时需注意Adapter中布局类型及手动将ScrollView滚动至顶端。

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

解决ScrollView嵌套ListView问题

在学习开发中,曾多次碰到ScrollView嵌套ListView的问题,确认订单的页面,ScrollView中嵌套了ExpandableListView,ExpandableListView上面有固定的一些控件,下面也有固定的一些控件,整体又要能够滚动。 列表数据要嵌在固定数据中间,并且作为整体一起滚动。但是网上查到的解决办法好多无法真正解决,这里介绍一种亲测有效的办法。

先看一个例子

<ScrollView android:id="@+id/act_solution_1_sv" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\nListView上方数据\n" /> <ListView android:id="@+id/act_solution_1_lv" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\nListView下方数据\n" /> </LinearLayout> </ScrollView><ScrollView android:id="@+id/act_solution_1_sv" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\nListView上方数据\n" /> <ListView android:id="@+id/act_solution_1_lv" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\nListView下方数据\n" /> </LinearLayout> </ScrollView>
接着在ListView中放里面放了很多条数据,但是却只显示了1条。控件的属性设置上看没有什么问题,为什么不能像预期的那样?原因就是scroll事件的消费处理以及ListView控件的高度设定问题。

解决方案:手动设置ListView高度

经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量。于是手动代码设置ListView高度的方法就诞生了。

`/**

  • 动态设置ListView的高度
  • @param listView
    */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
    if(listView == null) return;
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
    // pre-condition
    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));
    listView.setLayoutParams(params);
    } `
    上面这个方法就是设定ListView的高度了,在为ListView设置了Adapter之后使用,就可以解决问题了。
    但是这个方法有个两个细节需要注意:
    一是Adapter中getView方法返回的View的必须由LinearLayout组成,因为只有LinearLayout才有measure()方法,如果使用其他的布局如RelativeLayout,在调用listItem.measure(0, 0);时就会抛异常,因为除LinearLayout外的其他布局的这个方法就是直接抛异常的,没理由…。我最初使用的就是这个方法,但是因为子控件的顶层布局是RelativeLayout,所以一直报错,不得不放弃这个方法。
    二是需要手动把ScrollView滚动至最顶端,因为使用这个方法的话,默认在ScrollView顶端的项是ListView,具体原因不了解可以在Activity中设置:
    sv = (ScrollView) findViewById(R.id.act_solution_1_sv);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值