編輯:關於Android編程
int id; int pid = 0; /** *名稱 */ String label; /** * 層級 */ int level; /** * 當前是否展開 */ private boolean isExpand =false; int index; private int icon; Node parent; ListsetExpand,getLevel這兩個方法挺有意思的,主要是一種遞歸的思想 TreeHelper類是這個Tree ListView的核心思想children = new ArrayList (); public Node(int id, int pid, String label) { super(); this.id = id; this.pid = pid; this.label = label; } 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 getLabel() { return label; } public void setLabel(String label) { this.label = label; } /** * 節點的層級 * @return */ public int getLevel() { return parent ==null?0:parent.getLevel()+1; } public void setLevel(int level) { this.level = level; } public boolean isExpand() { return isExpand; } public void setExpand(boolean isExpand) { this.isExpand = isExpand; if(!isExpand){ for(Node n :children){ n.setExpand(false); } } } 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; } /** * 父節點是否展開 * @return */ public boolean isParentExpand(){ if(parent == null) return true; return parent.isExpand(); } /** * 是否是葉節點 * @return */ public boolean isLeaf(){ return children.size() == 0; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; }
/** * 將用戶數據轉化為Node * @param datas * @return * @throws IllegalAccessException * @throws IllegalArgumentException */ public staticList convertToNodes(List datas) throws IllegalAccessException, IllegalArgumentException{ List nodes = new ArrayList (); Log.i("TAG", "convertToNodes data="+datas.size()+""); int index = 0; for(T t: datas){ Node node =null; Class clazz = t.getClass(); Field[] fileds = clazz.getDeclaredFields(); int id =-1; int pid=-1; String label=null; Log.i("TAG", "fileds.length="+fileds.length); for(Field f :fileds){ if(f.getAnnotation(TreeNodeId.class)!=null){ f.setAccessible(true); id = f.getInt(t); Log.i("TAG", "通過反射注解獲取到注解"); }else{ Log.i("TAG", "通過反射注解獲取失敗"); } if(f.getAnnotation(TreeNodePid.class)!=null){ f.setAccessible(true); pid = f.getInt(t); Log.i("TAG", "通過反射注解獲取到注解"); }else{ Log.i("TAG", "通過反射注解獲取失敗"); } if(f.getAnnotation(TreeNodeLabel.class)!=null){ f.setAccessible(true); label = (String) f.get(t); Log.i("TAG", "convertToNodes label="+label+""); Log.i("TAG", "通過反射注解獲取到注解"); }else{ Log.i("TAG", "通過反射注解獲取失敗"); } } Log.i("TAG", "convertToNodes id,pid,label="+id+"-"+pid+"-"+label); node = new Node(id, pid, label); node.setIndex(index); nodes.add(node); index++; } Log.i("TAG", "convertToNodes nodes="+nodes.size()+""); //關聯關系 for(int i =0;i 0&&n.isExpand()){ n.setIcon(R.drawable.expand); }else if(n.getChildren().size()>0&&!n.isExpand()){ n.setIcon(R.drawable.shrink); }else{ n.setIcon(-1); } } public static List getSortedNodes(List datas,int defaultExpandLevel) throws IllegalAccessException, IllegalArgumentException{ List result = new ArrayList (); List nodes = convertToNodes(datas); List rootNodes = getRootNodes(nodes); for(Node node : rootNodes){ addNode(result,node,defaultExpandLevel,1); } return result; } private static void addNode(List result, Node node, int defaultExpandLevel, int curLevel) { node.setLevel(curLevel); result.add(node); if(node.isLeaf()) return ; if(defaultExpandLevel>=curLevel){ node.setExpand(true); }else{ node.setExpand(false); } for(Node n:node.getChildren()){ addNode(result, n, defaultExpandLevel, curLevel+1); } } /** * 過濾出要顯示的 * @param nodes * @return */ public static List filterVisbleNode(List nodes){ List visbleNode = new ArrayList (); for(Node n :nodes){ if(n.isRoot()||n.isParentExpand()){ setNodeIcon(n); visbleNode.add(n); } } return visbleNode; } /** * 獲取根節點 * @param nodes * @return */ private static List getRootNodes(List nodes) { List rootNodes = new ArrayList (); for(Node n :nodes){ if(n.isRoot()){ setNodeIcon(n); rootNodes.add(n); } } return rootNodes; }
protected Context context; protected ListmAllNodes; protected List visbleNodes; protected LayoutInflater inflater; protected ListView mTree; public interface OnTreeNodeClickListener{ void onClick(Node node,int position); } private OnTreeNodeClickListener mListener; public void setOnTreeNodeClickListener(OnTreeNodeClickListener mClickListener){ this.mListener =mClickListener; } public TreeListViewAdapter(ListView mTree, Context context,List datas,int defaultExpandLevel) throws IllegalAccessException, IllegalArgumentException { // TODO Auto-generated constructor stub data this.context = context; mAllNodes =TreeHelper.getSortedNodes(datas, defaultExpandLevel); visbleNodes = TreeHelper.filterVisbleNode(mAllNodes); Log.i("TAG", "TreeListViewAdapter"+visbleNodes.size()); Log.i("TAG", "TreeListViewAdapter"+mAllNodes.size()); inflater = LayoutInflater.from(context); this.mTree = mTree; this.mTree.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { expandOrCollapse(position); if(mListener!=null){ mListener.onClick(visbleNodes.get(position), position); } } }); } /** * 點擊收縮或展開 * @param position */ protected void expandOrCollapse(int position) { Node n = visbleNodes.get(position); if(n!=null){ if(n.isLeaf()){ return ; } n.setExpand(!n.isExpand()); visbleNodes = TreeHelper.filterVisbleNode(mAllNodes); notifyDataSetChanged(); } } @Override public int getCount() { // TODO Auto-generated method stub return visbleNodes.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return visbleNodes.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Node n = visbleNodes.get(position); convertView = getConvertView(visbleNodes.get(position), position, convertView, parent); Log.i("---------", "getView="+visbleNodes.get(position).getLabel()); convertView.setPadding(30*n.getLevel(), 3, 3, 3); return convertView; } public abstract View getConvertView(Node node ,int position,View convertView,ViewGroup parent);
用過android手機的人都知道android使用app的時候屏幕上方的狀態欄都是黑色的,就算不是黑色的都與正在打開的app顏色不同。有一種灰常不搭調的感覺。
好久沒寫博客了,今天沒事怒更一記。 如標題今天我們來模仿一下搜狐新聞,先上個效果圖. 1,接下來我們就來分析一下這個效果 下面的內容應該是用
一、介紹這是新浪微博的一個帖子,剛好包括了話題、表情、@好友三種顯示。顯示方法上篇已經闡述了,就是使用SpannableString。這篇主要介紹顯示這種帖子的解析工具類
父Activity啟動子Activity,並且向其傳遞消息,子Activity啟動後完成相應的操作後回饋父Activity消息,父Activity完成相應的操作。The