tab与list配合使用会使效果增添不少,可是在之前没有接触过此框架的要做到很好却是有点小难度,以下就按我在开发过程中遇到的问题,一一罗列出来,并附上解决的方法。
如图:
问题一:首先我从登陆窗体进入时,我不希望每个Tab选项卡中的List中都去服务器取数据,这样会导致加载时间过长导致用户等待过久,因为Tab是在窗体初始化的时候就要把List中的数据添加进去,如果要分别加载数据则在tab事件中必须重新加载之前的数据。
解决方法:此问题的关键是Container的使用,我们可以为每个选项卡添加一个Container,然后触发tab事件时我们改变的只是Container中的数据及控件,所以这样最完美了解决了存在的问题,创建list的代码就不重复了,需要请看这里。
Code
// 个人邮件
String[][] mailPersonal = GetMailPersonal();
MailInfo[] mailInfoPersonal = GetMainInfoArrary(mailPersonal, true);
containerPersonal = new Container();
containerPersonal.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
if (mailInfoPersonal != null)
containerPersonal.addComponent(createList(mailInfoPersonal,
List.VERTICAL, new ContactsRenderer()));
else {
Label label = new Label("您的个人邮件没有数据!");
containerPersonal.getStyle().setMargin(Component.TOP, 60);
containerPersonal.setLayout(new FlowLayout(Component.CENTER));
containerPersonal.addComponent(label);
}
tab.addTab("个人邮件", getRes().getImage("SJX.gif"), containerPersonal);
// 群发邮件,未加载数据赋空数值
String[][] mailGroup = { { "正在加载", "", "", "", "", "", "" } };
MailInfo[] mailInfoGroup = GetMainInfoArrary(mailGroup, false);
final Container ContainerGroup = new Container();
ContainerGroup.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
ContainerGroup.addComponent(createList(mailInfoGroup, List.VERTICAL,
new ContactsRenderer()));
tab.addTab("群发邮件", getRes().getImage("SJX.gif"), ContainerGroup);
// 保留邮件,未加载数据赋空数值
String[][] mailSave = { { "正在加载", "", "", "", "", "", "" } };
MailInfo[] mailInfoSave = GetMainInfoArrary(mailSave, true);
final Container ContainerSave = new Container();
ContainerSave.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
ContainerSave.addComponent(createList(mailInfoSave, List.VERTICAL,
new ContactsRenderer()));
tab.addTab("保留邮件", getRes().getImage("SJX.gif"), ContainerSave);
// 发件箱,未加载数据赋空数值
String[][] mailSend = { { "正在加载", "", "", "", "", "", "" } };
MailInfo[] mailInfoSend = GetMainInfoArrary(mailSend, true);
final Container ContainerSend = new Container();
ContainerSend.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
ContainerSend.addComponent(createList(mailInfoSend, List.VERTICAL,
new ContactsRenderer()));
tab.addTab("发件箱", getRes().getImage("SJX.gif"), ContainerSend);
tab.setTabPlacement(TabbedPane.TOP);
addComponent(BorderLayout.CENTER, tab);
// tab事件,触发时请求数据并向Container添加已经填充好数据的list
tab.addTabsListener(new SelectionListener() {
public void selectionChanged(int oldSelected, int newSelected) {
int indexvalue = tab.getSelectedIndex();
if (indexvalue == 1 && flagGroup == 0) {
Global.GetWaitCursor("正在转到群发邮件,请稍候", FormFactory
.getInstance().getMailForm());
new Thread() {
public void run() {
String[][] mailGroup = GetMailGroup();
flagGroup = 1;
LoadDataAgain(ContainerGroup, "您的群发邮件没有数据!",
mailGroup, false);
FormFactory.getInstance().getMailForm().show();
}
}.start();
}
if (indexvalue == 2 && flagSave == 0) {
Global.GetWaitCursor("正在转到保留邮件,请稍候", FormFactory
.getInstance().getMailForm());
new Thread() {
public void run() {
String[][] mailSave = GetMailSave();
flagSave = 1;
LoadDataAgain(ContainerSave, "您的保留邮件没有任何数据!",
mailSave, true);
FormFactory.getInstance().getMailForm().show();
}
}.start();
}
if (indexvalue == 3 && flagSend == 0) {
Global.GetWaitCursor("正在转到发件箱,请稍候", FormFactory
.getInstance().getMailForm());
new Thread() {
public void run() {
String[][] mailSend = GetMailSend();
flagSend = 1;
LoadDataAgain(ContainerSend, "您的发件箱没有任何数据!",
mailSend, true);
FormFactory.getInstance().getMailForm().show();
}
}.start();
}
}
});
//将指定的Container添加上List
private void LoadDataAgain(Container containner, String Labelstring,
String[][] mail, boolean bolvalue) {
MailInfo[] mailInfo = GetMainInfoArrary(mail, bolvalue);
containner.removeAll();
if (mailInfo != null)
containner.addComponent(createList(mailInfo, List.VERTICAL,
new ContactsRenderer()));
else {
Label label = new Label(Labelstring);
containner.getStyle().setMargin(Component.TOP, 60);
containner.setLayout(new FlowLayout(Component.CENTER));
containner.addComponent(label);
}
}
// 个人邮件
String[][] mailPersonal = GetMailPersonal();
MailInfo[] mailInfoPersonal = GetMainInfoArrary(mailPersonal, true);
containerPersonal = new Container();
containerPersonal.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
if (mailInfoPersonal != null)
containerPersonal.addComponent(createList(mailInfoPersonal,
List.VERTICAL, new ContactsRenderer()));
else {
Label label = new Label("您的个人邮件没有数据!");
containerPersonal.getStyle().setMargin(Component.TOP, 60);
containerPersonal.setLayout(new FlowLayout(Component.CENTER));
containerPersonal.addComponent(label);
}
tab.addTab("个人邮件", getRes().getImage("SJX.gif"), containerPersonal);
// 群发邮件,未加载数据赋空数值
String[][] mailGroup = { { "正在加载", "", "", "", "", "", "" } };
MailInfo[] mailInfoGroup = GetMainInfoArrary(mailGroup, false);
final Container ContainerGroup = new Container();
ContainerGroup.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
ContainerGroup.addComponent(createList(mailInfoGroup, List.VERTICAL,
new ContactsRenderer()));
tab.addTab("群发邮件", getRes().getImage("SJX.gif"), ContainerGroup);
// 保留邮件,未加载数据赋空数值
String[][] mailSave = { { "正在加载", "", "", "", "", "", "" } };
MailInfo[] mailInfoSave = GetMainInfoArrary(mailSave, true);
final Container ContainerSave = new Container();
ContainerSave.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
ContainerSave.addComponent(createList(mailInfoSave, List.VERTICAL,
new ContactsRenderer()));
tab.addTab("保留邮件", getRes().getImage("SJX.gif"), ContainerSave);
// 发件箱,未加载数据赋空数值
String[][] mailSend = { { "正在加载", "", "", "", "", "", "" } };
MailInfo[] mailInfoSend = GetMainInfoArrary(mailSend, true);
final Container ContainerSend = new Container();
ContainerSend.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
ContainerSend.addComponent(createList(mailInfoSend, List.VERTICAL,
new ContactsRenderer()));
tab.addTab("发件箱", getRes().getImage("SJX.gif"), ContainerSend);
tab.setTabPlacement(TabbedPane.TOP);
addComponent(BorderLayout.CENTER, tab);
// tab事件,触发时请求数据并向Container添加已经填充好数据的list
tab.addTabsListener(new SelectionListener() {
public void selectionChanged(int oldSelected, int newSelected) {
int indexvalue = tab.getSelectedIndex();
if (indexvalue == 1 && flagGroup == 0) {
Global.GetWaitCursor("正在转到群发邮件,请稍候", FormFactory
.getInstance().getMailForm());
new Thread() {
public void run() {
String[][] mailGroup = GetMailGroup();
flagGroup = 1;
LoadDataAgain(ContainerGroup, "您的群发邮件没有数据!",
mailGroup, false);
FormFactory.getInstance().getMailForm().show();
}
}.start();
}
if (indexvalue == 2 && flagSave == 0) {
Global.GetWaitCursor("正在转到保留邮件,请稍候", FormFactory
.getInstance().getMailForm());
new Thread() {
public void run() {
String[][] mailSave = GetMailSave();
flagSave = 1;
LoadDataAgain(ContainerSave, "您的保留邮件没有任何数据!",
mailSave, true);
FormFactory.getInstance().getMailForm().show();
}
}.start();
}
if (indexvalue == 3 && flagSend == 0) {
Global.GetWaitCursor("正在转到发件箱,请稍候", FormFactory
.getInstance().getMailForm());
new Thread() {
public void run() {
String[][] mailSend = GetMailSend();
flagSend = 1;
LoadDataAgain(ContainerSend, "您的发件箱没有任何数据!",
mailSend, true);
FormFactory.getInstance().getMailForm().show();
}
}.start();
}
}
});
//将指定的Container添加上List
private void LoadDataAgain(Container containner, String Labelstring,
String[][] mail, boolean bolvalue) {
MailInfo[] mailInfo = GetMainInfoArrary(mail, bolvalue);
containner.removeAll();
if (mailInfo != null)
containner.addComponent(createList(mailInfo, List.VERTICAL,
new ContactsRenderer()));
else {
Label label = new Label(Labelstring);
containner.getStyle().setMargin(Component.TOP, 60);
containner.setLayout(new FlowLayout(Component.CENTER));
containner.addComponent(label);
}
}
问题二:获取选中的List列中的数据,这是普遍会遇到的难题,如果你单单是用手机中间键触发的话,或许比较简单,但是如果在菜单按钮上也要增加此获取list列表的数据,就会比较麻烦,不过麻烦都过去了。
解决办法:通过list的获取焦点失去焦点还有选择焦点,设置全局变量来获取list中绑定的属性值。
Code
// list列表选择焦点监听事件
list.addSelectionListener(new SelectionListener() {
public void selectionChanged(int oldSelected, int newSelected) {
globalStrMailGuid = MainInfos[newSelected].fid;
}
});
list.addFocusListener(new FocusListener() {
// list获得焦点
public void focusGained(Component cmp) {
globalStrMailGuid = MainInfos[list.getSelectedIndex()].fid;
}
// list失去焦点
public void focusLost(Component cmp) {
globalStrMailGuid = "";
}
});
list.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
//触发列表中的列的时候你所要做的事情。。。
}
});
// list列表选择焦点监听事件
list.addSelectionListener(new SelectionListener() {
public void selectionChanged(int oldSelected, int newSelected) {
globalStrMailGuid = MainInfos[newSelected].fid;
}
});
list.addFocusListener(new FocusListener() {
// list获得焦点
public void focusGained(Component cmp) {
globalStrMailGuid = MainInfos[list.getSelectedIndex()].fid;
}
// list失去焦点
public void focusLost(Component cmp) {
globalStrMailGuid = "";
}
});
list.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
//触发列表中的列的时候你所要做的事情。。。
}
});
问题三:选中状态的调整,如果没有调整的话,你会发现list放到Tab后,选中状态跟绑定的数据会慢了一拍,也就是你选在第二条而数据却是第一条的。
解决办法:设置一全局变量,getListFocusComponent做下更改即可,代码如下。
Code
//创建list中的getListFocusComponent中添加此if语句。
public Component getListFocusComponent(final List list) {
if (globalselect == 1)
return focus;
else
return null;
}
//也就是list获得焦点的时候才返回focus选中状态,否则返回null
list.addFocusListener(new FocusListener() {
// list获得焦点
public void focusGained(Component cmp) {
globalselect = 1;
}
// list失去焦点
public void focusLost(Component cmp) {
globalselect = 0;
}
});
//创建list中的getListFocusComponent中添加此if语句。
public Component getListFocusComponent(final List list) {
if (globalselect == 1)
return focus;
else
return null;
}
//也就是list获得焦点的时候才返回focus选中状态,否则返回null
list.addFocusListener(new FocusListener() {
// list获得焦点
public void focusGained(Component cmp) {
globalselect = 1;
}
// list失去焦点
public void focusLost(Component cmp) {
globalselect = 0;
}
});
这三点是比较大的问题所在,其他的还有一些可能会比较好解决就不列出来了。