服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

j2me 实现可伸展目录树treelist(图)


  j2me里面有自带的list类,但是功能太弱,没有实现view和model的分离,所以操作起来比较费事。本来事想写一个canvas的treelist,但是画起来算坐标又太麻烦,所以选取了一个折中的方法,继承list,实现一个操作起来比较方便的组件。
  
  目的:
  
  1.可伸缩的目录树结构,暂时先实现两层。
  
  2.label和存储内容分离。
  
  3.激活和非激活图片分开。
  
  4.通过选择事件可以准确快速找到对应内容
  
  5.存储内容无关性,里面可以放置任何object
  
  实现思路:
  
  1.封装一个expanditem类,用来存储每一条数据。
  
  /**
  * 默认图片
  */
  private string imagepath="";
  /*
  * 激活图片,如果为空说明此图片无效
  */
  private string selectimgpath=null;
  /**
  * 组
  */
  public static int group=1;
  /**
  * 记录
  */
  public static int item=0;
  /**
  * 是否选中,如果选中则默认为展开状态
  */
  private boolean ifselected=false;
  /**
  * 显示label
  */
  private string label;
  /**
  * 类型:组,记录
  */
  private int type;
  /**
  * 存储的对象
  */
  
  group表示这个item是一个父节点,下面包含字节点,这样它的content将是一个vector.
  
  item表示这个item是根节点。
  
  selectimgpath,是激活后的图标,可以为空,为空的时候选择了这个item图标不变。
  
  然后就是expandlist类,此类的数据结构如下:
  
  private vector itemlist = new vector();
  
  /*用来存储内容的数据结构*/
  
  private expandlistitem currentselectedobject = null;
  
  /*当前所选择的对象,方便获取*/
  
  private int currentselectedindex = -1;
  
  /*当前选择的对象在队列中的index,队列有两个,一个是真实数据的存储vector,另外一个是显示在屏幕上的队列。这两个有时候是不一样的。因为有的节点有子节点*/
  
  private vector appearhooklist = new vector();
  
  /*显示在屏幕上的label队列*/
  
  总的思路如下:
  
  初始化list的时候,参数是一个vector,里面可以是expanditem或者是vector.然后根据expanditem里面的参数初始化屏幕,如果group节点的ifselected状态为true则递归添加下面的子节点,否则只插入当前节点。图标也是一样,如果ifselected为true 则用激活图标否则用默认图标。
  
  在用户选择了一个结点后,取得当前的激活的index号码,判断是不是父节点,如果是的话,首先更新这个父节点的ifselected属性为true,然后重画这个list;(其实效率更高的方法是直接插入这个父节点的子节点,但是这样做的话,在移除的时候会稍微稍微麻烦一点。有时间我在改过来,呵呵)。如果选择的是子节点,则判断是否有激活图标,如果有,则更新这个图标,就好了。
  
  下面是效果
  
j2me 实现可伸展目录树treelist(图)
点击查看大图

  附代码一份,这是我me组件库中很早的版本了,呵呵。别的组件以后在写。其实最好的方法就是写canvas。
  
  --------------------------------------------------------------------------------
  
  expandlist.java
  
  package com.skystudio.expandlist;
  
  public class expandlistitem {
  public expandlistitem(object content,string imgpath,string selectimgpath,string label,int type,boolean ifselected){
  this.selectimgpath=selectimgpath;
  this.imagepath=imgpath;
  this.content=content;
  this.label=label;
  this.type=type;
  this.ifselected=ifselected;
  }
  /**
  * 默认图片
  */
  private string imagepath="";
  /*
  * 激活图片,如果为空说明此图片无效
  */
  private string selectimgpath=null;
  /**
  * 组
  */
  public static int group=1;
  /**
  * 记录
  */
  public static int item=0;
  /**
  * 是否选中
  */
  private boolean ifselected=false;
  /**
  * 显示label
  */
  private string label;
  /**
  * 类型:组,记录
  */
  private int type;
  /**
  * 存储的对象
  */
  private object content;
  
  public object getcontent() {
  return content;
  }
  public void setcontent(object content) {
  this.content = content;
  }
  public string getlabel() {
  return label;
  }
  public void setlabel(string label) {
  this.label = label;
  }
  public int gettype() {
  return type;
  }
  public void settype(int type) {
  this.type = type;
  }
  public boolean ifselected() {
  return ifselected;
  }
  public void setifselected(boolean ifselected) {
  this.ifselected = ifselected;
  }
  public string tostring() {
  
  return this.label+" ";
  }
  public string getimagepath() {
  return imagepath;
  }
  public void setimagepath(string imagepath) {
  this.imagepath = imagepath;
  }
  public string getselectimgpath() {
  return selectimgpath;
  }
  public void setselectimgpath(string selectimgpath) {
  this.selectimgpath = selectimgpath;
  }
  }
  
  --------------------------------------------------------------------------------
  
  package com.skystudio.expandlist;
  
  import java.util.vector;
  
  import javax.microedition.lcdui.command;
  import javax.microedition.lcdui.commandlistener;
  import javax.microedition.lcdui.displayable;
  import javax.microedition.lcdui.image;
  import javax.microedition.lcdui.list;
  
  import com.skystudio.ui.toolkit.util;
  
  /**
  * @author sky
  *
  */
  public class expandlist extends list implements commandlistener {
  private vector itemlist = new vector();
  
  private expandlistitem currentselectedobject = null;
  
  private int currentselectedindex = -1;
  
  private vector appearhooklist = new vector();
  
  public expandlist(string title, int type, vector itemlist) {
  super(title, type);
  this.itemlist = itemlist;
  this.setcommandlistener(this);
  loadlist();
  }
  
  public void appenditem(expandlistitem item, image icon, boolean ifsub) {
  appearhooklist.addelement(item);
  system.out.println("add current display list:" + item);
  if (!ifsub) {
  this.append(item.getlabel(), icon);
  } else {
  this.append(" " + item.getlabel(), icon);
  }
  
  }
  
  public void init() {
  int count = this.size();
  for (int i = 0; i < count; i++) {
  this.delete(0);
  }
  this.appearhooklist.removeallelements();
  system.out.println("now itemlist:" + this.itemlist);
  }
  
  public void loadlist() {
  init();
  for (int i = 0; i < itemlist.size(); i++) {
  expandlistitem elitem = (expandlistitem) itemlist.elementat(i);
  if (elitem.gettype() == expandlistitem.group) {
  image icon = util.getimage(elitem.getimagepath());
  /**
  * @debug
  */
  if (elitem.ifselected()) {
  if (elitem.getselectimgpath() != null) {
  icon = util.getimage(elitem.getselectimgpath());
  }
  system.out.println("add parent node:");
  this.appenditem(elitem, icon, false);
  vector group = (vector) elitem.getcontent();
  for (int j = 0; j < group.size(); j++) {
  expandlistitem item = (expandlistitem) group
  .elementat(j);
  image ic = util.getimage(item.getimagepath());
  system.out.println("add sub node:");
  this.appenditem(item, ic, true);
  }
  } else {
  system.out.println("add leave node:");
  this.appenditem(elitem, icon, false);
  }
  
  } else if (elitem.gettype() == expandlistitem.item) {
  image icon = util.getimage(elitem.getimagepath());
  this.appenditem(elitem, icon, false);
  }
  
  }
  if (this.currentselectedindex != -1) {
  thi

扫描关注微信公众号