安卓实现二级菜单功能

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

收缩状态

展开状态

相关推荐
亘元有量-流量变现7 分钟前
ASO优化全流程实操指南:从基础到迭代,精准提升App曝光与转化
android·ios·harmonyos·aso优化·方糖试玩
私人珍藏库33 分钟前
【Android】GameNative 0.9.0 [特殊字符] 手机畅玩Steam游戏
android·游戏·智能手机·app·工具·软件·多功能
诸神黄昏EX1 小时前
Android Safety 系列专题【篇七:Android AVF机制】
android
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.1 小时前
MySQL 主从架构中的使用技巧及优化
android·mysql·架构
羊小蜜.1 小时前
Mysql 11: 存储过程全解——从创建到使用
android·数据库·mysql·存储过程
zh_xuan1 小时前
Android compose和传统view混用
android
大黄说说2 小时前
MySQL索引失效的常见场景有哪些?如何通过EXPLAIN分析查询性能?
android·adb
冰糖葫芦三剑客2 小时前
华为 Android APP 应用内生成合成内容的文件元数据中添加隐式标识的截图 开发要怎么生成?
android·华为
羊小蜜.2 小时前
Mysql 12: 视图全解——从创建到使用
android·数据库·mysql·视图
zh_xuan4 小时前
Android 传统view嵌入compose
android