Android中怎么实现多级树形菜单
创始人
2024-12-21 21:42:25
0次

这样的树形结构,重点是我们应该怎样去定义数据结构,这个是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

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...