Android提高之多级树形菜单的实现方法

一般来说在Android里要实现树形菜单,都是用ExpandableList(也有高手自己继承ListView或者LinearLayout来做),但是ExpandableList一般只能实现2级树形菜单。本文所述实例也依然使用ExpandableList,但是要实现的是3级树形菜单。

本文程序运行效果图如下图所示:

当用BaseExpandableListAdapter来实现二级树形菜单时,父项(getGroupView())和子项(getChildView())都是使用TextView。当要实现三级树形菜单时,子项(getChildView())就必须使用ExpandableList了。另外还要定义结构体来方便调用三级树形的数据,二级树形菜单可以用如下:

static public class TreeNode{
 Object parent;
 List<Object> childs=new ArrayList<Object>();
}

三级树形菜单可以用如下,子项是二级树形菜单的结构体如下所示:

static public class SuperTreeNode {
 Object parent;
 //二级树形菜单的结构体
 List<TreeViewAdapter.TreeNode> childs = new ArrayList<TreeViewAdapter.TreeNode>();
}

实现三级树形菜单有两点要注意的:

1、第二级也是个树形菜单,因此必须在第二级项目展开/回收时设置足够的空间来完全显示二级树形菜单;

2、在实现三级树形菜单时,发现菜单的方法都是用不了(如OnChildClickListener、OnGroupClickListener等),因此要获得选中的数据就必须在外部定义好回调函数,然后在第二级生成二级树形菜单时回调这个外部函数。

此外还需要注意:本文在解决No.2关键点的时候,只能取得第三级选中的序号。而第一,第二级依然无法获取其序号。

main.xml源码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="wrap_content" android:layout_height="wrap_content">
 <Button android:layout_height="wrap_content" android:text="两层结构"
  android:layout_width="160dip" android:id="@+id/btnNormal"></Button>
 <Button android:layout_height="wrap_content" android:text="三层结构"
  android:layout_width="160dip" android:id="@+id/btnSuper"></Button>
 </LinearLayout>
 <ExpandableListView android:id="@+id/ExpandableListView01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"></ExpandableListView>
</LinearLayout>

testExpandableList.java是主类,调用其他工具类,源码如下:

package com.testExpandableList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class testExpandableList extends Activity {
  /** Called when the activity is first created. */
 ExpandableListView expandableList;
 TreeViewAdapter adapter;
 SuperTreeViewAdapter superAdapter;
 Button btnNormal,btnSuper;
  // Sample data set. children[i] contains the children (String[]) for groups[i].
  public String[] groups = { "xxxx好友", "xxxx同学", "xxxxx女人"};
  public String[][] child= {
      { "A君", "B君", "C君", "D君" },
      { "同学甲", "同学乙", "同学丙"},
      { "御姐", "萝莉" }
  };
  public String[] parent = { "xxxx好友", "xxxx同学"};
  public String[][][] child_grandson= {
   {{"A君"},
    {"AA","AAA"}},
   {{"B君"},
    {"BBB","BBBB","BBBBB"}},
   {{"C君"},
    {"CCC","CCCC"}},
   {{"D君"},
    {"DDD","DDDD","DDDDD"}},
  };
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.setTitle("ExpandableListView练习----hellogv");
    btnNormal=(Button)this.findViewById(R.id.btnNormal);
    btnNormal.setOnClickListener(new ClickEvent());
    btnSuper=(Button)this.findViewById(R.id.btnSuper);
    btnSuper.setOnClickListener(new ClickEvent());
    adapter=new TreeViewAdapter(this,TreeViewAdapter.PaddingLeft>>1);
    superAdapter=new SuperTreeViewAdapter(this,stvClickEvent);
    expandableList=(ExpandableListView) testExpandableList.this.findViewById(R.id.ExpandableListView01);
  }
  class ClickEvent implements View.OnClickListener{
 @Override
 public void onClick(View v) {
  adapter.RemoveAll();
  adapter.notifyDataSetChanged();
  superAdapter.RemoveAll();
  superAdapter.notifyDataSetChanged();

  if(v==btnNormal)
  {
     List<TreeViewAdapter.TreeNode> treeNode = adapter.GetTreeNode();
     for(int i=0;i<groups.length;i++)
     {
      TreeViewAdapter.TreeNode node=new TreeViewAdapter.TreeNode();
      node.parent=groups[i];
      for(int ii=0;ii<child[i].length;ii++)
      {
      node.childs.add(child[i][ii]);
      }
      treeNode.add(node);
     }
     adapter.UpdateTreeNode(treeNode);
     expandableList.setAdapter(adapter);
     expandableList.setOnChildClickListener(new OnChildClickListener(){
   @Override
   public boolean onChildClick(ExpandableListView arg0, View arg1,
   int parent, int children, long arg4) {
   String str="parent id:"+String.valueOf(parent)+",children id:"+String.valueOf(children);
   Toast.makeText(testExpandableList.this, str, 300).show();
   return false;
   }
     });
  }
  else if(v==btnSuper){
  List<SuperTreeViewAdapter.SuperTreeNode> superTreeNode = superAdapter.GetTreeNode();
     for(int i=0;i<parent.length;i++)//第一层
     {
      SuperTreeViewAdapter.SuperTreeNode superNode=new SuperTreeViewAdapter.SuperTreeNode();
      superNode.parent=parent[i];

      //第二层
      for(int ii=0;ii<child_grandson.length;ii++)
       {
        TreeViewAdapter.TreeNode node=new TreeViewAdapter.TreeNode();
        node.parent=child_grandson[ii][0][0];//第二级菜单的标题

        for(int iii=0;iii<child_grandson[ii][1].length;iii++)//第三级菜单
        {
        node.childs.add(child_grandson[ii][1][iii]);
        }
        superNode.childs.add(node);
       }
      superTreeNode.add(superNode);
     }
     superAdapter.UpdateTreeNode(superTreeNode);
     expandableList.setAdapter(superAdapter);
  }
 }
  }
  /**
   * 三级树形菜单的事件不再可用,本函数由三级树形菜单的子项(二级菜单)进行回调
   */
  OnChildClickListener stvClickEvent=new OnChildClickListener(){
 @Override
 public boolean onChildClick(ExpandableListView parent,
  View v, int groupPosition, int childPosition,
  long id) {
  String str="parent id:"+String.valueOf(groupPosition)+",children id:"+String.valueOf(childPosition);
  Toast.makeText(testExpandableList.this, str, 300).show();
  return false;
 }
  };
}

