安卓实现二级菜单功能

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

收缩状态

展开状态

相关推荐
tmacfrank4 小时前
Android 性能优化入门(二)—— 内存优化
android·性能优化
奔跑吧 android4 小时前
【android bluetooth 协议分析 02】【bluetooth hal 层详解 3】【高通蓝牙hal主要流程介绍-上】
android·bluetooth·qcom·bt_hal_1.0·qcom_bt·高通蓝牙hal
追随远方6 小时前
Android OkHttp控制链:深入理解网络请求的流程管理
android·网络·okhttp
stevenzqzq8 小时前
kotlin flow的两种SharingStarted策略的区别
android·flow
掘金-我是哪吒8 小时前
分布式微服务系统架构第138集:打包发布全流程(iOS + Android)
android·微服务·云原生·架构
奔跑吧 android9 小时前
【android bluetooth 协议分析 01】【HCI 层介绍 9】【ReadLocalSupportedCommands命令介绍】
android·bluetooth·bt·aosp13·hcicmd·bt5.3
小邓是个人才呀10 小时前
第二章:Android常用UI控件
android·java·ui
许三多202015 小时前
Android studio Could not move temporary workspace
android·ide·android studio·workspace
王江奎16 小时前
FFmpeg中使用Android Content协议打开文件设备
android·ffmpeg
清霜之辰16 小时前
安卓开发用到的设计模式(1)创建型模式
android·设计模式·创建型模式