栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android树形控件的实现方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android树形控件的实现方法

在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; 
 
 
public class Node { 
 private Node parent=null;//父节点 
 private List 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(); 
  } 
  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 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 nodeList=new ArrayList ();//所有的节点 
 private List nodeListToShow=new ArrayList();//要展现的节点 
 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 children=node.getChildren(); 
  for(int i=0;i children=node.getChildren(); 
   for(int i=0;i

listview_item.xml

 
 
  
  
  
 
  
 
 

实现效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号