TreeViewAdapter.java是实现二级树形菜单的工具类,源码如下:

package com.testExpandableList;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class TreeViewAdapter extends BaseExpandableListAdapter{
 public static final int ItemHeight=48;//每项的高度
 public static final int PaddingLeft=36;//每项的高度
 private int myPaddingLeft=0;//如果是由SuperTreeView调用,则作为子项需要往右移
 static public class TreeNode{
 Object parent;
 List<Object> childs=new ArrayList<Object>();
 }
 List<TreeNode> treeNodes = new ArrayList<TreeNode>();
 Context parentContext;
 public TreeViewAdapter(Context view,int myPaddingLeft)
 {
 parentContext=view;
 this.myPaddingLeft=myPaddingLeft;
 }
 public List<TreeNode> GetTreeNode()
 {
 return treeNodes;
 }
 public void UpdateTreeNode(List<TreeNode> nodes)
 {
 treeNodes=nodes;
 }
 public void RemoveAll()
 {
 treeNodes.clear();
 }
 public Object getChild(int groupPosition, int childPosition) {
 return treeNodes.get(groupPosition).childs.get(childPosition);
 }
 public int getChildrenCount(int groupPosition) {
 return treeNodes.get(groupPosition).childs.size();
 }
 static public TextView getTextView(Context context) {
 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
  ViewGroup.LayoutParams.FILL_PARENT, ItemHeight);
 TextView textView = new TextView(context);
 textView.setLayoutParams(lp);
 textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
 return textView;
 }
 public View getChildView(int groupPosition, int childPosition,
  boolean isLastChild, View convertView, ViewGroup parent) {
 TextView textView = getTextView(this.parentContext);
 textView.setText(getChild(groupPosition, childPosition).toString());
 textView.setPadding(myPaddingLeft+PaddingLeft, 0, 0, 0);
 return textView;
 }
 public View getGroupView(int groupPosition, boolean isExpanded,
  View convertView, ViewGroup parent) {
 TextView textView = getTextView(this.parentContext);
 textView.setText(getGroup(groupPosition).toString());
 textView.setPadding(myPaddingLeft+(PaddingLeft>>1), 0, 0, 0);
 return textView;
 }
 public long getChildId(int groupPosition, int childPosition) {
 return childPosition;
 }
 public Object getGroup(int groupPosition) {
 return treeNodes.get(groupPosition).parent;
 }
 public int getGroupCount() {
 return treeNodes.size();
 }
 public long getGroupId(int groupPosition) {
 return groupPosition;
 }
 public boolean isChildSelectable(int groupPosition, int childPosition) {
 return true;
 }
 public boolean hasStableIds() {
 return true;
 }
}

