目的:
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;
/**
* 存储的对象
*/
* 默认图片
*/
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;(其实效率更高的方法是直接插入这个父节点的子节点,但是这样做的话,在移除的时候会稍微稍微麻烦一点。有时间我在改过来,呵呵)。如果选择的是子节点,则判断是否有激活图标,如果有,则更新这个图标,就好了。
下面是效果



附代码一份,这是我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;
}
}
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 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();
* @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();
}
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);
}
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);
}
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);
}
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);
}
image icon = util.getimage(elitem.getimagepath());
this.appenditem(elitem, icon, false);
}
}
if (this.currentselectedindex != -1) {
this.setselectedindex(currentselectedindex, true);
}
}
if (this.currentselectedindex != -1) {
this.setselectedindex(currentselectedindex, true);
}
}
public vector getitemlist() {
return itemlist;
}
return itemlist;
}
public void setitemlist(vector itemlist) {
this.itemlist = itemlist;
}
this.itemlist = itemlist;
}
public void commandaction(command arg0, displayable arg1) {
if (arg0 == list.select_command) {
/**
* set current list selected status
*/
this.currentselectedindex = this.getselectedindex();
system.out.println(this.appearhooklist);
if (arg0 == list.select_command) {
/**
* set current list selected status
*/
this.currentselectedindex = this.getselectedindex();
system.out.println(this.appearhooklist);
this.currentselectedobject = (expandlistitem) this.appearhooklist
.elementat(currentselectedindex);
.elementat(currentselectedindex);
int indexinitemlist = this.itemlist.indexof(this.appearhooklist
.elementat(this.getselectedindex()));
system.out.println(" selected: " + currentselectedindex + " "
+ this.currentselectedobject + " indexinitemlist:"
+ indexinitemlist);
/**
*
*/
if (this.currentselectedobject.gettype() == expandlistitem.group) {
if (this.currentselectedobject.ifselected() == false) {// previous
// item
// status
// is
// contractive,need
// to be
// expanded.
system.out.println(this.currentselectedobject.ifselected());
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(true);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
} else {
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(false);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
}
this.init();
this.loadlist();
} else {
.elementat(this.getselectedindex()));
system.out.println(" selected: " + currentselectedindex + " "
+ this.currentselectedobject + " indexinitemlist:"
+ indexinitemlist);
/**
*
*/
if (this.currentselectedobject.gettype() == expandlistitem.group) {
if (this.currentselectedobject.ifselected() == false) {// previous
// item
// status
// is
// contractive,need
// to be
// expanded.
system.out.println(this.currentselectedobject.ifselected());
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(true);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
} else {
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(false);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
}
this.init();
this.loadlist();
} else {
if (this.currentselectedobject.getselectimgpath() != null) {
if (this.currentselectedobject.ifselected() == false) {
image icon = util.getimage(this.currentselectedobject
.getselectimgpath());
system.out.println(this.currentselectedobject
.ifselected());
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(true);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
this.delete(this.currentselectedindex);
this.insert(this.currentselectedindex,
this.currentselectedobject.getlabel(), icon);
} else {
image icon = util.getimage(this.currentselectedobject
.getimagepath());
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(false);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
this.delete(this.currentselectedindex);
this.insert(this.currentselectedindex,
this.currentselectedobject.getlabel(), icon);
}
this.setselectedindex(this.currentselectedindex,true);
}
if (this.currentselectedobject.ifselected() == false) {
image icon = util.getimage(this.currentselectedobject
.getselectimgpath());
system.out.println(this.currentselectedobject
.ifselected());
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(true);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
this.delete(this.currentselectedindex);
this.insert(this.currentselectedindex,
this.currentselectedobject.getlabel(), icon);
} else {
image icon = util.getimage(this.currentselectedobject
.getimagepath());
this.itemlist.removeelementat(indexinitemlist);
this.currentselectedobject.setifselected(false);
this.itemlist.insertelementat(currentselectedobject,
indexinitemlist);
this.delete(this.currentselectedindex);
this.insert(this.currentselectedindex,
this.currentselectedobject.getlabel(), icon);
}
this.setselectedindex(this.currentselectedindex,true);
}
}
}
}
}
}
}
附测试代码
import java.util.vector;
import javax.microedition.lcdui.choice;
import javax.microedition.lcdui.display;
import javax.microedition.midlet.midlet;
import javax.microedition.midlet.midletstatechangeexception;
import javax.microedition.lcdui.display;
import javax.microedition.midlet.midlet;
import javax.microedition.midlet.midletstatechangeexception;
import com.skystudio.canvas.listcanvas;
import com.skystudio.expandlist.expandlist;
import com.skystudio.expandlist.expandlistitem;
import com.skystudio.expandlist.expandlist;
import com.skystudio.expandlist.expandlistitem;
public class main extends midlet {
display d=null;
protected void startapp() throws midletstatechangeexception {
d=display.getdisplay(this);
listtest();
}
private void testui(){
listcanvas l=new listcanvas();
d.setcurrent(l);
}
private void listtest(){
vector v1=new vector();
for(int i=0;i<10;i++){
v1.addelement(new expandlistitem("土匪"+integer.tostring(i),"/img/default.png","/img/group-open.png","土匪"+integer.tostring(i),expandlistitem.item,false));
}
string v2="警察";
vector v3=new vector();
for(int i=0;i<10;i++){
v3.addelement(new expandlistitem("警察"+integer.tostring(i),"/img/default.png","/img/group-open.png","警察"+integer.tostring(i),expandlistitem.item,false));
}
vector v=new vector();
v.addelement(new expandlistitem(v1,"/img/group-close.png","/img/group-open.png","土匪帮",expandlistitem.group,false));
v.addelement(new expandlistitem(v3,"/img/group-close.png","/img/group-open.png","警察局",expandlistitem.group,false));
v.addelement(new expandlistitem(v2,"/img/default.png","/img/group-open.png","法官",expandlistitem.item,false));
d.setcurrent(new expandlist("花名册",choice.implicit,v));
}
display d=null;
protected void startapp() throws midletstatechangeexception {
d=display.getdisplay(this);
listtest();
}
private void testui(){
listcanvas l=new listcanvas();
d.setcurrent(l);
}
private void listtest(){
vector v1=new vector();
for(int i=0;i<10;i++){
v1.addelement(new expandlistitem("土匪"+integer.tostring(i),"/img/default.png","/img/group-open.png","土匪"+integer.tostring(i),expandlistitem.item,false));
}
string v2="警察";
vector v3=new vector();
for(int i=0;i<10;i++){
v3.addelement(new expandlistitem("警察"+integer.tostring(i),"/img/default.png","/img/group-open.png","警察"+integer.tostring(i),expandlistitem.item,false));
}
vector v=new vector();
v.addelement(new expandlistitem(v1,"/img/group-close.png","/img/group-open.png","土匪帮",expandlistitem.group,false));
v.addelement(new expandlistitem(v3,"/img/group-close.png","/img/group-open.png","警察局",expandlistitem.group,false));
v.addelement(new expandlistitem(v2,"/img/default.png","/img/group-open.png","法官",expandlistitem.item,false));
d.setcurrent(new expandlist("花名册",choice.implicit,v));
}
protected void pauseapp() {
// todo auto-generated method stub
// todo auto-generated method stub
}
protected void destroyapp(boolean arg0) throws midletstatechangeexception {
// todo auto-generated method stub
// todo auto-generated method stub
}
}
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


