安卓实现二级菜单功能

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);
    }
}

收缩状态

展开状态

相关推荐
喜欢踢足球的老罗3 小时前
自动化模型管理:MediaPipe Android SDK 中的模型文件下载与加载机制
android·运维·自动化
AgilityBaby5 小时前
Untiy打包安卓踩坑
android·笔记·学习·unity·游戏引擎
硬件学长森哥6 小时前
Android音视频多媒体开源框架基础大全
android·图像处理·音视频
二流小码农7 小时前
鸿蒙开发:CodeGenie万能卡片生成
android·ios·harmonyos
没有了遇见7 小时前
Android 直播间动画动画队列实现
android
月山知了7 小时前
Android有的命令不需要root权限,有的命令需要root权限是如何实现的
android
科技道人8 小时前
Android 实体键盘 设置默认布局
android·实体键盘·设置默认键盘语言
SHUIPING_YANG8 小时前
tp3.1临时连接指定数据库,切片分类in查询,带过滤需要的数据
android·数据库
前端呆猿8 小时前
Vuex:Vue.js 应用程序的状态管理模式
android·vue.js·flutter
望佑9 小时前
Jetpack Compose 入门:从默认工程到实战开发
android