当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现多级树形菜单并支持多选功能

Android实现多级树形菜单并支持多选功能

2019年09月06日  | 移动技术网移动技术  | 我要评论

公司有一个需求,实现一个多级的树形菜单,并且支持多选功能,实现这个功能之前,我在网上找了找,树形菜单很好找,但是支持多选功能并没有很合适的,所以没办法,只能自己动手写了,由于本人第一次写博客,如果有什么不足的地方,大家多多指教。

这个是效果图:

这个菜单是可以无限极分类的,如果父元素的子元素,都被选了,父元素的checkbox应该自动选中,或者说选中一个父元素,当前父元素下的子元素应该全部被选中。就是这样的一个效果!

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

public class node implements serializable {
 private node parent = null; // 父节点
 private list<node> childrens = new arraylist<node>();//子节点
 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<node> 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<node> getparents() {
 return parents;
 }
 /**
 * 得到父节点
 * @return
 *
 */
 public node getparent() {
 return parent;
 }
 /**
 * 设置父节点
 * @param parent
 *
 */
 public void setparent(node parent) {
 this.parent = parent;
 }
 /**
 * 得到子节点
 * @return
 *
 */
 public list<node> 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 <noderesource> list = new arraylist<noderesource>();
 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<node> all = new arraylist<node>();//展示
 private list<node> cache = new arraylist<node>();//缓存
 private treeadapter tree = this;
 boolean hascheckbox;
 private int expandicon = -1;//展开图标
 private int collapseicon = -1;//收缩图标

 viewitem vi = null;

// //存储checkbox选中的集合
// private list<>

 /**
 * 构造方法
 */
 public treeadapter(context context,list<node>rootnodes){
 this.con = context;
 this.lif = (layoutinflater)con.getsystemservice(context.layout_inflater_service);
 for(int i=0;i<rootnodes.size();i++){
  addnode(rootnodes.get(i));
 }
 }
 /**
 * 把一个节点上的所有的内容都挂上去
 * @param node
 *
 */
 public void addnode(node node){
 all.add(node);
 cache.add(node);
 if(node.isleaf())return;
 for(int i = 0;i<node.getchildrens().size();i++){
  addnode(node.getchildrens().get(i));
 }
 }
 /**
 * 设置展开收缩图标
 * @param expandicon
 * @param collapseicon
 *
 */
 public void setcollapseandexpandicon(int expandicon,int collapseicon){
 this.collapseicon = collapseicon;
 this.expandicon = expandicon;
 }
 /**
 * 一次性对某节点的所有节点进行选中or取消操作
 *
 *
 */
 public void checknode(node n,boolean ischecked){
 n.setchecked(ischecked);
 for(int i =0 ;i<n.getchildrens().size();i++){
  checknode((node) n.getchildrens().get(i), ischecked);
 }
 //徐强加的代码
 unchecknode(n, ischecked);
 }

 public void quanxuanclick(node n){
 n.setchecked(true);
 for(int i =0 ;i<n.getchildrens().size();i++){
  quanxuanclick(n.getchildrens().get(i));
 }
 }

 public void quxiaoclick(node n){
 n.setchecked(false);
 for(int i =0 ;i<n.getchildrens().size();i++){
  quxiaoclick(n.getchildrens().get(i));
 }
 }

 //徐强加的代码
 public void unchecknode(node n, boolean ischecked){

 boolean flag = false;

 n.setchecked(ischecked);

 if(n.getparent() != null ){
  //if(n.getparent().getchildrens().size() != 0) {
  log.d("parentsize", n.getparent().getchildrens().get(0).ischecked() + "");
  for (int i = 0; i < n.getparent().getchildrens().size(); i++) {
  if((n.getparent().getchildrens().get(i)) != n && (n.getparent().getchildrens().get(i).ischecked() != true)){
   flag = true;
   break;
  }
  }
  if(!flag) {
  unchecknode(n.getparent(), ischecked);
  }
  //}
 }

 }

