Android ExpandableListView双层嵌套实现三级树形菜单

  

在Android开发中,列表可以说是最常见的了,一般都是使用列表视图,当涉及到二维数组时,更多的使用到ExpandableListView,然而当数据结构比较复杂时,就需要使用三级菜单或者更多级的菜单来显示,这就让人比较头疼了,最近做的项目就涉及到了三级菜单,遇到了不少问题,虽然不够完美,但是基本需求实现了,在此记录一下。(之前见过有人使用ListView实现4级、5级甚至更多级菜单的,是在适配器的数据源里定义的结构,根据等级缩进左间距的倍数,链接地址找不到了,有兴趣的可以自己找找)

  

先上效果图:

  

 Android ExpandableListView双层嵌套实现三级树形菜单

  

 Android ExpandableListView双层嵌套实现三级树形菜单”> <br/>
  </p>
  <p>简单介绍下重点,为了简便,把第一层ExpandableListView称之为EListOne,相应的适配器称之为AdpOne;第二层ExpandableListView称之为EListTwo,相应的适配器称之为AdpTwo。</p>
  <p>首先第一个要处理的问题是在AdpOne的getChildView方法中,需要对EListTwo的高度进行动态计算,因为EListTwo展开和关闭时高度是不一样的,所以要在EListTwo的setOnGroupExpandListener和setOnGroupCollapseListener方法中做相应的处理:</p>
  <p> </p>
  
  <pre类=/* *   * @author冷漠,恒   *   *子ExpandableListView展开时,因为集团只有一项,所以子ExpandableListView的总高度=*(子ExpandableListView的孩子数量+ 1)*每一项的高度   * */eListView.setOnGroupExpandListener (new>/* *   * @author冷漠,恒   *   *点击子ExpandableListView子项时,调用回调接口   * */eListView.setOnChildClickListener (new>   包com.heng.tree;      进口java.util.ArrayList;      进口com.heng.tree.R;   进口com.heng.tree.adapter.ParentAdapter;   进口com.heng.tree.adapter.ParentAdapter.OnChildTreeViewClickListener;   进口com.heng.tree.entity.ChildEntity;   进口com.heng.tree.entity.ParentEntity;      进口android.app.Activity;   进口android.content.Context;   进口android.graphics.Color;   进口android.os.Bundle;   进口android.widget.ExpandableListView;   进口android.widget.ExpandableListView.OnGroupExpandListener;   进口android.widget.Toast;/* *   *   * @author冷漠,恒   *   * & lt; br/比;   *   * shexiaoheng@163.com   *   * @blog   * & lt; a href=' http://blog.csdn.net/shexiaoheng '在http://blog.csdn.net/shexiaoheng   包com.heng.tree.adapter;      进口java.util.ArrayList;      进口android.content.Context;   进口android.view.LayoutInflater;   进口android.view.View;   进口android.view.ViewGroup;   进口android.widget.BaseExpandableListAdapter;   进口android.widget.ExpandableListView;   进口android.widget.ExpandableListView.OnChildClickListener;   进口android.widget.ExpandableListView.OnGroupCollapseListener;   进口android.widget.ExpandableListView.OnGroupExpandListener;   进口android.widget.AbsListView.LayoutParams;   进口android.widget.TextView;      进口com.heng.tree.R;   进口com.heng.tree.entity.ChildEntity;   进口com.heng.tree.entity.ParentEntity;/* *   *   * @author冷漠,恒   *   *父类分组的适配器   *   * & lt; br/比;   * & lt; br/比;   *   *方法{@link # getChildView (int, int,布尔、视图ViewGroup)} & lt; b> & lt;字体   *颜色=#就的大?' 2 '祝辞极其重要& lt;/font> & lt;/b>   *   * */公开课ParentAdapter延伸BaseExpandableListAdapter {      私人上下文mContext;//上下文      私人ArrayListmParents;//数据源      私人>   包com.heng.tree.adapter;      进口java.util.ArrayList;      进口com.heng.tree.R;   进口com.heng.tree.entity.ChildEntity;      进口android.content.Context;   进口android.graphics.Color;   进口android.view.LayoutInflater;   进口android.view.View;   进口android.view.ViewGroup;   进口android.widget.BaseExpandableListAdapter;   进口android.widget.TextView;/* *   *   * @author冷漠,恒   *   * & lt; br/比;   * & lt; br/比;   *   *子类分组的适配器   *   * & lt; br/比;   * & lt; br/比;   *   *方法{@link # isChildSelectable (int, int)} & lt; b> & lt;字体颜色=#就的   *大?' 2 '祝辞必须返回true

Android ExpandableListView双层嵌套实现三级树形菜单