当然了,原理说起来简单,还得控制每一层间关系,添加展开缩回等,以及有了缩进还要能显示在正确的位置,不过没关系,我会带着大家一步一步实现的。
=======================================================================
由于整体比较长,我决定首先带大家看一下用法,就是如果学完了这篇博客,我们需要树形控件,我们需要花多少精力去完成~~
现在需求来了:我现在需要展示一个文件管理系统的树形结构:
数据是这样的:
[html] view plain copy
-
//id , pid , label , 其他属性
-
mDatas.add(new FileBean(1, 0, “文件管理系统”));
-
mDatas.add(new FileBean(2, 1, “游戏”));
-
mDatas.add(new FileBean(3, 1, “文档”));
-
mDatas.add(new FileBean(4, 1, “程序”));
-
mDatas.add(new FileBean(5, 2, “war3”));
-
mDatas.add(new FileBean(6, 2, “刀塔传奇”));
-
mDatas.add(new FileBean(7, 4, “面向对象”));
-
mDatas.add(new FileBean(8, 4, “非面向对象”));
-
mDatas.add(new FileBean(9, 7, “C++”));
-
mDatas.add(new FileBean(10, 7, “JAVA”));
-
mDatas.add(new FileBean(11, 7, “Javascript”));
-
mDatas.add(new FileBean(12, 8, “C”));
当然了,bean可以有很多属性,我们提供你动态的设置树节点上的显示、以及不约束id, pid 的命名,你可以起任意丧心病狂的属性名称;
那么我们如何确定呢?
看下Bean:
[java] view plain copy
-
package com.zhy.bean;
-
import com.zhy.tree.bean.TreeNodeId;
-
import com.zhy.tree.bean.TreeNodeLabel;
-
import com.zhy.tree.bean.TreeNodePid;
-
public class FileBean
-
{
-
@TreeNodeId
-
private int _id;
-
@TreeNodePid
-
private int parentId;
-
@TreeNodeLabel
-
private String name;
-
private long length;
-
private String desc;
-
public FileBean(int _id, int parentId, String name)
-
{
-
super();
-
this._id = _id;
-
this.parentId = parentId;
-
this.name = name;
-
}
-
}
现在,不用说,应该也知道我们通过注解来确定的。
下面看我们如何将这数据转化为树
布局文件就一个listview,就补贴了,直接看Activity
[java] view plain copy
-
package com.zhy.tree_view;
-
import java.util.ArrayList;
-
import java.util.List;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.widget.ListView;
-
import com.zhy.bean.FileBean;
-
import com.zhy.tree.bean.TreeListViewAdapter;
-
public class MainActivity extends Activity
-
{
-
private List mDatas = new ArrayList();
-
private ListView mTree;
-
private TreeListViewAdapter mAdapter;
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
initDatas();
-
mTree = (ListView) findViewById(R.id.id_tree);
-
try
-
{
-
mAdapter = new SimpleTreeAdapter(mTree, this, mDatas, 10);
-
mTree.setAdapter(mAdapter);
-
} catch (IllegalAccessException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
private void initDatas()
-
{
-
// id , pid , label , 其他属性
-
mDatas.add(new FileBean(1, 0, “文件管理系统”));
-
mDatas.add(new FileBean(2, 1, “游戏”));
-
mDatas.add(new FileBean(3, 1, “文档”));
-
mDatas.add(new FileBean(4, 1, “程序”));
-
mDatas.add(new FileBean(5, 2, “war3”));
-
mDatas.add(new FileBean(6, 2, “刀塔传奇”));
-
mDatas.add(new FileBean(7, 4, “面向对象”));
-
mDatas.add(new FileBean(8, 4, “非面向对象”));
-
mDatas.add(new FileBean(9, 7, “C++”));
-
mDatas.add(new FileBean(10, 7, “JAVA”));
-
mDatas.add(new FileBean(11, 7, “Javascript”));
-
mDatas.add(new FileBean(12, 8, “C”));
-
}
-
}
Activity里面并没有什么特殊的代码,拿到listview,传入mData,当中初始化了一个Adapter;
看来我们的核心代码都在我们的Adapter里面:
那么看一眼我们的Adapter
[java] view plain copy
-
package com.zhy.tree_view;
-
import java.util.List;
-
import android.content.Context;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.ImageView;
-
import android.widget.ListView;
-
import android.widget.TextView;
-
import com.zhy.tree.bean.Node;
-
import com.zhy.tree.bean.TreeListViewAdapter;
-
public class SimpleTreeAdapter extends TreeListViewAdapter
-
{
-
public SimpleTreeAdapter(ListView mTree, Context context, List datas,
-
int defaultExpandLevel) throws IllegalArgumentException,
-
IllegalAccessException
-
{
-
super(mTree, context, datas, defaultExpandLevel);
-
}
-
@Override
-
public View getConvertView(Node node , int position, View convertView, ViewGroup parent)
-
{
-
ViewHolder viewHolder = null;
-
if (convertView == null)
-
{
-
convertView = mInflater.inflate(R.layout.list_item, parent, false);
-
viewHolder = new ViewHolder();
-
viewHolder.icon = (ImageView) convertView
-
.findViewById(R.id.id_treenode_icon);
-
viewHolder.label = (TextView) convertView
-
.findViewById(R.id.id_treenode_label);
-
convertView.setTag(viewHolder);
-
} else
-
{
-
viewHolder = (ViewHolder) convertView.getTag();
-
}
-
if (node.getIcon() == -1)
-
{
-
viewHolder.icon.setVisibility(View.INVISIBLE);
-
} else
-
{
-
viewHolder.icon.setVisibility(View.VISIBLE);
-
viewHolder.icon.setImageResource(node.getIcon());
-
}
-
viewHolder.label.setText(node.getName());
-
return convertView;
-
}
-
private final class ViewHolder
-
{
-
ImageView icon;
-
TextView label;
-
}
-
}
我们的SimpleTreeAdapter继承了我们的TreeListViewAdapter ; 除此之外,代码上只需要复写getConvertView , 且getConvetView其实和我们平时的getView写法一致;
公布出getConvertView 的目的是,让用户自己去决定Item的展示效果。其他的代码,我已经打包成jar了,用的时候导入即可。这样就完成了我们的树形控件。
也就是说用我们的树形控件,只需要将传统继承BaseAdapter改为我们的TreeListViewAdapter ,然后去实现getConvertView 就好了。
那么现在的效果是:
默认就全打开了,因为我们也支持动态设置打开的层级,方面使用者使用。
用起来是不是很随意,加几个注解,ListView的Adapater换个类继承下~~好了,下面开始带大家一起从无到有的实现~
=======================================================================
1、思路
我们的思路是这样的,我们显示时,需要很多属性,我们需要知道当前节点是否是父节点,当前的层级,他的孩子节点等等;但是用户的数据集是不固定的,最多只能给出类似id,pId 这样的属性。也就是说,用户给的bean并不适合我们用于控制显示,于是我们准备这样做:
1、在用户的Bean中提取出必要的几个元素 id , pId , 以及显示的文本(通过注解+反射);然后组装成我们的真正显示时的Node;即List -> List
2、显示的并非是全部的Node,比如某些节点的父节点是关闭状态,我们需要进行过滤;即List ->过滤后的List
3、显示时,比如点击父节点,它的子节点会跟随其后显示,我们内部是个List,也就是说,这个List的顺序也是很关键的;当然排序我们可以放为步骤一;
最后将过滤后的Node进行显示,设置左内边距即可。
说了这么多,首先看一眼我们封装后的Node
[java] view plain copy [
](https://code.csdn.net/snippet
资料获取→专栏
s/489360/fork “派生到我的代码片”)
-
package com.zhy.tree.bean;
-
import java.util.ArrayList;
-
import java.util.List;
-
import org.w3c.dom.NamedNodeMap;
-
import android.util.Log;
-
public class Node
-
{
-
private int id;
-
/**
-
* 根节点pId为0
-
*/
-
private int pId = 0;
-
private String name;
-
/**
-
* 当前的级别
-
*/
-
private int level;
-
/**
-
* 是否展开
-
*/
-
private boolean isExpand = false;
-
private int icon;
-
/**
-
* 下一级的子Node
-
*/
-
private List children = new ArrayList();
-
/**
-
* 父Node
-
*/
-
private Node parent;
-
public Node()
-
{
-
}
-
public Node(int id, int pId, String name)
-
{
-
super();
-
this.id = id;
-
this.pId = pId;
-
this.name = name;
-
}
-
public int getIcon()
-
{
-
return icon;
-
}
-
public void setIcon(int icon)
-
{
-
this.icon = icon;
-
}
-
public int getId()
-
{
-
return id;
-
}
-
public void setId(int id)
-
{
-
this.id = id;
-
}
-
public int getpId()
-
{
-
return pId;
-
}
-
public void setpId(int pId)
-
{
-
this.pId = pId;
-
}
-
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 List getChildren()
-
{
-
return children;
-
}
-
public void setChildren(List children)
-
{
-
this.children = children;
-
}
-
public Node getParent()
-
{
-
return parent;
-
}
-
public void setParent(Node parent)
-
{
-
this.parent = parent;
-
}
-
/**
-
* 是否为跟节点
-
*
-
* @return
-
*/
-
public boolean isRoot()
-
{
-
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);
-
}
-
}
-
}
-
}
包含了树节点一些常见的属性,一些常见的方法;对于getLevel,setExpand这些方法,大家可以好好看看~
有了Node,刚才的用法中,出现的就是我们Adapter所继承的超类:TreeListViewAdapter;核心代码都在里面,我们准备去一探究竟:
代码不是很长,直接完整的贴出:
[java] view plain copy
-
package com.zhy.tree.bean;
-
import java.util.List;
-
import android.content.Context;
-
import android.view.LayoutInflater;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.AdapterView;
-
import android.widget.AdapterView.OnItemClickListener;
-
import android.widget.BaseAdapter;
-
import android.widget.ListView;
-
public abstract class TreeListViewAdapter extends BaseAdapter
-
{
-
protected Context mContext;
-
/**
-
* 存储所有可见的Node
-
*/
-
protected List mNodes;
-
protected LayoutInflater mInflater;
-
/**
-
* 存储所有的Node
-
*/
-
protected List mAllNodes;
-
/**
-
* 点击的回调接口
-
*/
-
private OnTreeNodeClickListener onTreeNodeClickListener;
-
public interface OnTreeNodeClickListener
-
{
-
void onClick(Node node, int position);
-
}
-
public void setOnTreeNodeClickListener(
-
OnTreeNodeClickListener onTreeNodeClickListener)
-
{
-
this.onTreeNodeClickListener = onTreeNodeClickListener;
-
}
-
/**
-
*
-
* @param mTree
-
* @param context
-
* @param datas
-
* @param defaultExpandLevel
-
* 默认展开几级树
-
* @throws IllegalArgumentException
-
* @throws IllegalAccessException
-
*/
-
public TreeListViewAdapter(ListView mTree, Context context, List datas,
-
int defaultExpandLevel) throws IllegalArgumentException,
-
IllegalAccessException
-
{
-
mContext = context;
-
/**
-
* 对所有的Node进行排序
-
*/
-
mAllNodes = TreeHelper.getSortedNodes(datas, defaultExpandLevel);
-
/**
-
* 过滤出可见的Node
-
*/
-
mNodes = TreeHelper.filterVisibleNode(mAllNodes);
-
mInflater = LayoutInflater.from(context);
-
/**
-
* 设置节点点击时,可以展开以及关闭;并且将ItemClick事件继续往外公布
-
*/
-
mTree.setOnItemClickListener(new OnItemClickListener()
-
{
-
@Override
-
public void onItemClick(AdapterView<?> parent, View view,
-
int position, long id)
-
{
-
expandOrCollapse(position);
-
if (onTreeNodeClickListener != null)
-
{
-
onTreeNodeClickListener.onClick(mNodes.get(position),
-
position);
-
}
-
}
-
});
-
}
-
/**
-
* 相应ListView的点击事件 展开或关闭某节点
-
*
-
* @param position
-
*/
-
public void expandOrCollapse(int position)
-
{
-
Node n = mNodes.get(position);
-
if (n != null)// 排除传入参数错误异常
-
{
-
if (!n.isLeaf())
-
{
-
n.setExpand(!n.isExpand());
-
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() * 30, 3, 3, 3);
-
return convertView;
-
}
-
public abstract View getConvertView(Node node, int position,
-
View convertView, ViewGroup parent);
-
}
首先我们的类继承自BaseAdapter,然后我们对应的数据集是,过滤出的可见的Node;
我们的构造方法默认接收4个参数:listview,context,mdatas,以及默认展开的级数:0只显示根节点;
可以在构造方法中看到:对用户传入的数据集做了排序,和过滤的操作;一会再看这些方法,这些方法我们使用了一个TreeHelper进行了封装。
注:如果你觉得你的Item布局十分复杂,且布局会展示Bean的其他数据,那么为了方便,你可以让Node中包含一个泛型T , 每个Node携带与之对于的Bean的所有数据;
可以看到我们还直接为Item设置了点击事件,因为我们树,默认就有点击父节点展开与关闭;但是为了让用户依然可用点击监听,我们自定义了一个点击的回调供用户使用;
当用户点击时,默认调用expandOrCollapse方法,将当然节点重置展开标志,然后重新过滤出可见的Node,最后notifyDataSetChanged即可;
其他的方法都是BaseAdapter默认的一些方法了。
下面我们看下TreeHelper中的一些方法:
首先看TreeListViewAdapter构造方法中用到的两个方法:
[java] view plain copy
-
/**
-
* 传入我们的普通bean,转化为我们排序后的Node
-
* @param datas
-
* @param defaultExpandLevel
-
* @return
-
* @throws IllegalArgumentException
-
* @throws IllegalAccessException
-
*/
-
public static List getSortedNodes(List datas,
-
int defaultExpandLevel) throws IllegalArgumentException,
-
IllegalAccessException
-
{
-
List result = new ArrayList();
-
//将用户数据转化为List以及设置Node间关系
-
List nodes = convetData2Node(datas);
-
//拿到根节点
-
List rootNodes = getRootNodes(nodes);
-
//排序
-
for (Node node : rootNodes)
-
{
-
addNode(result, node, defaultExpandLevel, 1);
-
}
-
return result;
-
}
拿到用户传入的数据,转化为List以及设置Node间关系,然后根节点,从根往下遍历进行排序;
接下来看:filterVisibleNode
[java] view plain copy
-
/**
-
* 过滤出所有可见的Node
-
*
-
* @param nodes
-
* @return
-
*/
-
public static List filterVisibleNode(List nodes)
-
{
-
List result = new ArrayList();
-
for (Node node : nodes)
-
{
-
// 如果为跟节点,或者上层目录为展开状态
-
if (node.isRoot() || node.isParentExpand())
-
{
-
setNodeIcon(node);
-
result.add(node);
-
}
-
}
-
return result;
-
}
过滤Node的代码很简单,遍历所有的Node,只要是根节点或者父节点是展开状态就添加返回;
最后看看这两个方法用到的别的一些私有方法:
[java] view plain copy
-
/**
-
* 将我们的数据转化为树的节点
-
*
-
* @param datas
-
* @return
-
* @throws NoSuchFieldException
-
* @throws IllegalAccessException
-
* @throws IllegalArgumentException
-
*/
-
private static List convetData2Node(List datas)
-
throws IllegalArgumentException, IllegalAccessException
-
{
-
List nodes = new ArrayList();
-
Node node = null;
-
for (T t : datas)
-
{
-
int id = -1;
-
int pId = -1;
-
String label = null;
-
Class<? extends Object> clazz = t.getClass();
-
Field[] declaredFields = clazz.getDeclaredFields();
-
for (Field f : declaredFields)
-
{
-
if (f.getAnnotation(TreeNodeId.class) != null)
- }
过滤Node的代码很简单,遍历所有的Node,只要是根节点或者父节点是展开状态就添加返回;
最后看看这两个方法用到的别的一些私有方法:
[java] view plain copy [外链图片转存中…(img-aIv1Lbro-1718726703521)] [外链图片转存中…(img-U8pm6OhO-1718726703521)]
-
/**
-
* 将我们的数据转化为树的节点
-
*
-
* @param datas
-
* @return
-
* @throws NoSuchFieldException
-
* @throws IllegalAccessException
-
* @throws IllegalArgumentException
-
*/
-
private static List convetData2Node(List datas)
-
throws IllegalArgumentException, IllegalAccessException
-
{
-
List nodes = new ArrayList();
-
Node node = null;
-
for (T t : datas)
-
{
-
int id = -1;
-
int pId = -1;
-
String label = null;
-
Class<? extends Object> clazz = t.getClass();
-
Field[] declaredFields = clazz.getDeclaredFields();
-
for (Field f : declaredFields)
-
{
-
if (f.getAnnotation(TreeNodeId.class) != null)