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;(實在效率更高的方法是直接插進這個父節點的子節點,但是這樣做的話,在移除的時候會稍微稍微麻煩一點。有時間我在改過來,呵呵)。假如選擇的是子節點,則判定是否有激活圖標,假如有,則更新這個圖標,就好了。
這是我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) {
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
}
}