在PC上我们已经习惯了树形控件,因为其可以清晰的展现各个节点之间的层次结果,但是在AndroID平台上,系统并没有提供这样一个控件,而是只有ListVIEw。不过通过改写与ListVIEw绑定的Adapter可以实现这样一个效果。
一个ListVIEw需要和一个Adapter绑定,用于管理数据。在这里以BaseAdapter为例,继承Adapter需要重写四个函数,其中较为重要的是两个:
1 public int getCount();//该函数返回ListVIEw 的ListItem的条数
2 public VIEw getVIEw(int position,VIEw vIEw,VIEwGroup arg2)//负责绘制每一个item。如果getCount()返回10,那么getVIEw()就会被调用10次。
首先开发自己的数据结构:
package bupt.liyazhou.ui; import java.util.ArrayList; import java.util.List; /* * @ author:liyazhou * @date:2013.4.29 * @description:Node类用来在UI层中存储一个节点的信息 * */ public class Node { private Node parent=null;//父节点 private List<Node> children=null; private String oID=null;//该节点的oID private String name=null;//该节点信息的描述 private String value=null;//该节点的值 private boolean isLeaf=false;//是否为叶节点 private boolean isExpanded=false;//该节点是否展开 private int icon=-1;//该节点的图标对应的ID private int iconForExpandedOrFolded=-1; private int iconForExpanding=-1; private int iconForFolding=-1; private boolean tableItemOrNot=false;//表示是否为表结构的一列 public Node(Node parent,String oID,String description,boolean isLeaf,int icon,int exIcon,int foIcon) { this.parent=parent; this.oID=oID; this.name=description; this.isLeaf=isLeaf; this.icon=icon; this.iconForExpanding=exIcon; this.iconForFolding=foIcon; } public voID settableItemOrNot(boolean tableItemOrNot) { this.tableItemOrNot=tableItemOrNot; } public boolean gettableItemOrNot() { return this.tableItemOrNot; } //设置value public voID setValue(String value) { this.value=value; } //得到value public String getValue() { return this.value; } //设置图标 public voID setIcon(int icon) { this.icon=icon; } public int getIcon() { return this.icon; } //得到description public String getDescription() { return this.name; } //得到oID public String getoID() { return this.oID; } //得到是否为叶节点 public boolean isLeafOrNot() { return this.isLeaf; } //得到当前节点所在的层数,根为0层 public int getLevel() { return parent==null?0:parent.getLevel()+1; } //设置是否展开 public voID setExpanded(boolean isExpanded) { this.isExpanded=isExpanded; } public boolean getExpanded() { return this.isExpanded; } //添加子节点 public voID addChildNode(Node child) { if(this.children==null) { this.children=new ArrayList<Node>(); } this.children.add(child); } //清空子节点 public voID clearChildren() { if(!this.children.equals(null)) { this.children.clear(); } } //是否为根节点 public boolean isRoot() { return this.parent.equals(null)?true:false; } //设置展开图标 public voID setExpandIcon(int expand) { this.iconForExpanding=expand; } //设置折叠图标 public voID setFoldIcon(int fold) { this.iconForFolding=fold; } //得到展开或折叠图标 public int getExpandOrFoldIcon() { if(this.isExpanded==true) return this.iconForExpanding; else return this.iconForFolding; } //得到子树 public List<Node> getChildren() { return this.children; } }
然后写自己的Adapter
package bupt.liyazhou.ui; import java.util.ArrayList; import java.util.List; import androID.R; import androID.content.Context; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.BaseAdapter; import androID.Widget.ImageVIEw; import androID.Widget.TextVIEw; import androID.Widget.Toast; public class MibTreelistadapter extends BaseAdapter { private Context context=null; private List<Node> nodeList=new ArrayList<Node> ();//所有的节点 private List<Node> nodeListToShow=new ArrayList<Node>();//要展现的节点 private LayoutInflater inflater=null; private Node root=null; public MibTreelistadapter(Context con,Node Root,int layout) { this.context=con; this.inflater=(LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); establishNodeList(Root); this.root=Root; setNodeListToShow(); } public voID establishNodeList(Node node) { nodeList.add(node); if(node.isLeafOrNot()) return; List<Node> children=node.getChildren(); for(int i=0;i<children.size();i++) { establishNodeList(children.get(i)); } } public voID setNodeListToShow() { this.nodeListToShow.clear(); establishNodeListToShow(this.root); } //构造要展示在ListvIEw的nodeListToShow public voID establishNodeListToShow(Node node) { this.nodeListToShow.add(node); if(node.getExpanded()&&!node.isLeafOrNot()&&node.getChildren()!=null) { List<Node> children=node.getChildren(); for(int i=0;i<children.size();i++) { establishNodeListToShow(children.get(i)); } } } //根据oID得到某一个Node,并更改其状态 public voID changeNodeExpandOrFold(int position) { String oID=this.nodeListToShow.get(position).getoID(); for(int i=0;i<this.nodeList.size();i++) { if(nodeList.get(i).getoID().equals(oID)) { boolean flag=nodeList.get(i).getExpanded(); nodeList.get(i).setExpanded(!flag); } } } //ListItem被点击的响应事件 public Node OnListItemClick(int position) { Node node=this.nodeListToShow.get(position); if(node.isLeafOrNot()) { //处理snmp代码 Toast.makeText(this.context,"该节点为子节点",Toast.LENGTH_SHORT).show(); return node; } else { this.changeNodeExpandOrFold(position); this.setNodeListToShow(); this.notifyDataSetChanged(); return null; } } public int getCount() { // Todo auto-generated method stub return nodeListToShow.size(); } public Object getItem(int arg0) { // Todo auto-generated method stub return nodeListToShow.get(arg0); } public long getItemID(int arg0) { // Todo auto-generated method stub return arg0; } public VIEw getVIEw(int position,VIEwGroup parent) { // Todo auto-generated method stub Holder holder=null; if(vIEw!=null) { holder=(Holder)vIEw.getTag(); } else { holder=new Holder(); vIEw=this.inflater.inflate(bupt.liyazhou.R.layout.ListvIEw_item,null); holder.description=(TextVIEw)vIEw.findVIEwByID(bupt.liyazhou.R.ID.textvIEw_nodeDescription); holder.nodeIcon=(ImageVIEw)vIEw.findVIEwByID(bupt.liyazhou.R.ID.imagevIEw_nodeImage); holder.expandOrFoldIcon=(ImageVIEw)vIEw.findVIEwByID(bupt.liyazhou.R.ID.imagevIEw_expandedImage); vIEw.setTag(holder); } //绘制一个item //设置文字 Node node= this.nodeListToShow.get(position); holder.description.setText(node.getDescription()); //设置图标 int icon=node.getIcon(); if(icon!=-1) { holder.nodeIcon.setimageResource(icon); holder.nodeIcon.setVisibility(VIEw.VISIBLE); } else holder.nodeIcon.setVisibility(VIEw.INVISIBLE); //设置展开折叠图标 if(!node.isLeafOrNot()) { int expandIcon=node.getExpandOrFoldIcon(); if(expandIcon==-1) holder.expandOrFoldIcon.setVisibility(VIEw.INVISIBLE); else { holder.expandOrFoldIcon.setimageResource(expandIcon); holder.expandOrFoldIcon.setVisibility(VIEw.VISIBLE); } } else { holder.expandOrFoldIcon.setVisibility(VIEw.INVISIBLE); } vIEw.setpadding(node.getLevel()*35,10,10); return vIEw; } public class Holder { TextVIEw description; ImageVIEw nodeIcon; ImageVIEw expandOrFoldIcon; } }
ListvIEw_item.xml
<?xml version="1.0" enCoding="utf-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <ImageVIEw androID:ID="@+ID/imagevIEw_nodeImage" androID:layout_height="fill_parent" androID:layout_wIDth="wrap_content" androID:layout_alignParentleft="true" androID:paddingRight="10dp"/> <TextVIEw androID:ID="@+ID/textvIEw_nodeDescription" androID:layout_height="fill_parent" androID:layout_wIDth="wrap_content" androID:layout_toRightOf="@ID/imagevIEw_nodeImage" /> <ImageVIEw androID:ID="@+ID/imagevIEw_expandedImage" androID:layout_height="fill_parent" androID:layout_wIDth="wrap_content" androID:layout_alignParentRight="true"/> </relativeLayout>
实现效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android树形控件的实现方法全部内容,希望文章能够帮你解决Android树形控件的实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)