欢迎访问宙启技术站
智能推送

Android中怎么实现二级菜单展示

发布时间:2023-05-16 17:50:53

一、概述

二级菜单是指在主菜单的某个选项下再展示出一级菜单的设计方式,常用于大型应用中对功能进行分类,方便用户查找和使用。Android中实现二级菜单展示,可以采用多种方式,如使用ListView、RecyclerView、ExpandableListView等控件来展示,本文将详细介绍使用ExpandableListView来实现二级菜单展示的步骤。

二、实现步骤

1.准备数据

在ExpandableListView中,需要准备一个二维数组, 维代表一级菜单的个数,第二维代表一级菜单下的二级菜单的个数。同时,还需要准备两个列表,一个是一级菜单列表,一个是二级菜单列表。

具体的数据结构如下:

//定义二维数组,      维代表一级菜单的个数,第二维代表每个一级菜单下的二级菜单的个数
String[][] childStrings = { 
        { "葡萄酒", "红酒", "白酒" }, 
        { "计算器", "日历", "设置" }, 
        { "十万个为什么", "小学数学", "地球知识" } 
        };
        
//一级菜单列表
ArrayList<String> groupData = new ArrayList<String>(Arrays.asList("饮品", "工具", "知识"));
        
//二级菜单列表
ArrayList<ArrayList<String>> childData = new ArrayList<ArrayList<String>>();
for (int i = 0; i < groupData.size(); i++) {
    ArrayList<String> child = new ArrayList<String>();
    for (int j = 0; j < childStrings[i].length; j ++) {
        child.add(childStrings[i][j]);
    }
    childData.add(child);
}

2.创建布局文件

在布局文件中,可以先创建一个ExpandableListView控件,用来展示二级菜单列表。同时,还需要为一级菜单和二级菜单分别创建一个单独的布局文件。

1)创建ExpandableListView布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</LinearLayout>

2)创建一级菜单布局文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/groupText"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center_vertical"
    android:paddingLeft="20dp"
    android:textSize="18sp"
    android:textColor="@android:color/black" />

3)创建二级菜单布局文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/childText"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center_vertical|left"
    android:paddingLeft="45dp"
    android:textSize="16sp"
    android:textColor="@android:color/black" />

3.创建Adapter

在Adapter中主要实现三个方法:getGroupCount()方法返回一级菜单个数,getChildrenCount(int groupPosition)方法返回对应一级菜单下的二级菜单个数,getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)方法返回对应的二级菜单View。

/**
 * 二级菜单适配器
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ArrayList<String> groupData;
    private ArrayList<ArrayList<String>> childData;

    public MyExpandableListAdapter(Context context, ArrayList<String> groupData, ArrayList<ArrayList<String>> childData) {
        this.mContext = context;
        this.groupData = groupData;
        this.childData = childData;
    }

    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childData.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView groupText;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_group, null);
        }
        groupText = convertView.findViewById(R.id.groupText);
        String groupName = (String) getGroup(groupPosition);
        groupText.setText(groupName);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView childText;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_child, null);
        }
        childText = convertView.findViewById(R.id.childText);
        String childName = (String) getChild(groupPosition, childPosition);
        childText.setText(childName);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

4.设置Adapter

将Adapter设置到ExpandableListView中。

MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(this, groupData, childData);
mExpandableListView.setAdapter(mAdapter);

5.设置二级菜单点击事件

由于二级菜单是被包含在一级菜单中的,因此需要分别设置一级菜单和二级菜单的点击事件。

1)设置一级菜单点击事件

mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        //如果返回true,点击一级菜单时不展开下级菜单
        return false;
    }
});

2)设置二级菜单点击事件

mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        //处理二级菜单点击事件
        return true;
    }
});

至此,就完成了Android中通过ExpandableListView控件实现二级菜单展示的步骤。

三、注意事项

1.在一级菜单或者二级菜单中可以添加ImageView等控件,但需要自己实现对应控件的点击事件。

2.在Adapter中需要实现getGroupView()和getChildView()两个方法,分别用来返回一级菜单和二级菜单的View。

3.在设置Adapter时,groupData和childData的长度需要一致。

4.在设置点击事件时,需要注意返回值,如onGroupClick()方法返回true时,点击一级菜单时不展开下级菜单。

5.在处理点击事件时,可以通过parent的getPositionForView()方法获取点击的菜单项在列表中的位置,也可以通过getItemAtPosition()方法获取点击菜单项的数据。