欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现菜单三级树效果

程序员文章站 2022-05-27 10:22:24
本文实例为大家分享了android实现菜单三级树展示的具体代码,供大家参考,具体内容如下 以下是部分代码,完整代码请在最下面点击下载: 【mainactivity】...

本文实例为大家分享了android实现菜单三级树展示的具体代码,供大家参考,具体内容如下

以下是部分代码,完整代码请在最下面点击下载:

【mainactivity】

public void treeonclick() {
 // todo auto-generated method stub
 superadapter.removeall();
 superadapter.notifydatasetchanged();
 string menucatname = getdate("menucatname");
 string menugroupname = getdate("menugroupname");
 string menuitemname = getdate("menuitemname");
 // 一级菜单名数组
 parent = menucatname.split("/");
 // 二级菜单名数组
 child = menugroupname.split("/");
 // 三级菜单名数组
 grandchild = menuitemname.split("/");
 
 list<supertreeviewadapter.supertreenode> supernodetree = superadapter
  .gettreenode();
 for (int i = 0; i < parent.length; i++) {
  supertreeviewadapter.supertreenode supernode = new supertreeviewadapter.supertreenode();
  supernode.parent = parent[i];
  for (int j = 0; j < child_grandchild.length; j++) {
  treeviewadapter.treenode node = new treeviewadapter.treenode();
  node.parent = child_grandchild[j][0][0];
  for (int k = 0; k < child_grandchild[j][1].length; k++) {
   node.childs.add(child_grandchild[j][1][k]);
  }
  supernode.childs.add(node);
  }
  supernodetree.add(supernode);
 }
 superadapter.updatetreenode(supernodetree);
 expandablelistview.setadapter(superadapter);
 
 }

【supertreeviewadapter】

public expandablelistview getexpandablelistview() {
 abslistview.layoutparams lp = new abslistview.layoutparams(
  viewgroup.layoutparams.fill_parent, treeviewadapter.itemheight);
 expandablelistview supertreeview = new expandablelistview(parentcontext);
 supertreeview.setlayoutparams(lp);
 // 去掉子菜单分割线
 supertreeview.setdivider(null);
 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);
 // 临时变量取得treeviewadapter的treenode集合,可为空
 list<treenode> tmp = treeviewadapter.gettreenode();
 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 + 0);
  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 * 2, 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());
 // 设置首层menu标题背景
 textview.setbackgroundresource(r.drawable.menu_category);
 textview.setpadding(treeviewadapter.paddingleft * 2, 0, 0, 0);
 
 return textview;
 }

效果图:

android实现菜单三级树效果

完整代码下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。