You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何在ScrollView内使用多个ExpandableListView?

实现3个ExpandableListView的布局方案

没问题!要在单屏幕里放置3个ExpandableListView,用ScrollView包裹垂直LinearLayout是很合适的方案——这样当屏幕空间不够时,用户可以滚动查看全部内容。我帮你补全并优化了布局代码,直接就能用:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".fragments.FragmentTestWellnessScoreBoard">

    <!-- 垂直线性布局,用来容纳3个ExpandableListView -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:gap="12dp"> <!-- 给每个列表之间留间距,视觉更舒适 -->

        <!-- 第一个ExpandableListView -->
        <ExpandableListView
            android:id="@+id/expandable_list_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nestedScrollingEnabled="true" <!-- 解决ScrollView和内部列表的滚动冲突 -->
            android:divider="@color/light_gray"
            android:dividerHeight="1dp"
            android:groupIndicator="@drawable/ic_expand_indicator" /> <!-- 可自定义展开/收起图标 -->

        <!-- 第二个ExpandableListView -->
        <ExpandableListView
            android:id="@+id/expandable_list_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nestedScrollingEnabled="true"
            android:divider="@color/light_gray"
            android:dividerHeight="1dp"
            android:groupIndicator="@drawable/ic_expand_indicator" />

        <!-- 第三个ExpandableListView -->
        <ExpandableListView
            android:id="@+id/expandable_list_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nestedScrollingEnabled="true"
            android:divider="@color/light_gray"
            android:dividerHeight="1dp"
            android:groupIndicator="@drawable/ic_expand_indicator" />

    </LinearLayout>

</ScrollView>

几个关键注意点:

  • 每个ExpandableListView都设置了独特的android:id,方便你在Fragment/Activity中分别绑定对应的适配器
  • android:nestedScrollingEnabled="true"是为了让内部列表和外层ScrollView的滚动更协调,这个属性从API 21开始支持;如果要兼容更低版本,可以自定义ExpandableListView并重写onMeasure方法,强制它测量自身的完整高度:
public class NonScrollExpandableListView extends ExpandableListView {
    public NonScrollExpandableListView(Context context) {
        super(context);
    }

    public NonScrollExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 让列表测量完整高度,避免滚动冲突
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
  • 布局里的android:gap和android:padding是为了提升视觉体验,你可以根据自己的UI需求调整数值
  • 如果你需要自定义展开/收起的图标,只需要替换android:groupIndicator指向你的自定义drawable即可

内容的提问来源于stack exchange,提问作者Vanraj Ghed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:31:47