 /**
 * 获取所有选中节点
 * @return
 *
 */
 public list<node>getselectednode(){
 log.d("getselectednode", "我被执行了!");
 list<node>checks =new arraylist<node>() ;
 for(int i = 0;i<cache.size();i++){
  node n =(node)cache.get(i);
  if(n.ischecked())
  checks.add(n);
 }
 return checks;
 }
 /**
 * 设置是否有复选框
 * @param hascheckbox
 *
 */
 public void setcheckbox(boolean hascheckbox){
 this.hascheckbox = hascheckbox;
 }
 /**
 * 控制展开缩放某节点
 * @param location
 *
 */
 public void expandorcollapse(int location){
 node n = all.get(location);//获得当前视图需要处理的节点 
 if(n!=null)//排除传入参数错误异常
 {
  if(!n.isleaf()){
  n.setexplaned(!n.isexplaned());// 由于该方法是用来控制展开和收缩的,所以取反即可
  filternode();//遍历一下,将所有上级节点展开的节点重新挂上去
  this.notifydatasetchanged();//刷新视图
  }
 }

 }
 /**
 * 设置展开等级
 * @param level
 *
 */
 public void setexpandlevel(int level){
 all.clear();
 for(int i = 0;i<cache.size();i++){
  node n = cache.get(i);
  if(n.getlevel()<=level){
  if(n.getlevel()<level)
   n.setexplaned(true);
  else
   n.setexplaned(false);
  all.add(n);
  }
 }

 }
 /* 清理all,从缓存中将所有父节点不为收缩状态的都挂上去*/
 public void filternode(){
 all.clear();
 for(int i = 0;i<cache.size();i++){
  node n = cache.get(i);
  if(!n.isparentcollapsed()||n.isroot())//凡是父节点不收缩或者不是根节点的都挂上去
  all.add(n);
 }
 }
 /* (non-javadoc)
 * @see android.widget.adapter#getcount()
 */
 @override
 public int getcount() {
 // todo auto-generated method stub
 return all.size();
 }

 /* (non-javadoc)
 * @see android.widget.adapter#getitem(int)
 */
 @override
 public object getitem(int location) {
 // todo auto-generated method stub
 return all.get(location);
 }

 /* (non-javadoc)
 * @see android.widget.adapter#getitemid(int)
 */
 @override
 public long getitemid(int location) {
 // todo auto-generated method stub
 return location;
 }

 /* (non-javadoc)
 * @see android.widget.adapter#getview(int, android.view.view, android.view.viewgroup)
 */
 @override
 public view getview(int location, view view, viewgroup viewgroup) {

 final node n = all.get(location);

 //viewitem vi = null;
 if(view == null){
  view = lif.inflate(r.layout.list_item, null);
  vi = new viewitem();
  vi.cb = (checkbox)view.findviewbyid(r.id.cb);
  vi.flagicon = (imageview)view.findviewbyid(r.id.ivec);
  vi.tv = (textview)view.findviewbyid(r.id.itemvalue);
  vi.cb.setonclicklistener(new onclicklistener() {

  private node mcheckboxn;

  @override
  public void onclick(view v) {

   mcheckboxn = (node) v.gettag();
   checknode(mcheckboxn, ((checkbox) v).ischecked());
   //unchecknode(n, ((checkbox) v).ischecked());
   tree.notifydatasetchanged();
  }
  });
  view.settag(vi);
 }
 else{
  vi = (viewitem)view.gettag();
  if(vi ==null)
  system.out.println();
 }

 if(n!=null){
  if(vi==null||vi.cb==null)
  system.out.println();
  vi.cb.settag(n);
  vi.cb.setchecked(n.ischecked());
  //叶节点不显示展开收缩图标
  if(n.isleaf()){
  vi.flagicon.setvisibility(view.gone);
  }
  else{
  vi.flagicon.setvisibility(view.visible);
  if(n.isexplaned()){
   if(expandicon!=-1){
   vi.flagicon.setimageresource(expandicon);
   }
  }
  else{
   if(collapseicon!=-1){
   vi.flagicon.setimageresource(collapseicon);
   }
  }
  }
  //设置是否显示复选框
  if(n.hascheckbox()&&n.hascheckbox()){
  vi.cb.setvisibility(view.visible);
  }
  else{
  vi.cb.setvisibility(view.gone);
  }
  //显示文本
  vi.tv.settext(n.gettitle());
  // 控制缩进
  view.setpadding(30*n.getlevel(), 3,3, 3);
 }
 return view;
 }


 public class viewitem{
 private checkbox cb;
 //private imageview icon;
 private imageview flagicon;
 private textview tv;
 }
}

我们对多选的操作,主要是在adapter里进行操作的,我也不多说什么了,看代码就能一目了然了。

对了,我记得当时树形菜单是一个人分享的,具体是哪个人我忘记了,我在他的基础上又做了修改,非常感谢那个人的分享。

多说无益,看看源代码比什么都强,一会我会附上源代码。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网