问题
I'm stuck trying to implement a sliding menu in BlackBerry.
The code for the slide works fine, but I don't know what is wrong with the browserfield.
After the first click received by the button, the browserField is deleted from its manager, so I guess the problem starts with the FieldChangeListener code.
This is the full code of my screen:
public final class MyScreen extends MainScreen
{
boolean val=false;
private BrowserField contentField;
public MyScreen()
{
final ButtonField l=new ButtonField("menu");
final HorizontalFieldManager hfm_main=new HorizontalFieldManager();
final VerticalFieldManager vfm_l=new VerticalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(280, maxHeight);
setExtent(280, maxHeight);
}
protected void paint(Graphics g){
g.setBackgroundColor(Color.RED);
g.clear();
super.paint(g);
}
};
final VerticalFieldManager vfm_r=new VerticalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth+300, maxHeight);
setExtent(maxWidth, maxHeight);
}
protected void paint(Graphics g){
g.setBackgroundColor(Color.YELLOW);
g.clear();
super.paint(g);
}
};
vfm_l.add(new LabelField("sliding pannel"));
vfm_r.add(l);
BrowserField bf = new BrowserField();
vfm_r.add(bf);
bf.requestContent("http://www.google.com");
hfm_main.add(vfm_r);
add(hfm_main);
FieldChangeListener listener=new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(field==l){
if(!val){
val=true;
hfm_main.deleteAll();
hfm_main.add(vfm_l);
hfm_main.add(vfm_r);
}else{
val=false;
hfm_main.deleteAll();
hfm_main.add(vfm_r);
}
}
}
};
l.setChangeListener(listener);
}
}
Why is the browserField disappearing from the screen?
Thanks in advance.
回答1:
I don't like or support this code. If you want to have menus come sliding in from the left, I would use Screen transitions, with a 'transparent' screen on the right and menus on the left. Alternatively, since there is actually no sliding involved in this processing, you could display a popup screen on the left hand side.
But this code seems to be making the BB try to look like some other phone. Why not use the supplied BB menu processing that the users are familiar with?
Finally, I suspect this code will be a nightmare to use on a trackpad only device.
Below is not nice code, but at least it works better than your current code. Please review and note the changes and implement as you see fit.
Note that this is dubious:
super.sublayout(maxWidth+300, maxHeight); // A
setExtent(maxWidth, maxHeight); // B
What you are saying in each line is as follows
A - you can layout this Field to take 300 more pixels than I can actually give to this Field, so you can make it 300 pixels wider than I can actually display.
B - despite the space that the layout takes, I will use the maximum I am allowed to for this Field.
Anyway, here is my code, it seems to work, use it as you see fit, but I don't support it and would never use anything like this in a production program....
public final class MyScreen extends MainScreen
{
boolean val=false;
private BrowserField contentField;
public MyScreen() {
final ButtonField l=new ButtonField("menu");
final HorizontalFieldManager hfm_main=new HorizontalFieldManager();
final VerticalFieldManager vfm_l=new VerticalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(280, maxHeight);
setExtent(280, maxHeight);
}
protected void paint(Graphics g){
g.setBackgroundColor(Color.RED);
g.clear();
super.paint(g);
}
};
final VerticalFieldManager vfm_r=new VerticalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight) {
int ourRequiredWidth = Display.getWidth();
super.sublayout(ourRequiredWidth, maxHeight);
setExtent(ourRequiredWidth, maxHeight);
}
protected void paint(Graphics g){
g.setBackgroundColor(Color.YELLOW);
g.clear();
super.paint(g);
}
};
vfm_l.add(new LabelField("sliding pannel"));
vfm_r.add(l);
BrowserField bf = new BrowserField();
vfm_r.add(bf);
bf.requestContent("http://www.google.com");
hfm_main.add(vfm_r);
add(hfm_main);
FieldChangeListener listener=new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(field==l){
if(vfm_l.getManager() == null){
hfm_main.insert(vfm_l, 0);
} else {
hfm_main.delete(vfm_l);
}
}
}
};
l.setChangeListener(listener);
}
}
来源:https://stackoverflow.com/questions/22559007/blackberry-why-is-this-browserfield-disappearing-from-the-screen-after-this-ch