We are accustomed to the tree control on the PC because it can clearly show the hierarchical results between the nodes, but on the Android platform, the system does not provide such a control, but only ListView. However, this can be achieved by overwriting the adapter bound with ListView.
A ListView needs to be bound to a adapter to manage the data. In Baseadapter, for example, inheriting adapter needs to rewrite four functions, the more important of which is two:
1 public int GetCount ()//The function returns the number of ListView ListItem
2 public View GetView (int position, view view, ViewGroup arg2)//is responsible for drawing each item. If GetCount () returns 10, then GetView () is called 10 times.
Start by developing your own data structures:
Package bupt.liyazhou.ui;
Import java.util.ArrayList;
Import java.util.List; /* @ Author:liyazhou * @date: 2013.4.29 * @description: Node class is used to store information about a node in the UI layer * * */public class Node {privat
E node parent=null;//parent nodes private list<node> children=null; private string oid=null;//The OID private string of the node name=null;//the description of the node information private string value=null;//the value of the node private bool Ean isleaf=false;//is a leaf node private Boolean isexpanded=false;//whether the node expands private int icon=-1;//The node's icon corresponds to the ID private int i
Conforexpandedorfolded=-1;
private int iconforexpanding=-1;
private int iconforfolding=-1; Private Boolean tableitemornot=false;//represents a column of public Node (node parent,string oid,string description,boolean for a table structure isLe
Af,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;
}//Set value public void SetValue (String value) {this.value=value;
//Get Value public String GetValue () {return this.value;
//Set icon public void SetIcon (int icon) {This.icon=icon;
public int GetIcon () {return this.icon;
//Get Description public String getdescription () {return this.name;
//Get OID public String getoid () {return this.oid;
}//Get whether the leaf node is public boolean isleafornot () {return this.isleaf;
///Get the number of layers of the current node, the root is 0 layer public int getlevel () {return parent==null?0:parent.getlevel () +1;
}//Set whether the public void setexpanded (Boolean isexpanded) {this.isexpanded=isexpanded is expanded;
public Boolean getexpanded () {return this.isexpanded; ///Add child nodes public void Addchildnode (node child) {if (this.children==null) {this.children=new arraylist< NOde> ();
} this.children.add (child);
//Clear the empty node public void Clearchildren () {if (!this.children.equals (null)) {this.children.clear ();
//Is the root node public boolean isRoot () {return this.parent.equals (null)? True:false;
//Set the expand icon public void Setexpandicon (int expand) {This.iconforexpanding=expand;
//Set the collapse icon public void Setfoldicon (int fold) {this.iconforfolding=fold;
//Get the expand or collapse icon public int Getexpandorfoldicon () {if (this.isexpanded==true) return this.iconforexpanding;
else return this.iconforfolding;
///Get subtree public list<node> GetChildren () {return this.children;
}
}
Then write your own 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> ();//all Nodes private list<node> nodelisttoshow=new
Arraylist<node> ()//To show the 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); }//Constructs to be displayed in 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)); To get a node from the OID and change its state public void changenodeexpandorfold (int position) {String oid=this.nodelist
Toshow.get (position). Getoid (); for (int i=0;i<this.nodelist.size (); i++) {if (Nodelist.get (i). Getoid (). Equals (OID)) {Boolean Flag=nodel
Ist.get (i). getexpanded (); Nodelist.get (i). setexpanded (!FLAG); //listitem clicked Response Event public Node Onlistitemclick (int position) {node NODE=THIS.NODELISTTOSHOW.G
ET (position);
if (Node.isleafornot ()) {//Handle SNMP code Toast.maketext (this.context, "This node is a child node", Toast.length_short). Show ();
return node;
else {this.changenodeexpandorfold (position);
This.setnodelisttoshow ();
This.notifydatasetchanged ();
return null;
The public int GetCount () {//TODO auto-generated a stub return nodelisttoshow.size ();
The 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 Hol
Der=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);
///Draw an item//Set text Node node= this.nodeListToShow.get (position);
Holder.description.setText (Node.getdescription ());
Set icon int Icon=node.geticon ();
if (icon!=-1) {holder.nodeIcon.setImageResource (icon);
Holder.nodeIcon.setVisibility (view.visible);
else holder.nodeIcon.setVisibility (view.invisible);
Set the expand Collapse icon 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>
Implementation effect:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.