时间: 2021-07-31 作者:daque
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;(本来功效更高的本领是径直插入这个父节点的子节点,然而如许做的话,在移除的功夫会略微略微烦恼一点。有功夫我在改正来,呵呵)。即使采用的是子节点,则确定能否有激活图标,即使有,则革新这个图标,就好了。 底下是功效[page_break] 附代码一份,这是我me组件库中很早的本子了。其余组件此后在写。本来最佳的本领即是写canvas。expandlist.javapackage 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) { this.setselectedindex(currentselectedindex, true); } } public vector getitemlist() { return itemlist; } public void setitemlist(vector 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); this.currentselectedobject = (expandlistitem) this.appearhooklist.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 { 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); } } } }} 附尝试代码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 com.skystudio.canvas.listcanvas;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)); } protected void pauseapp() { // todo auto-generated method stub } protected void destroyapp(boolean arg0) throws midletstatechangeexception { // todo auto-generated method stub }}