SuperTreeViewAdapter.java是实现三级树形菜单的工具类,会用到TreeViewAdapter.java,源码如下:

package com.testExpandableList;
import java.util.ArrayList;
import java.util.List;
import com.testExpandableList.TreeViewAdapter.TreeNode;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;
public class SuperTreeViewAdapter extends BaseExpandableListAdapter {
 static public class SuperTreeNode {
 Object parent;
 //二级树形菜单的结构体
 List<TreeViewAdapter.TreeNode> childs = new ArrayList<TreeViewAdapter.TreeNode>();
 }
 private List<SuperTreeNode> superTreeNodes = new ArrayList<SuperTreeNode>();
 private Context parentContext;
 private OnChildClickListener stvClickEvent;//外部回调函数
 public SuperTreeViewAdapter(Context view,OnChildClickListener stvClickEvent) {
 parentContext = view;
 this.stvClickEvent=stvClickEvent;
 }
 public List<SuperTreeNode> GetTreeNode() {
 return superTreeNodes;
 }
 public void UpdateTreeNode(List<SuperTreeNode> node) {
 superTreeNodes = node;
 }
 public void RemoveAll()
 {
 superTreeNodes.clear();
 }
 public Object getChild(int groupPosition, int childPosition) {
 return superTreeNodes.get(groupPosition).childs.get(childPosition);
 }
 public int getChildrenCount(int groupPosition) {
 return superTreeNodes.get(groupPosition).childs.size();
 }
 public ExpandableListView getExpandableListView() {
 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
  ViewGroup.LayoutParams.FILL_PARENT, TreeViewAdapter.ItemHeight);
 ExpandableListView superTreeView = new ExpandableListView(parentContext);
 superTreeView.setLayoutParams(lp);
 return superTreeView;
 }
 /**
 * 三层树结构中的第二层是一个ExpandableListView
 */
 public View getChildView(int groupPosition, int childPosition,
  boolean isLastChild, View convertView, ViewGroup parent) {
 // 是
 final ExpandableListView treeView = getExpandableListView();
 final TreeViewAdapter treeViewAdapter = new TreeViewAdapter(this.parentContext,0);
 List<TreeNode> tmp = treeViewAdapter.GetTreeNode();//临时变量取得TreeViewAdapter的TreeNode集合,可为空
 final TreeNode treeNode=(TreeNode) getChild(groupPosition, childPosition);
 tmp.add(treeNode);
 treeViewAdapter.UpdateTreeNode(tmp);
 treeView.setAdapter(treeViewAdapter);
 //关键点:取得选中的二级树形菜单的父子节点,结果返回给外部回调函数
 treeView.setOnChildClickListener(this.stvClickEvent);
 /**
  * 关键点:第二级菜单展开时通过取得节点数来设置第三级菜单的大小
  */
 treeView.setOnGroupExpandListener(new OnGroupExpandListener() {
  @Override
  public void onGroupExpand(int groupPosition) {
  AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
   ViewGroup.LayoutParams.FILL_PARENT,
   (treeNode.childs.size()+1)*TreeViewAdapter.ItemHeight + 10);
  treeView.setLayoutParams(lp);
  }
 });
 /**
  * 第二级菜单回收时设置为标准Item大小
  */
 treeView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
  @Override
  public void onGroupCollapse(int groupPosition) {

  AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
   TreeViewAdapter.ItemHeight);
  treeView.setLayoutParams(lp);
  }
 });
 treeView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);
 return treeView;
 }
 /**
 * 三级树结构中的首层是TextView,用于作为title
 */
 public View getGroupView(int groupPosition, boolean isExpanded,
  View convertView, ViewGroup parent) {
 TextView textView = TreeViewAdapter.getTextView(this.parentContext);
 textView.setText(getGroup(groupPosition).toString());
 textView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);
 return textView;
 }
 public long getChildId(int groupPosition, int childPosition) {
 return childPosition;
 }
 public Object getGroup(int groupPosition) {
 return superTreeNodes.get(groupPosition).parent;
 }
 public int getGroupCount() {
 return superTreeNodes.size();
 }
 public long getGroupId(int groupPosition) {
 return groupPosition;
 }
 public boolean isChildSelectable(int groupPosition, int childPosition) {
 return true;
 }
 public boolean hasStableIds() {
 return true;
 }
}

