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

Android开发:如何在Drawer Layout中实现ListView下方的ExpandableListView?

解决DrawerLayout中同时放置ListView和ExpandableListView的布局问题

嘿,我懂你作为Android新手碰到这个布局难题的郁闷!先看看你代码里的核心问题:你现在给DrawerLayout加了两个独立的抽屉子View——一个是包着ExpandableListView的LinearLayout,另一个是单独的ListView,而且两者都设置了android:layout_gravity="left"、高度都是match_parent,这直接导致两个列表重叠在一起,自然没法正常显示啦。

正确的思路是把ListView和ExpandableListView放在同一个垂直方向的LinearLayout里,将这个LinearLayout作为DrawerLayout的单个抽屉子View,这样就能让它们在侧边抽屉里上下有序排列。另外要注意DrawerLayout的标准结构:第一个子View是占满屏幕的主内容区,第二个子View才是侧边抽屉。

下面是修改后的activity_main.xml代码:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="dev.edmt.expandablelistview.MainActivity">

    <!-- 主内容区域:这里放APP的核心界面内容,比如TextView或Fragment容器 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="主内容区域"
            android:textSize="24sp"
            android:gravity="center"
            android:padding="20dp"/>
    </LinearLayout>

    <!-- 侧边抽屉容器:将ListView和ExpandableListView垂直排列 -->
    <LinearLayout
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:orientation="vertical"
        android:background="#b9cae5">

        <!-- 上方的ListView:自适应内容高度 -->
        <ListView
            android:id="@+id/drawerList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            android:dividerHeight="1dp"/>

        <!-- 下方的ExpandableListView:占据剩余空间 -->
        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null"/>
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

再给你几个实用小提示:

  • 给ListView设置layout_height="wrap_content",让它根据内容自动调整高度,剩余空间交给ExpandableListView(设为match_parent)
  • 可以在两个列表之间加个分割线,比如在LinearLayout里插入一个高度为2dp、背景色为深色的View
  • 在MainActivity里分别给两个列表设置适配器即可,这里给你贴个示例代码:
public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private ListView drawerList;
    private ExpandableListView expandableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        drawerLayout = findViewById(R.id.activity_main);
        drawerList = findViewById(R.id.drawerList);
        expandableListView = findViewById(R.id.lvExp);

        // 设置ListView适配器(示例数据)
        List<String> listData = new ArrayList<>();
        listData.add("首页");
        listData.add("设置");
        listData.add("关于");
        ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
        drawerList.setAdapter(listAdapter);

        // 设置ExpandableListView适配器(示例数据)
        List<String> groupTitles = Arrays.asList("分类A", "分类B");
        Map<String, List<String>> childData = new HashMap<>();
        childData.put("分类A", Arrays.asList("子项A1", "子项A2"));
        childData.put("分类B", Arrays.asList("子项B1", "子项B2", "子项B3"));

        SimpleExpandableListAdapter expandableAdapter = new SimpleExpandableListAdapter(
                this,
                // 分组数据转换为适配格式
                getGroupData(groupTitles),
                android.R.layout.simple_expandable_list_item_1,
                new String[]{"group_title"},
                new int[]{android.R.id.text1},
                // 子项数据转换为适配格式
                getChildData(groupTitles, childData),
                android.R.layout.simple_list_item_1,
                new String[]{"child_title"},
                new int[]{android.R.id.text1}
        );
        expandableListView.setAdapter(expandableAdapter);
    }

    // 辅助方法:处理分组数据格式
    private List<Map<String, String>> getGroupData(List<String> titles) {
        List<Map<String, String>> groupList = new ArrayList<>();
        for (String title : titles) {
            Map<String, String> map = new HashMap<>();
            map.put("group_title", title);
            groupList.add(map);
        }
        return groupList;
    }

    // 辅助方法:处理子项数据格式
    private List<List<Map<String, String>>> getChildData(List<String> groups, Map<String, List<String>> childMap) {
        List<List<Map<String, String>>> childList = new ArrayList<>();
        for (String group : groups) {
            List<Map<String, String>> children = new ArrayList<>();
            for (String child : childMap.get(group)) {
                Map<String, String> map = new HashMap<>();
                map.put("child_title", child);
                children.add(map);
            }
            childList.add(children);
        }
        return childList;
    }
}

这样修改后,你的ListView和ExpandableListView就能在侧边抽屉里正常上下排列、各自显示内容啦!如果还有列表点击事件、抽屉开关逻辑这类需求,随时调整就行~

内容的提问来源于stack exchange,提问作者karan1.singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:03:09