hyman老师真的是大神级别的人,跟他学习了很多,也给了我很多解决问题的思路,膜拜。这次是TreeView的实现,中间用到了注解和反射,还有大部分数据结构的知识,获益多多;
效果图:
TreeHelper.java:package utis; import com.songdongwan.treeview.Node; import com.songdongwan.treeview.R; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import Annotation.TreeNodeId; import Annotation.TreeNodeLabel; import Annotation.TreeNodePid; /** * Created by Administrator on 2016/5/8. */ public class TreeHelper { public static <T> List<Node> convertDatas2Nodes(List<T> datas) throws IllegalAccessException { List<Node> result = new ArrayList<Node>(); for (T data : datas) { Node node = null; int id = -1; int Pid = -1; String label = null; Class clazz = data.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.getAnnotation(TreeNodeId.class) != null) { field.setAccessible(true); id = field.getInt(data); } else if (field.getAnnotation(TreeNodePid.class) != null) { field.setAccessible(true); Pid = field.getInt(data); } else if (field.getAnnotation(TreeNodeLabel.class) != null) { field.setAccessible(true); label = (String) field.get(data); } } node = new Node(id, Pid, label); result.add(node); } //设置Node间的节点关系,确定父节点和子节点 for (int i = 0; i < result.size(); i++) { Node n = result.get(i); for (int j = i + 1; j < result.size(); j++) { Node m = result.get(j); if (n.getpId() == m.getId()) { n.setParentNode(m); m.getChildrenNode().add(n); } if (n.getId() == m.getpId()) { m.setParentNode(n); n.getChildrenNode().add(m); } } } //设置图片 for (int i = 0; i < result.size(); i++) { setNodeIcon(result.get(i)); } return result; } /** * 设置节点图片 * * @param node */ public static void setNodeIcon(Node node) { if (node.getChildrenNode().size()!= 0) { if (!node.getIsOpen()) { node.setIcon(R.mipmap.tree_ec); } else { node.setIcon(R.mipmap.tree_ex); } } else { node.setIcon(-1); } } /** * 排序节点 * * @param datas * @param defaultExpandLevel * @param <T> * @return * @throws IllegalAccessException */ public static <T> List<Node> getSortedNodes(List<T> datas, int defaultExpandLevel) throws IllegalAccessException { List<Node> nodes = new ArrayList<Node>(); nodes = convertDatas2Nodes(datas); List<Node> result = new ArrayList<>(); List<Node> rootNodes = getRootNodes(nodes); for (Node node : rootNodes) { addChilNodes(result, node, defaultExpandLevel, 1); } return result; } public static List<Node> filterVisuableNode(List<Node> nodes) { List<Node> result = new ArrayList<>(); for (Node node : nodes) { if (node.isRoot() || node.isParentOpen()) { setNodeIcon(node); result.add(node); } } return result; } /** * 把所有根节点的子节点加入进去 * * @param nodes * @param node * @param defaultExpandLevel * @param currentLevel */ public static void addChilNodes(List<Node> nodes, Node node, int defaultExpandLevel, int currentLevel) { nodes.add(node); if (defaultExpandLevel >= currentLevel) { node.setIsOpen(true); } if (node.isLeaf()) { return; } for (int i = 0; i < node.getChildrenNode().size(); i++) { addChilNodes(nodes, node.getChildrenNode().get(i), defaultExpandLevel, currentLevel + 1); } } public static List<Node> getRootNodes(List<Node> node) { List<Node> rootNode = new ArrayList<>(); for (int i = 0; i < node.size(); i++) { if (node.get(i).isRoot()) { rootNode.add(node.get(i)); } } return rootNode; } }TreeViewAdapter.java:package utis; 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 com.songdongwan.treeview.Node; import java.util.List; /** * Created by Administrator on 2016/5/8. */ public abstract class TreeViewAdapter<T> extends BaseAdapter { protected List<Node> allNodes; protected List<Node> visuableNodes; protected Context context; protected LayoutInflater layoutInflater; protected ListView mtree; protected OnTreeNodeClikListener onTreeNodeClickListener; public interface OnTreeNodeClikListener { public void onTreeClick(Node node, int poisition); } public void setOnTreeNodeClikListener(OnTreeNodeClikListener onTreeNodeClickListener) { this.onTreeNodeClickListener = onTreeNodeClickListener; } public TreeViewAdapter(ListView tree, Context context, List<T> datas, int defaultLevel) throws IllegalAccessException { this.context = context; mtree = tree; allNodes = TreeHelper.getSortedNodes(datas, defaultLevel); visuableNodes = TreeHelper.filterVisuableNode(allNodes); layoutInflater = LayoutInflater.from(context); mtree.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { expandOrCollapse(position); if (onTreeNodeClickListener != null) { onTreeNodeClickListener.onTreeClick(visuableNodes.get(position), position); } } }); } /** * 点击展开或收缩 * * @param position */ private void expandOrCollapse(int position) { Node node = visuableNodes.get(position); if (node != null) { if (node.isLeaf()) return; node.setIsOpen(!node.getIsOpen()); setChildrenClose(node); visuableNodes = TreeHelper.filterVisuableNode(allNodes); notifyDataSetChanged(); } } /** * 根据父节点的展开变化,都让子节点关闭 * * @return */ static public void setChildrenClose(Node node) { for (int i = 0; i < node.getChildrenNode().size(); i++) { node.getChildrenNode().get(i).setIsOpen(false); } } @Override public int getCount() { return visuableNodes.size(); } @Override public Object getItem(int position) { return visuableNodes.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Node node = visuableNodes.get(position); convertView = getConvertView(node, position, convertView, parent); convertView.setPadding(node.getLevel() * 40, 3, 3, 3); return convertView; } public abstract View getConvertView(Node node, int position, View convertView, ViewGroup parent); }接下来是使用注解定位: TreeNodeId:@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface TreeNodeId { }
源代码下载