之前做项目的时候做人员组织架构时候需要用到,同样可以用于目录视图。简单搜了一下没有合适的,只找到一个基础的有瑕疵的树形结构,就在基础上改了增加了复选框以及简化了部分代码,。下面上演示效果图,时长25秒,手机卡见谅。
复选框有两种设计模式:
1、子节点选中则父节点选中,适合多级多item下方便了解哪些被选中;
2、子节点全部选中父节点才选中,更符合日常逻辑,适合少数量以及少层级。
下面上主要代码:
首先上MainActivity,主要作用上加载layout以及读取数据。实际中一般从数据库获取。命名较为随意请见谅。
public class MainActivity extends AppCompatActivity {
List list = new ArrayList();
private TreeListView listView;
private RelativeLayout relativeLayout, rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.main_relative_layout);
Context context=MainActivity.this;
rl = new RelativeLayout(context);
rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
listView = new TreeListView(context, initNodeTree());
listView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
relativeLayout.addView(listView);
}
public List initNodeTree() {
List member_list =new ArrayList();
// -1表示为根节点,id的作用为标识对象身份,第三个参数此例子中是text文本
member_list.add(new Node("" + -1, "1" , "111"));
member_list.add(new Node(""+1 , "2" , "222"));
member_list.add(new Node("" + -1, "3" , "333"));
member_list.add(new Node("" + 1, "4" , "444"));
member_list.add(new Node("" + 4, "5" , "555"));
member_list.add(new Node("" + 4, "6" , "666"));
member_list.add(new Node("" + 4, "7" , "777"));
member_list.add(new Node("" + 7, "8" , "888"));
member_list.add(new Node("" + 8, "9" , "999"));
member_list.add(new Node("" + 8, "10" , "101010"));
list.addAll(member_list);
return list;
}
}
接下来是Node类:
Node对象当前主要有父节点Id,自身id以及值组成,自身id自加,父节点id,使用过程中根据实际使用增加成员属性。比如作为组织架构,标识为人名还是一个空的部门,当前对象为第几层级等等,以及从数据库中获取时候直接设置默认选中。
public class Node implements Serializable {
private Node parent = null; // 父节点
private List childrens = new ArrayList();//子节点
private String value;//节点显示值
private boolean isChecked = false; //是否被选中
private boolean isExpand = true;//是否处于扩展状态
private boolean hasCheckBox = true;//是否有复选框
private String parentId = null;
private String curId = null;
//父节点集合
private List parents = new ArrayList<>();
/**
* 设置节点值
*
* @param parentId
* TODO
* @param curId
* TODO
*/
public Node( String parentId, String curId, String value) {
// TODO Auto-generated constructor stub
this.value = value;
this.parentId = parentId;
this.curId = curId;
}
public List getParents() {
return parents;
}
public void setParents(Node node) {
if(node != null) {
if (!parents.contains(node)) {
parents.add(node);
}
}
}
/**
* 得到父节点
*/
public Node getParent() {
return parent;
}
/**
* 设置父节点
* @param parent
*/
public void setParent(Node parent) {
this.parent = parent;
}
/**
* 得到子节点
* @return
*/
public List getChildrens() {
return childrens;
}
/**
* pandu是否根节点
* @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 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;
}
}
下面是核心代码:
两种选择模式在treeAdapter中进行修改。
package com.example.administrator.treeview.treeView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.treeview.R;
import java.util.ArrayList;
import java.util.List;
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, List rootNodes){
this.con = context;
this.lif = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i=0;i getSelectedNode(){
Log.d("getSelectedNode", "我被执行了!");
List checks =new ArrayList() ;
for(int i = 0;i selectedNode){
for (int i=0;i
接下来是TreeListView:
package com.example.administrator.treeview.treeView;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import com.example.administrator.treeview.R;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TreeListView extends ListView {
ListView treelist = null;
TreeAdapter ta = null;
public List mNodeList;
private List checkList;
public TreeListView(final Context context, List res) {
super(context);
treelist = this;
treelist.setFocusable(false);
treelist.setBackgroundColor(0xffffff);
treelist.setFadingEdgeLength(0);
treelist.setLayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
treelist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
((TreeAdapter) parent.getAdapter()).ExpandOrCollapse(position);
}
});
initNode(context, initNodRoot(res), true, -1, -1, 0);
}
// 使用 onMeasure 方法,来解决尺寸高度的问题,以及事件冲突的问题;
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE>>2,
MeasureSpec.AT_MOST
);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
// /**
// *
// * @param context
// * 响应监听的上下文
// * @param root
// * 已经挂好树的根节点
// * @param hasCheckBox
// * 是否整个树有复选框
// * @param tree_ex_id
// * 展开iconid -1会使用默认的
// * @param tree_ec_id
// * 收缩iconid -1会使用默认的
// * @param expandLevel
// * 初始展开等级
// *
// */
public List initNodRoot(List res) {
ArrayList list = new ArrayList();
ArrayList roots = new ArrayList();
Map nodemap = new LinkedHashMap();
for (int i = 0; i < res.size(); i++) {
Node nr = res.get(i);
Node n = new Node( nr.getParentId(), nr.getCurId(), nr.getValue());
nodemap.put(n.getCurId(), n);// 生成map树
}
Set set = nodemap.keySet();
Collection collections = nodemap.values();
Iterator iterator = collections.iterator();
while (iterator.hasNext()) {// 添加所有根节点到root中
Node n = iterator.next();
if (!set.contains(n.getParentId()))
roots.add(n);
list.add(n);
}
for (int i = 0; i < list.size(); i++) {
Node n = list.get(i);
for (int j = i + 1; j < list.size(); j++) {
Node m = list.get(j);
if (m.getParentId() .equals( n.getCurId())) {
n.addNode(m);
m.setParent(n);
m.setParents(n);
} else if (m.getCurId() .equals( n.getParentId())) {
m.addNode(n);
n.setParent(m);
m.setParents(m);
}
}
}
return roots;
}
public void initNode(Context context, List root, boolean hasCheckBox,
int tree_ex_id, int tree_ec_id, int expandLevel) {
ta = new TreeAdapter(context, root);
//获取
mNodeList = ta.all;
// 设置整个树是否显示复选框
ta.setCheckBox(true);
// 设置展开和折叠时图标
int tree_ex_id_ = (tree_ex_id == -1) ? R.drawable.down_icon : tree_ex_id;
int tree_ec_id_ = (tree_ec_id == -1) ? R.drawable.right_icon : tree_ec_id;
ta.setCollapseAndExpandIcon(tree_ex_id_, tree_ec_id_);
// 设置默认展开级别
ta.setExpandLevel(expandLevel);
this.setAdapter(ta);
}
/* 返回当前所有选中节点的List数组 */
public List get() {
Log.d("get", ta.getSelectedNode().size() + "");
return ta.getSelectedNode();
}
public void setSelect(List allSelect){
ta.setSelectedNode(allSelect);
}}
资源地址:
https://download.csdn.net/download/weixin_40299948/11157435
github链接:
https://github.com/Black-Mango/treeListView
书写不易,麻烦点个赞/star/关注