安卓实现二级菜单功能

activity_main.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

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

收缩状态

在这里插入图片描述

展开状态

在这里插入图片描述

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值