国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android樹形控件的實(shí)現(xiàn)方法

2019-12-12 04:46:52
字體:
供稿:網(wǎng)友

在PC上我們已經(jīng)習(xí)慣了樹形控件,因?yàn)槠淇梢郧逦恼宫F(xiàn)各個(gè)節(jié)點(diǎn)之間的層次結(jié)果,但是在Android平臺(tái)上,系統(tǒng)并沒有提供這樣一個(gè)控件,而是只有ListView。不過通過改寫與ListView綁定的Adapter可以實(shí)現(xiàn)這樣一個(gè)效果。

 一個(gè)ListView需要和一個(gè)Adapter綁定,用于管理數(shù)據(jù)。在這里以BaseAdapter為例,繼承Adapter需要重寫四個(gè)函數(shù),其中較為重要的是兩個(gè):
   1 public int getCount();//該函數(shù)返回ListView 的ListItem的條數(shù)
   2 public View getView(int position, View view, ViewGroup arg2)//負(fù)責(zé)繪制每一個(gè)item。如果getCount()返回10,那么getView()就會(huì)被調(diào)用10次。

首先開發(fā)自己的數(shù)據(jù)結(jié)構(gòu):

package bupt.liyazhou.ui;  import java.util.ArrayList; import java.util.List;  /*  * @ author:liyazhou  * @date:2013.4.29  * @description:Node類用來在UI層中存儲(chǔ)一個(gè)節(jié)點(diǎn)的信息  *  */ public class Node {  private Node parent=null;//父節(jié)點(diǎn)  private List<Node> children=null;  private String oid=null;//該節(jié)點(diǎn)的oid  private String name=null;//該節(jié)點(diǎn)信息的描述  private String value=null;//該節(jié)點(diǎn)的值  private boolean isLeaf=false;//是否為葉節(jié)點(diǎn)  private boolean isExpanded=false;//該節(jié)點(diǎn)是否展開  private int icon=-1;//該節(jié)點(diǎn)的圖標(biāo)對(duì)應(yīng)的id  private int iconForExpandedOrFolded=-1;  private int iconForExpanding=-1;  private int iconForFolding=-1;  private boolean tableItemOrNot=false;//表示是否為表結(jié)構(gòu)的一列    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;  }  //設(shè)置value  public void setValue(String value)  {   this.value=value;  }  //得到value  public String getValue()  {   return this.value;  }  //設(shè)置圖標(biāo)  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;  }  //得到是否為葉節(jié)點(diǎn)  public boolean isLeafOrNot()  {   return this.isLeaf;  }  //得到當(dāng)前節(jié)點(diǎn)所在的層數(shù),根為0層  public int getLevel()  {   return parent==null?0:parent.getLevel()+1;  }  //設(shè)置是否展開  public void setExpanded(boolean isExpanded)  {   this.isExpanded=isExpanded;  }  public boolean getExpanded()  {   return this.isExpanded;  }  //添加子節(jié)點(diǎn)  public void addChildNode(Node child)  {   if(this.children==null)   {    this.children=new ArrayList<Node>();   }   this.children.add(child);  }  //清空子節(jié)點(diǎn)  public void clearChildren()  {   if(!this.children.equals(null))   {    this.children.clear();   }  }  //是否為根節(jié)點(diǎn)  public boolean isRoot()  {   return this.parent.equals(null)?true:false;  }  //設(shè)置展開圖標(biāo)  public void setExpandIcon(int expand)  {   this.iconForExpanding=expand;  }  //設(shè)置折疊圖標(biāo)  public void setFoldIcon(int fold)  {   this.iconForFolding=fold;  }  //得到展開或折疊圖標(biāo)  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> ();//所有的節(jié)點(diǎn)  private List<Node> nodeListToShow=new ArrayList<Node>();//要展現(xiàn)的節(jié)點(diǎn)  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);  }      //構(gòu)造要展示在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));    }   }  }    //根據(jù)oid得到某一個(gè)Node,并更改其狀態(tài)  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被點(diǎn)擊的響應(yīng)事件  public Node OnListItemClick(int position)  {   Node node=this.nodeListToShow.get(position);   if(node.isLeafOrNot())   {    //處理snmp代碼    Toast.makeText(this.context, "該節(jié)點(diǎn)為子節(jié)點(diǎn)", 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);   }      //繪制一個(gè)item      //設(shè)置文字   Node node= this.nodeListToShow.get(position);   holder.description.setText(node.getDescription());      //設(shè)置圖標(biāo)   int icon=node.getIcon();   if(icon!=-1)   {    holder.nodeIcon.setImageResource(icon);    holder.nodeIcon.setVisibility(View.VISIBLE);   }   else    holder.nodeIcon.setVisibility(View.INVISIBLE);      //設(shè)置展開折疊圖標(biāo)   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> 

實(shí)現(xiàn)效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 云梦县| 图们市| 宜昌市| 安岳县| 涟水县| 钟祥市| 高唐县| 汾西县| 仁怀市| 柘城县| 武义县| 承德市| 象州县| 永靖县| 南京市| 义马市| 台江县| 遵化市| 凌海市| 南康市| 监利县| 永康市| 房山区| 吉林市| 凤翔县| 泸溪县| 道真| 前郭尔| 普定县| 游戏| 焉耆| 鄂州市| 二连浩特市| 潼关县| 贡嘎县| 安新县| 新乐市| 通江县| 大冶市| 井冈山市| 房山区|