成都创新互联网站制作重庆分公司

Android中怎么实现多级树形菜单

这期内容当中小编将会给大家带来有关Android中怎么实现多级树形菜单,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事网站建设、成都网站制作企业网站建设、手机网站制作、网页设计、品牌网站设计、网页制作、做网站、建网站。创新互联建站拥有实力坚强的技术研发团队及素养的视觉设计专才。

这样的树形结构,重点是我们应该怎样去定义数据结构,这个是Node实体类:

public class Node implements Serializable { private Node parent = null; // 父节点 private List childrens = new ArrayList();//子节点 private String title;//节点显示文字 private String value;//节点显示值 private boolean isChecked = false; //是否被选中 private boolean isExpand = true;//是否处于扩展状态 private boolean hasCheckBox = true;//是否有复选框 private String parentId = null; private String curId = null; private boolean isVisiable = true; //自己加的,父节点集合 private List parents = new ArrayList<>(); /** * 设置节点值 * * @param parentId *  TODO * @param curId *  TODO */ public Node(String title,String value, String parentId,String curId) { // TODO Auto-generated constructor stub this.title = title; this.value = value; this.parentId = parentId; //this.icon = iconId; this.curId = curId; } public List getParents() { return parents; } /** * 得到父节点 * @return * */ public Node getParent() { return parent; } /** * 设置父节点 * @param parent * */ public void setParent(Node parent) { this.parent = parent; } /** * 得到子节点 * @return * */ public List getChildrens() { return childrens; } /** * 是否根节点 * @return * */ public boolean isRoot(){ return parent ==null?true:false; } /** * 是否被选中 * @return * */ public boolean isChecked() { return isChecked; } public void setChecked(boolean isChecked) { this.isChecked = isChecked; } /** * 是否是展开状态 * @return * */ public boolean isExplaned() { return isExpand; } /** * 设置展开状态 * @param isExplaned * */ public void setExplaned(boolean isExplaned) { this.isExpand = isExplaned; } /** * 是否有复选框 * @return * */ public boolean hasCheckBox() { return hasCheckBox; } /** * 设置是否有复选框 * @param hasCheckBox * */ public void setHasCheckBox(boolean hasCheckBox) { this.hasCheckBox = hasCheckBox; } /** * 得到节点标题 * @return * */ public String getTitle() { return title; } /** * 设置节点标题 * @param title * */ public void setTitle(String title) { this.title = title; } /** * 得到节点值 * @return * */ public String getValue() { return value; } /** * 设置节点值 * @param value * */ public void setValue(String value) { this.value = value; } /** * 增加一个子节点 * @param node * */ public void addNode(Node node){ if(!childrens.contains(node)){  childrens.add(node); } } /** * 移除一个子节点 * @param node * */ public void removeNode(Node node){ if(childrens.contains(node))  childrens.remove(node); } /** * 移除指定位置的子节点 * @param location * */ public void removeNode(int location){ childrens.remove(location); } /** * 清除所有子节点 * */ public void clears(){ childrens.clear(); } /** * 判断给出的节点是否当前节点的父节点 * @param node * @return * */ public boolean isParent(Node node){ if(parent == null)return false; if(parent.equals(node))return true; return parent.isParent(node); } /** * 递归获取当前节点级别 * @return * */ public int getLevel(){ return parent ==null?0:parent.getLevel()+1; } /** * 父节点是否处于折叠的状态 * @return * */ public boolean isParentCollapsed(){ if(parent ==null)return false; if(!parent.isExplaned())return true; return parent.isParentCollapsed(); } /** * 是否叶节点(没有展开下级的几点) * @return * */ public boolean isLeaf(){ return childrens.size()<1?true:false; } /** * 返回自己的id * @return **/ public String getCurId() { // TODO Auto-generated method stub return curId; } /** * 返回的父id * @return **/ public String getParentId() { // TODO Auto-generated method stub return parentId; }}

这里定义了父节点和子节点,为什么这么定义呢,因为方便对节点的操作,这样子我们可以通过父节点查找子节点,也可以通过子节点去查找父节点。

List list = new ArrayList(); NodeResource n1 = new NodeResource(""+-1, ""+0, "全部城市", "dfs");//, R.drawable.icon_department list.add(n1); NodeResource n3 = new NodeResource(""+0, ""+1, "北京", "dfs"); list.add(n3); NodeResource n4 = new NodeResource(""+1, ""+2, "金刚狼军团", "dfs"); list.add(n4); NodeResource n5 = new NodeResource(""+1, ""+3, "蚂蚁军团", "dfs"); list.add(n5); NodeResource n6 = new NodeResource(""+1, ""+4, "大象军团", "dfs"); list.add(n6); NodeResource n7 = new NodeResource(""+2,""+5, "张三", "dfs"); list.add(n7); NodeResource n8 = new NodeResource(""+2,""+6, "李四", "dfs"); list.add(n8); NodeResource n9 = new NodeResource(""+0,""+7, "天津", "dfs"); list.add(n9); NodeResource n10 = new NodeResource(""+7,""+8, "老鼠军团", "dfs"); list.add(n10); NodeResource n11 = new NodeResource(""+8,""+9, "王五", "dfs"); list.add(n11); NodeResource n12 = new NodeResource(""+8,""+10, "赵六", "dfs"); list.add(n12); return list;

这里是我们的数据源,要说明一点是,无论我们从接口拿到的是什么数据,统一要给它们添加父ID,这样会大大方便了我们的操作。

package cn.thinkmore.test;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.util.Log;import android.view.View.OnClickListener;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.TextView;public class TreeAdapter extends BaseAdapter { private Context con; private LayoutInflater lif; public List all = new ArrayList();//展示 private List cache = new ArrayList();//缓存 private TreeAdapter tree = this; boolean hasCheckBox; private int expandIcon = -1;//展开图标 private int collapseIcon = -1;//收缩图标 ViewItem vi = null;// //存储checkbox选中的集合// private List<> /** * 构造方法 */ public TreeAdapter(Context context,ListrootNodes){ this.con = context; this.lif = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); for(int i=0;igetSelectedNode(){ Log.d("getSelectedNode", "我被执行了!"); Listchecks =new ArrayList() ; for(int i = 0;i

上述就是小编为大家分享的Android中怎么实现多级树形菜单了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


文章标题:Android中怎么实现多级树形菜单
文章来源:http://cxhlcq.com/article/pdgggo.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部