J2ME has its own list class, but the function is too weak to realize the separation of view and model, so the operation is more trouble. Originally wanted to write a canvas of treelist, but the picture to calculate the coordinates is too troublesome, so chose a compromise method, inherit list, realize a more convenient operation of components.
Objective:
1. Scalable directory tree structure, the first two-tier implementation.
2.Label and storage content separation.
3. Activation and non-active pictures are separated.
4. To find the content accurately and quickly by selecting events
5. Storage content independent, inside can place any object
Realize the idea:
1. Encapsulates a Expanditem class for storing each piece of data.
/** * 默认图片 */ 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 indicates that the item is a parent node, which contains a byte point so that its content will be a Vector. Item indicates that this item is the root node.
Selectimgpath, is the activation of the icon, you can be empty, empty when the choice of the item icon unchanged.
Then there is the Expandlist class, which has the following data structure:
/*显示在屏幕上的Label队列*/ The general idea is as follows:
When initializing the list, the parameter is a vector, which can be either expanditem or vector. Then initialize the screen according to the parameters inside the Expanditem, if the group node's ifselected state is true recursionAdd the following child nodes, otherwise insert only the current node. The icon is the same, if ifselected is true, use the Activate icon otherwise the default icon.
After the user selects a node, get the current active index number, determine whether the parent node, if so, first update the parent node's Ifselected property is true, and then redraw the list; (in fact, the more efficient method is to directly insert the child node of this parent node, But doing so would be a little bit more difficult to remove. I have time to change over, hehe. If you select a child node, then determine if there is an activation icon, and if so, update the icon.
Here is the effect
A copy of the code, which is a very early version of my Me component library. Other components are written later. In fact, the best way is to write 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; } /** * Default picture */ Private String imagepath= ""; /* * Activate picture, if empty to indicate that this picture is invalid */ Private String Selectimgpath=null; /** * Group */ public static int group=1; /** * Record */ public static int item=0; /** * is selected */ Private Boolean ifselected=false; /** * Display Label */ Private String label; /** * Type: Group, record */ private int type; /** * Stored objects */ 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 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);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.