安卓实现二级菜单功能

activity_main.xml

xml 复制代码
<RelativeLayout 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"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

MainActivity.java

java 复制代码
package com.example.demo3;

import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;

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

        expandableListView = findViewById(R.id.expandableListView);
        expandableListView.setGroupIndicator(null);//设置一级标题下拉的箭头不显示
        // 数据源
        List<Map<String, String>> groupData = new ArrayList<>();
        List<List<Map<String, String>>> childData = new ArrayList<>();

        // 一级菜单项1
        Map<String, String> group1 = new HashMap<>();
        group1.put("group", "Group 1");
        groupData.add(group1);
        //一级菜单项2
        Map<String, String> group2 = new HashMap<>();
        group2.put("group", "Group 2");
        groupData.add(group2);

        // 一级菜单1项对应的二级菜单项
        List<Map<String, String>> child1 = new ArrayList<>();
        Map<String, String> child1Item1 = new HashMap<>();
        child1Item1.put("child", "Child 1.1");
        child1.add(child1Item1);
        Map<String, String> child1Item2 = new HashMap<>();
        child1Item2.put("child", "Child 1.2");
        child1.add(child1Item2);
        childData.add(child1);


        // 一级菜单2项对应的二级菜单项
        List<Map<String, String>> child2 = new ArrayList<>();
        Map<String, String> child2Item1 = new HashMap<>();
        child2Item1.put("child", "Child 2.1");
        child2.add(child2Item1);
        Map<String, String> child2Item2 = new HashMap<>();
        child2Item2.put("child", "Child 2.2");
        child2.add(child2Item2);
        childData.add(child2);

        // 设置适配器
        ExpandableListAdapter adapter = new SimpleExpandableListAdapter(this,
                groupData, android.R.layout.simple_expandable_list_item_1, new String[]{"group"}, new int[]{android.R.id.text1},
                childData, android.R.layout.simple_expandable_list_item_2, new String[]{"child"}, new int[]{android.R.id.text1}
        );

        expandableListView.setAdapter(adapter);
    }
}

收缩状态

展开状态

相关推荐
aqi0025 分钟前
FFmpeg开发笔记(九十六)采用Kotlin+Compose的视频编辑器OpenVideoEditor
android·ffmpeg·kotlin·音视频·流媒体
诸神黄昏EX1 小时前
Android Safety 系列专题【篇一:系统签名】
android
ll_god2 小时前
android gradle中如何引用 libs.versions.toml中定义的版本变量添加compose引用
android
行稳方能走远2 小时前
Android C++ 学习笔记
android·c++
2501_946230982 小时前
Cordova&OpenHarmony用户账户管理
android·javascript
x66ccff2 小时前
Claude Code 安装方法
android·java·数据库
码农搬砖_20203 小时前
【一站式学会compose】 Android UI体系之 Image的使用和介绍
android·image·compose·content·contentscale·scaletype
粤M温同学3 小时前
Android Room数据库的基本使用
android·数据库
lkbhua莱克瓦243 小时前
基础-约束
android·开发语言·数据库·笔记·sql·mysql·约束
戴西软件3 小时前
CAxWorks.VPG车辆工程仿真软件:打造新能源汽车安全的“数字防线“
android·大数据·运维·人工智能·安全·低代码·汽车