Android实现多级树形选择列表
程序员文章站
2023-11-14 13:46:22
项目中有多个地方要用到多级列表的菜单,最开始我用的是expandablelistview,但问题是expandablelistview只支持两级列表,于是我就用expand...
项目中有多个地方要用到多级列表的菜单,最开始我用的是expandablelistview,但问题是expandablelistview只支持两级列表,于是我就用expandablelistview嵌套expandablelistview,但非常麻烦,而且关键的是具体分几级是不确定的,也就是可能一级,可能多级,这要是五六级嵌套listview,于是我就去学习鸿洋大神之前写的一篇关于实现android多级树形列表的文章,实现很巧妙,使用一个listview就可以实现多级列表效果,我做了部分修改,功能顺利实现。
1.定义节点实体类:
package com.xiaoyehai.multileveltreelist.treelist; import java.util.arraylist; import java.util.list; /** * 节点实体类 * created by xiaoyehai on 2018/7/11 0011. */ public class node<t> { /** * 当前节点id */ private string id; /** * 父节点id */ private string pid; /** * 节点数据实体类 */ private t data; /** * 设置开启 关闭的图片 */ public int iconexpand = -1, iconnoexpand = -1; /** * 节点名称 */ private string name; /** * 当前的级别 */ private int level; /** * 是否展开 */ private boolean isexpand = false; private int icon = -1; /** * 下一级的子node */ private list<node> children = new arraylist<>(); /** * 父node */ private node parent; /** * 是否被checked选中 */ private boolean ischecked; public node() { } public node(string id, string pid, string name) { this.id = id; this.pid = pid; this.name = name; } public node(string id, string pid, t data, string name) { this.id = id; this.pid = pid; this.data = data; this.name = name; } /** * 是否为根节点 * * @return */ public boolean isrootnode() { return parent == null; } /** * 判断父节点是否展开 * * @return */ public boolean isparentexpand() { if (parent == null) return false; return parent.isexpand(); } /** * 是否是叶子节点 * * @return */ public boolean isleaf() { return children.size() == 0; } /** * 获取当前的级别level */ public int getlevel() { return parent == null ? 0 : parent.getlevel() + 1; } /** * 设置展开 * * @param isexpand */ public void setexpand(boolean isexpand) { this.isexpand = isexpand; if (!isexpand) { for (node node : children) { node.setexpand(isexpand); } } } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getpid() { return pid; } public void setpid(string pid) { this.pid = pid; } public t getdata() { return data; } public void setdata(t data) { this.data = data; } public int geticonexpand() { return iconexpand; } public void seticonexpand(int iconexpand) { this.iconexpand = iconexpand; } public int geticonnoexpand() { return iconnoexpand; } public void seticonnoexpand(int iconnoexpand) { this.iconnoexpand = iconnoexpand; } public string getname() { return name; } public void setname(string name) { this.name = name; } public void setlevel(int level) { this.level = level; } public boolean isexpand() { return isexpand; } public int geticon() { return icon; } public void seticon(int icon) { this.icon = icon; } public list<node> getchildren() { return children; } public void setchildren(list<node> children) { this.children = children; } public node getparent() { return parent; } public void setparent(node parent) { this.parent = parent; } public boolean ischecked() { return ischecked; } public void setchecked(boolean checked) { ischecked = checked; } }
2.定义每个节点数据的实体类
因为项目中多个地方用到树形菜单,而且数据都不一样,每个节点数据都比较复杂,所以我单独封装出一个类,要是数据和简单,这步可以不用,直接用node类。
例如:
/** * 每个节点的具体数据 * created by xiaoyehai on 2018/7/11 0011. */ public class nodedata { private string name; public nodedata() { } public nodedata(string name) { this.name = name; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
3.treehelper
package com.xiaoyehai.multileveltreelist.treelist; import java.util.arraylist; import java.util.list; /** * created by xiaoyehai on 2018/7/11 0011. */ public class treehelper { /** * 传入node 返回排序后的node * 拿到用户传入的数据,转化为list<node>以及设置node间关系,然后根节点,从根往下遍历进行排序; * * @param datas * @param defaultexpandlevel 默认显示 * @return * @throws illegalargumentexception * @throws illegalaccessexception */ public static list<node> getsortednodes(list<node> datas, int defaultexpandlevel) { list<node> result = new arraylist<node>(); // 设置node间父子关系 list<node> nodes = convetdata2node(datas); // 拿到根节点 list<node> rootnodes = getrootnodes(nodes); // 排序以及设置node间关系 for (node node : rootnodes) { addnode(result, node, defaultexpandlevel, 1); } return result; } /** * 过滤出所有可见的node * 过滤node的代码很简单,遍历所有的node,只要是根节点或者父节点是展开状态就添加返回 * * @param nodes * @return */ public static list<node> filtervisiblenode(list<node> nodes) { list<node> result = new arraylist<node>(); for (node node : nodes) { // 如果为跟节点,或者上层目录为展开状态 if (node.isrootnode() || node.isparentexpand()) { setnodeicon(node); result.add(node); } } return result; } /** * 将我们的数据转化为树的节点 * 设置node间,父子关系;让每两个节点都比较一次,即可设置其中的关系 */ private static list<node> convetdata2node(list<node> nodes) { for (int i = 0; i < nodes.size(); i++) { node n = nodes.get(i); for (int j = i + 1; j < nodes.size(); j++) { node m = nodes.get(j); if (m.getpid() instanceof string) { if (m.getpid().equals(n.getid())) { //n时m的父节点 n.getchildren().add(m); m.setparent(n); } else if (m.getid().equals(n.getpid())) { //m时n的父节点 m.getchildren().add(n); n.setparent(m); } } else { if (m.getpid() == n.getid()) { n.getchildren().add(m); m.setparent(n); } else if (m.getid() == n.getpid()) { m.getchildren().add(n); n.setparent(m); } } } } return nodes; } /** * 获得根节点 * * @param nodes * @return */ private static list<node> getrootnodes(list<node> nodes) { list<node> root = new arraylist<node>(); for (node node : nodes) { if (node.isrootnode()) root.add(node); } return root; } /** * 把一个节点上的所有的内容都挂上去 * 通过递归的方式,把一个节点上的所有的子节点等都按顺序放入 */ private static <t> void addnode(list<node> nodes, node<t> node, int defaultexpandleval, int currentlevel) { nodes.add(node); if (defaultexpandleval >= currentlevel) { node.setexpand(true); } if (node.isleaf()) return; for (int i = 0; i < node.getchildren().size(); i++) { addnode(nodes, node.getchildren().get(i), defaultexpandleval, currentlevel + 1); } } /** * 设置节点的图标 * * @param node */ private static void setnodeicon(node node) { if (node.getchildren().size() > 0 && node.isexpand()) { node.seticon(node.iconexpand); } else if (node.getchildren().size() > 0 && !node.isexpand()) { node.seticon(node.iconnoexpand); } else { node.seticon(-1); } } }
4.treelistviewadapter
对于listview的适配器,需要继承自treelistviewadapter,如
package com.xiaoyehai.multileveltreelist.treelist; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.listview; import java.util.arraylist; import java.util.list; /** * created by xiaoyehai on 2018/7/11 0011. */ public abstract class treelistviewadapter extends baseadapter { protected context mcontext; /** * 默认不展开 */ private int defaultexpandlevel = 0; /** * 展开与关闭的图片 */ private int iconexpand = -1, iconnoexpand = -1; /** * 存储所有的node */ protected list<node> mallnodes = new arraylist<>(); protected layoutinflater minflater; /** * 存储所有可见的node */ protected list<node> mnodes = new arraylist<>(); /** * 点击的回调接口 */ private ontreenodeclicklistener ontreenodeclicklistener; public void setontreenodeclicklistener(ontreenodeclicklistener ontreenodeclicklistener) { this.ontreenodeclicklistener = ontreenodeclicklistener; } public treelistviewadapter(listview listview, context context, list<node> datas, int defaultexpandlevel, int iconexpand, int iconnoexpand) { this.mcontext = context; this.defaultexpandlevel = defaultexpandlevel; this.iconexpand = iconexpand; this.iconnoexpand = iconnoexpand; for (node node : datas) { node.getchildren().clear(); node.seticonexpand(iconexpand); node.seticonnoexpand(iconnoexpand); } /** * 对所有的node进行排序 */ mallnodes = treehelper.getsortednodes(datas, defaultexpandlevel); /** * 过滤出可见的node */ mnodes = treehelper.filtervisiblenode(mallnodes); minflater = layoutinflater.from(context); /** * 设置节点点击时,可以展开以及关闭;并且将itemclick事件继续往外公布 */ listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { expandorcollapse(position); if (ontreenodeclicklistener != null) { ontreenodeclicklistener.onclick(mnodes.get(position), position); } } }); } /** * @param listview * @param context * @param datas * @param defaultexpandlevel 默认展开几级树 */ public treelistviewadapter(listview listview, context context, list<node> datas, int defaultexpandlevel) { this(listview, context, datas, defaultexpandlevel, -1, -1); } /** * 相应listview的点击事件 展开或关闭某节点 * * @param position */ public void expandorcollapse(int position) { node n = mnodes.get(position); if (n != null) {// 排除传入参数错误异常 if (!n.isleaf()) { n.setexpand(!n.isexpand()); //获取所有可见的node mnodes = treehelper.filtervisiblenode(mallnodes); notifydatasetchanged();// 刷新视图 } } } @override public int getcount() { return mnodes.size(); } @override public object getitem(int position) { return mnodes.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { node node = mnodes.get(position); convertview = getconvertview(node, position, convertview, parent); // 设置内边距 convertview.setpadding(node.getlevel() * 50, 12, 12, 12); return convertview; } /** * 获取排序后所有节点 * * @return */ public list<node> getallnodes() { if (mallnodes == null) mallnodes = new arraylist<node>(); return mallnodes; } /** * 获取所有选中节点 * * @return */ public list<node> getselectednode() { list<node> checks = new arraylist<node>(); for (int i = 0; i < mallnodes.size(); i++) { node n = mallnodes.get(i); if (n.ischecked()) { checks.add(n); } } return checks; } /** * 设置多选 * * @param node * @param checked */ protected void setchecked(final node node, boolean checked) { node.setchecked(checked); setchildchecked(node, checked); if (node.getparent() != null) setnodeparentchecked(node.getparent(), checked); notifydatasetchanged(); } /** * 设置是否选中 * * @param node * @param checked */ public <t> void setchildchecked(node<t> node, boolean checked) { if (!node.isleaf()) { node.setchecked(checked); for (node childrennode : node.getchildren()) { setchildchecked(childrennode, checked); } } else { node.setchecked(checked); } } private void setnodeparentchecked(node node, boolean checked) { if (checked) { node.setchecked(checked); if (node.getparent() != null) setnodeparentchecked(node.getparent(), checked); } else { list<node> childrens = node.getchildren(); boolean ischecked = false; for (node children : childrens) { if (children.ischecked()) { ischecked = true; } } //如果所有自节点都没有被选中 父节点也不选中 if (!ischecked) { node.setchecked(checked); } if (node.getparent() != null) setnodeparentchecked(node.getparent(), checked); } } /** * 清除掉之前数据并刷新 重新添加 * * @param mlists * @param defaultexpandlevel 默认展开几级列表 */ public void adddataall(list<node> mlists, int defaultexpandlevel) { mallnodes.clear(); adddata(-1, mlists, defaultexpandlevel); } /** * 在指定位置添加数据并刷新 可指定刷新后显示层级 * * @param index * @param mlists * @param defaultexpandlevel 默认展开几级列表 */ public void adddata(int index, list<node> mlists, int defaultexpandlevel) { this.defaultexpandlevel = defaultexpandlevel; notifydata(index, mlists); } /** * 在指定位置添加数据并刷新 * * @param index * @param mlists */ public void adddata(int index, list<node> mlists) { notifydata(index, mlists); } /** * 添加数据并刷新 * * @param mlists */ public void adddata(list<node> mlists) { adddata(mlists, defaultexpandlevel); } /** * 添加数据并刷新 可指定刷新后显示层级 * * @param mlists * @param defaultexpandlevel */ public void adddata(list<node> mlists, int defaultexpandlevel) { this.defaultexpandlevel = defaultexpandlevel; notifydata(-1, mlists); } /** * 添加数据并刷新 * * @param node */ public void adddata(node node) { adddata(node, defaultexpandlevel); } /** * 添加数据并刷新 可指定刷新后显示层级 * * @param node * @param defaultexpandlevel */ public void adddata(node node, int defaultexpandlevel) { list<node> nodes = new arraylist<>(); nodes.add(node); this.defaultexpandlevel = defaultexpandlevel; notifydata(-1, nodes); } /** * 刷新数据 * * @param index * @param mlistnodes */ public void notifydata(int index, list<node> mlistnodes) { for (int i = 0; i < mlistnodes.size(); i++) { node node = mlistnodes.get(i); node.getchildren().clear(); node.iconexpand = iconexpand; node.iconnoexpand = iconnoexpand; } for (int i = 0; i < mallnodes.size(); i++) { node node = mallnodes.get(i); node.getchildren().clear(); //node.isnewadd = false; } if (index != -1) { mallnodes.addall(index, mlistnodes); } else { mallnodes.addall(mlistnodes); } /** * 对所有的node进行排序 */ mallnodes = treehelper.getsortednodes(mallnodes, defaultexpandlevel); /** * 过滤出可见的node */ mnodes = treehelper.filtervisiblenode(mallnodes); //刷新数据 notifydatasetchanged(); } public abstract view getconvertview(node node, int position, view convertview, viewgroup parent); }
5.接口回调:
选中状态改变的回调:
package com.xiaoyehai.multileveltreelist.treelist; /** * created by xiaoyehai on 2018/7/12 0012. */ public interface ontreenodecheckedchangelistener { void oncheckchange(node node, int position, boolean ischecked); }
条目点击的回调:
package com.xiaoyehai.multileveltreelist.treelist; /** * created by xiaoyehai on 2018-07-12. */ public interface ontreenodeclicklistener { void onclick(node node, int position); }
6.使用:
布局文件:
<listview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></listview>
activity:
public class listviewactivity extends appcompatactivity { private listview mlistview; private list<node> datalist = new arraylist<>(); private listviewadapter madapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_view); mlistview = (listview) findviewbyid(r.id.listview); initdata(); //第一个参数 listview //第二个参数 上下文 //第三个参数 数据集 //第四个参数 默认展开层级数 0为不展开 //第五个参数 展开的图标 //第六个参数 闭合的图标 madapter = new listviewadapter(mlistview, this, datalist, 0, r.drawable.zoomout_yzs, r.drawable.zoomin_yzs); mlistview.setadapter(madapter); //获取所有节点 final list<node> allnodes = madapter.getallnodes(); for (node allnode : allnodes) { //log.e("xyh", "oncreate: " + allnode.getname()); } //选中状态监听 madapter.setcheckedchangelistener(new ontreenodecheckedchangelistener() { @override public void oncheckchange(node node, int position, boolean ischecked) { //获取所有选中节点 list<node> selectednode = madapter.getselectednode(); for (node n : selectednode) { log.e("xyh", "oncheckchange: " + n.getname()); } } }); } /** * 模拟数据,实际开发中对返回的json数据进行封装 */ private void initdata() { //根节点 node<nodedata> node = new node<>("0", "-1", "根节点1"); datalist.add(node); datalist.add(new node<>("1", "-1", "根节点2")); datalist.add(new node<>("2", "-1", "根节点3")); //根节点1的二级节点 datalist.add(new node<>("3", "0", "二级节点")); datalist.add(new node<>("4", "0", "二级节点")); datalist.add(new node<>("5", "0", "二级节点")); //根节点2的二级节点 datalist.add(new node<>("6", "1", "二级节点")); datalist.add(new node<>("7", "1", "二级节点")); datalist.add(new node<>("8", "1", "二级节点")); //根节点3的二级节点 datalist.add(new node<>("9", "2", "二级节点")); datalist.add(new node<>("10", "2", "二级节点")); datalist.add(new node<>("11", "2", "二级节点")); //三级节点 datalist.add(new node<>("12", "3", "三级节点")); datalist.add(new node<>("13", "3", "三级节点")); datalist.add(new node<>("14", "3", "三级节点")); datalist.add(new node<>("15", "4", "三级节点")); datalist.add(new node<>("16", "4", "三级节点")); datalist.add(new node<>("17", "4", "三级节点")); datalist.add(new node<>("18", "5", "三级节点")); datalist.add(new node<>("19", "5", "三级节点")); datalist.add(new node<>("20", "5", "三级节点")); //四级节点 datalist.add(new node<>("21", "12", "四级节点")); //... //可以有无线多层级 } }
adapter:
package com.xiaoyehai.multileveltreelist.adapter; import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.checkbox; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import com.xiaoyehai.multileveltreelist.r; import com.xiaoyehai.multileveltreelist.treelist.ontreenodecheckedchangelistener; import com.xiaoyehai.multileveltreelist.treelist.treelistviewadapter; import com.xiaoyehai.multileveltreelist.treelist.node; import java.util.list; /** * created by xiaoyehai on 2018/7/12 0012. */ public class listviewadapter extends treelistviewadapter { private ontreenodecheckedchangelistener checkedchangelistener; public void setcheckedchangelistener(ontreenodecheckedchangelistener checkedchangelistener) { this.checkedchangelistener = checkedchangelistener; } public listviewadapter(listview listview, context context, list<node> datas, int defaultexpandlevel, int iconexpand, int iconnoexpand) { super(listview, context, datas, defaultexpandlevel, iconexpand, iconnoexpand); } @override public view getconvertview(final node node, final int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { convertview = view.inflate(mcontext, r.layout.item, null); holder = new viewholder(convertview); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.tvname.settext(node.getname()); if (node.geticon() == -1) { holder.ivexpand.setvisibility(view.invisible); } else { holder.ivexpand.setvisibility(view.visible); holder.ivexpand.setimageresource(node.geticon()); } holder.checkbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { setchecked(node, holder.checkbox.ischecked()); if (checkedchangelistener != null) { checkedchangelistener.oncheckchange(node, position,holder.checkbox.ischecked()); } } }); if (node.ischecked()) { holder.checkbox.setchecked(true); } else { holder.checkbox.setchecked(false); } return convertview; } static class viewholder { private checkbox checkbox; private textview tvname; private imageview ivexpand; public viewholder(view convertview) { checkbox = convertview.findviewbyid(r.id.cb); tvname = convertview.findviewbyid(r.id.tv_name); ivexpand = convertview.findviewbyid(r.id.iv_expand); } } }
也可以用recycleview实现,在我的项目里面都有。
[项目地址]:multileveltreelist
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 微信小程序实现单列下拉菜单效果
推荐阅读
-
Android 列表选择框 Spinner详解及实例
-
Android 实现IOS 滚轮选择控件的实例(源码下载)
-
Android ExpandableListView双层嵌套实现三级树形菜单
-
Android实现选择相册图片并显示功能
-
Android实现多级树形选择列表
-
Android编程实现多列显示的下拉列表框Spinner功能示例
-
Android中使用Spinner实现下拉列表功能
-
android开发实现列表控件滚动位置精确保存和恢复的方法(推荐)
-
Android控件BottomSheet实现底边弹出选择列表
-
Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)