Android_QQ列表实现(1)

Android_QQ列表实现(1)

Android_QQ列表实现(1)

1.创建节点Node
public class Node
{
public Node()
{
}

public Node(int id, int pId, String name)
{
this.id = id;
this.pId = pId;
this.name = name;
}
private int id;
private int pId = 0;
private String name;
private int level;
private boolean isExpand = false;
private int icon;
private Node parent;
private List children = new ArrayList();
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 int getIcon()
{
return icon;
}

public void setIcon(int icon)
{
this.icon = icon;
}

public Node getParent()
{
return parent;
}

public void setParent(Node parent)
{
this.parent = parent;
}

public List getChildren()
{
return children;
}

public void setChildren(List children)
{
this.children = children;
}
public boolean isRoot()
{
return parent == null;
}

public boolean isParentExpand()
{
if (parent == null)
return false;
return parent.isExpand();
}

public boolean isLeaf()
{
return children.size() == 0;
}

public int getLevel()
{
return parent == null ? 0 : parent.getLevel() + 1;
}
public boolean isExpand()
{
return isExpand;
}

public void setExpand(boolean isExpand)
{
this.isExpand = isExpand;
if(!isExpand)
{
for(Node node :children)
{
node.setExpand(false);
}
}
}
public void setLevel(int level)
{
this.level = level;
}

@Override
public String toString()
{
return "Node [id=" + id + ", pId=" + pId + ", name=" + name
+ ", level=" + level + ", isExpand=" + isExpand + ", icon="
+ icon + "]";
}

}
2.将用户的数据转化成根节点Node:用反射+注解
public static List convertDatas2Nodes(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;

node = new Node();
Class clazz = t.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields)
{
if (field.getAnnotation(TreeNodeId.class) != null)
{
field.setAccessible(true);
id = field.getInt(t);
}
if (field.getAnnotation(TreeNodePid.class) != null)
{
field.setAccessible(true);
pid = field.getInt(t);
}
if (field.getAnnotation(TreeNodeLabel.class) != null)
{
field.setAccessible(true);
label = (String) field.get(t);
}
}
node = new Node(id, pid, label);
nodes.add(node);
}// for end
Log.e("TAG", nodes+"");
package com.imooc.treeview.utils.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeNodeId
{
// Class type();
}

package com.imooc.treeview.utils.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeNodeLabel
{

}

package com.imooc.treeview.utils.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeNodePid
{

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值