如何在BB RIM中进行即时更新?

时间:2012-07-09 09:45:41

标签: blackberry

我想手动显示一个字段。

public class Main_NewsDetail extends MainScreen {
private Custom_FontField slider;
private boolean a;

public Main_NewsDetail() {
    super(USE_ALL_WIDTH);

    slider = new Custom_FontField(
            Bitmap.getBitmapResource("slider_thumb_normal.png"),
            Bitmap.getBitmapResource("slider_progress_normal.png"),
            Bitmap.getBitmapResource("slider_base_normal.png"),
            Bitmap.getBitmapResource("slider_thumb_focused.png"),
            Bitmap.getBitmapResource("slider_progress_focused.png"),
            Bitmap.getBitmapResource("slider_base_focused.png"),
            Bitmap.getBitmapResource("slider_thumb_pressed.png"),
            Bitmap.getBitmapResource("slider_progress_pressed.png"),
            Bitmap.getBitmapResource("slider_base_pressed.png"), 35, 10, 5,
            5, FOCUSABLE);
    if(a)
    add(slider);


}

public class Custom_NewsDetailBottom extends Manager implements
        FieldChangeListener {


    Custom_NewsDetailBottom() {
        super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL
                | Manager.NO_HORIZONTAL_SCROLL);
        Background background = BackgroundFactory
                .createBitmapBackground(bg);
        setBackground(background);
        fontbtn = new Custom_ButtonField(font, fontactive, fontactive) {
            protected boolean navigationClick(int status, int time) {
                a = !a;   <-- here is to control field display
                return true;
            }
        };
        fontbtn.setChangeListener(this);
        add(fontbtn);


    }

    protected void sublayout(int width, int height) {
        Field field = getField(0);
        layoutChild(field, font.getWidth(), font.getHeight());
        setPositionChild(field, getGap(), 5);



        width = Math.min(width, getPreferredWidth());
        height = Math.min(height, getPreferredHeight());
        setExtent(width, height);
    }

    public int getPreferredHeight() {
        return 70;
    }

    public int getPreferredWidth() {
        return Display.getWidth();
    }

    protected void paint(Graphics graphics) {
        int rectHeight = getPreferredHeight();
        int rectWidth = getPreferredWidth();
        graphics.drawRect(0, 0, rectWidth, rectHeight);
        super.paint(graphics);
    }

    private int getGap() {
        return ((getPreferredWidth() / 4) - font.getWidth()) / 2;
    }

    public void fieldChanged(Field field, int context) {
        if (field == sharebtn) {

        } else if (field == commentbtn) {
            Main.getUiApplication().pushScreen(new Main_Comments());
        } else if (field == otherbtn) {
            Main.getUiApplication().pushScreen(new Menu_Others());
        }
    }

    public boolean keyDown(int keycode, int status) {
        if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
            delete(slider);
            return true;
        }
        return super.keyDown(keycode, status);
    }
}
}

以上是显示屏幕的代码。在fontbtn中,单击时将更改变量true / false。但是,它无法立即更新以显示字段slider

slider类似Android中的搜索栏。在android中,当点击时可以setvisibility而不是黑莓RIM,那么如何控制呢?

2 个答案:

答案 0 :(得分:0)

尝试调用Manager的invalidate - 方法强制重新绘制托管区域:

Marks this entire manager as requiring repainting.
Invoke this method to signal that this manager's entire region requires repainting. 

答案 1 :(得分:0)

首先,如果您可以避免使用,我建议不要使用名称如此的变量:

private boolean a;

尝试给它一个更具描述性的名称,因为这有助于我们更好地理解您的代码。

接下来,在a构造函数中,您似乎正在测试变量Main_NewsDetail,然后才有机会进行更改。这样就行不通了。也许在Main_NewsDetail课程中尝试这个:

/** separate boolean used because Field.isVisible() doesn't seem totally robust */
private boolean isSliderVisible = false;

private void setSliderVisible(boolean isVisible) {
   if (isVisible != isSliderVisible) {
      if (isVisible) {
          add(slider);
      } else {
          delete(slider);
          // but, we still retain the "slider" member variable, so it can be
          // added again later
      }
      isSliderVisible = isVisible;

      // I'm not actually sure that this is needed.  I include it because I can't run this code right now!
      invalidate();
   }  
}

然后,在按钮单击处理程序中:

fontbtn = new Custom_ButtonField(font, fontactive, fontactive) {
     protected boolean navigationClick(int status, int time) {
         setSliderVisible(!isSliderVisible);
         return true;
     }
};

现在,此代码可以正常运行,因为您的Main_NewsDetail只有一个Field,即滑块。如果实际上有多个Field对象(您可能会有),那么您可能需要更复杂的逻辑。您可能希望每次都在同一位置显示滑块。为此,您可以在所有index字段的列表中记录滑块的Main_NewsDetail(例如,滑块是第1个字段,第2个,第5个?)。然后,您可以执行以下操作:

,而不是调用add(slider)
private void setSliderVisible(boolean isVisible) {
   if (isVisible != isSliderVisible) {
      if (isVisible) {
         insert(slider, sliderIndex);

您可能需要将Main_NewsDetail作为Manager的子类并实现sublayout()方法。这样,您可以确保如果isSliderVisible,您始终将滑块布置在相同的位置。