总结,使用ExpandableList实现三级树形菜单时有些bug不好解决,而且定义三维数组的时候也要倍加小心!所以尽量把数据化简来使用二级树形菜单。

希望本文所述代码对大家进行Android程序设计有一定的帮助。

(0)

相关推荐

  • 用dtree实现树形菜单 dtree使用说明

    准备工作: 请从我们http://www.jb51.net/jiaoben/31974.html下载dtree.zip文件 dtree.zip压缩包介绍: dtree是一个由JavaScript编写成的简单的树形菜单组件,目前免费并且开源. 目前有很多的树形菜单组件(比如ext),dtree是一种简单易懂的js组件, 不需要复杂的操作即可生产,同时支持动态从数据库引入数据 解压后有以下几部分: img文件夹: 包含树形菜单显示需要的图标 api.html : 作者写的dtree帮助文档 dtre

  • 无限级CSS树形菜单 Ver2.0

    新写的树:    1.解决了大数量加载过慢的问题    2.改进应用接口,可扩展性更强 先看效果吧,回头再写相关的使用参数文档:) 实例演示:    http://www.jb51.net/tools/cssTREE/index.html http://www.jb51.net/tools/cssTREE/index.big.html(大数据量(3000结点)效果演示) 代码下载:    http://www.jb51.net/tools/cssTREE/lTREE.rar 使用说明:    待

  • json+jQuery实现的无限级树形菜单效果代码

    本文实例讲述了json+jQuery实现的无限级树形菜单效果代码.分享给大家供大家参考.具体如下: 这里演示json树形菜单,JS无级树树形菜单,引入了jQuery插件,使用递归实现获取无级树数据并生成DOM结构,可以在JSON数据里 扩展无限级 看结构就明白. 先来看看运行效果截图: 在线演示地址如下: http://demo.jb51.net/js/2015/jquery-json-tree-style-menu-codes/ 具体代码如下: <!DOCTYPE html PUBLIC &quo

  • CSS TreeMenu 二级树形菜单示例

    二级树形菜单示例:CSS TreeMenu a,a:visited {color:#333;text-decoration:none;} a:hover {color:#f60;} body,td {font:13px "Geneva","宋体", "Arial", "Helvetica",sans-serif;} ul,li {margin:0;padding:0;list-style:none;} h1,h2,h3,h4,

  • 一个简单的js树形菜单

    我练习一下,以免不时之需. 树形菜单不过就是把普通菜单重新排列一下,看起来像树形而已. 上图京东的菜单,给他多几个嵌套,然后添加收缩伸展事件,差不多就行了. 给个例子: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>

  • Java递归如何正确输出树形菜单

    本文实例为大家分享了java递归输出树形菜单的具体代码,供大家参考,具体内容如下 首先我们要建立树节点的类: package com.tree; public class Node { private Integer id; private Integer parentId; private String name; private String link; public Integer getId() { return id; } public void setId(Integer id) {

  • 轻松学习jQuery插件EasyUI EasyUI创建树形菜单

    一.EasyUI使用标记创建树形菜单 一个树形菜单(Tree)可以从标记创建.easyui 树形菜单(Tree)也可以定义在 <ul> 元素中.无序列表的 <ul> 元素提供一个基础的树(Tree)结构.每一个 <li> 元素将产生一个树节点,子 <ul> 元素将产生一个父树节点. 创建树形菜单(Tree) <ul class="easyui-tree"> <li> <span>Folder</s

  • winform树形菜单无限级分类实例

    以下实现的是一个树形菜单,每一级对应一类窗口,点击叶子节点时弹出新的窗口: 用递归和委托实现的. 第一部分功能-创建树形菜单 复制代码 代码如下: /// <summary>         /// 创建树形菜单         /// </summary>         public void AddTree(int ParentID, TreeNode pNode)         {             // 数据库名字字段             string str

  • 以前写的两个CSS树形菜单

    第一次写的CSS树形菜单比较简单,只有二级目录,javascript代码总共不到十行,主要是靠CSS中的包含写法来实现的.演示地址:(1)http://www.cnlei.org/mycode/csstreemenu/index.html(2)http://cnlei.iecn.net/mycode/csstreemenu/index.html下载链接:(1)http://www.cnlei.org/mycode/csstreemenu/csstreemenu.rar(2)http://cnle

  • JS无限极树形菜单,json格式、数组格式通用示例

    修改了一下数据格式,是json和数组或者混合型的数据都通用,不用特定key等 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"&g

随机推荐