Android树形控件的实现方法
程序员文章站
2024-03-01 22:03:58
在pc上我们已经习惯了树形控件,因为其可以清晰的展现各个节点之间的层次结果,但是在android平台上,系统并没有提供这样一个控件,而是只有listview。不过通过改写与...
在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, view view, 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, 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>
实现效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。