2012-07-13 16 views
1

我一直在努力在Blackberry上動態創建多個VerticalFieldManager。每個子管理器將向用戶顯示不同的數據。每個子管理器在屏幕上都有不同的位置。所以我創建了一個具有「mainManager」的類和創建「子管理員」的另一個類,然後我調用mainManager.add(new TheClassExtendingVerticalFieldManager);將subManagers添加到mainManager。問題是我只有一個子管理器而不是三個。我正在使用填充來分隔管理員。這是我使用的代碼。請指導我正確的方向,這將不勝感激。如何動態創建多個VerticalFieldManager黑莓

創建該submanager

public class ProgramListView extends VerticalFieldManager{ 

    private VerticalFieldManager subManager; 
    private int _height; 


    public ProgramListView(int height){ 
     this._height = height; 

//  subManager = new VerticalFieldManager(
//    Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR | 
//    Manager.NO_HORIZONTAL_SCROLLBAR | Manager.USE_ALL_WIDTH) 
// 
//  { 
// 
// 
//  }; 
    } 

    public int get_height() { 
     return _height; 
    } 

    public void set_height(int _height) { 
     this._height = _height; 
    } 

    public void setCoordinates(int x, int y){ 
     setPosition(100,140); 
    } 
    protected void sublayout(int maxWidth, int maxHeight) 
    { 
     int displayWidth = Display.getWidth(); 
     int displayHeight = maxHeight; 
     this.setPosition(300, 300); 
     super.sublayout(40, 40); 
     setPadding(this.get_height(), 0, 0, 0); 
     setExtent(displayWidth, this.get_height()); 

    } 

    public void paint(Graphics graphics) 
    { 
     graphics.setBackgroundColor(Color.BLUE);//blue 
     graphics.clear(); 
     super.paint(graphics);  
    } 
    public int getPreferredWidth() { 
     // TODO Auto-generated method stub 
     return Display.getWidth(); 
    } 

} 

的mainManager類

public class ProgramLayout extends MainScreen { 

    private HorizontalFieldManager mainManager; 
    private int deviceWidth = Display.getWidth(); 
    private int deviceHeight = Display.getHeight(); 
    private Vector subManagers; 
    private int theheight; 

    public ProgramLayout(){ 

     setToolbar(); 
     subManagers = new Vector(); 
     theheight = 100; 
     mainManager = new HorizontalFieldManager(Manager.VERTICAL_SCROLL | Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT) 

     { 

      protected void sublayout(int maxWidth, int maxHeight) 
      { 
       int displayWidth = deviceWidth; 
       int displayHeight = deviceHeight; 
       super.sublayout(displayWidth, displayHeight); 
       setExtent(displayWidth, displayHeight); 
      } 

      public void paint(Graphics graphics) 
      { 
       graphics.setBackgroundColor(Color.BLACK); 
       graphics.clear(); 
       super.paint(graphics); 
      } 

      public int getPreferredWidth() { 
       // TODO Auto-generated method stub 
       return Display.getWidth(); 
      } 

     }; 
     for (int i = 0; i < 3; i++) { 
      theheight = theheight+100; 
      subManagers.addElement(new ProgramListView(theheight)); 
     } 

     for (int i = 0; i < subManagers.size(); i++) { 

      mainManager.add((VerticalFieldManager)subManagers.elementAt(i)); 
     } 


     this.add(mainManager); 

    } 

在此先感謝

+2

也許是因爲您使用的是Horizo​​ntalFieldManager,而且子管理器類被編程爲佔用所有屏幕寬度 – 2012-07-13 18:38:40

+1

只是爲了詳細說明Eugen指出的內容,您的'mainManager'是'Horizo​​ntalFieldManager',這意味着它將在水平地「添加()」它們的順序。但是,每個孩子都是'ProgramListView'的一個實例,並在'getPreferredWidth()'中返回'Display.getWidth()'。所以,第一個佔用整個屏幕寬度。你想要這三個'ProgramListView'被垂直堆疊**嗎?然後,'mainManager'應該是'VerticalFieldManager'。 – Nate 2012-07-14 06:52:41

+0

謝謝你們。把主要經理改爲垂直就是個訣竅。 – madcoderz 2012-07-14 09:36:38

回答

0

試試這個類:

for (int i = 0; i < 3; i++) { 
     theheight = theheight+100; 
     mainManager.add(new ProgramListView(theheight)); 
    } 
    this.add(mainManager); 
0

爲了詳細說明Eugen Martynov指出的內容,您的mainManagerHorizontalFieldManager,這意味着它將按照您添加()的順序佈置子字段。它會根據您撥打add()的電話順序自動處理您的佈局。

但是,每個孩子是ProgramListView實例並返回Display.getWidth()在 getPreferredWidth():

public int getPreferredWidth() {   
    // TODO Auto-generated method stub   
    return Display.getWidth();  
} 

所以,第一個佔據整個屏幕寬度,而接下來的兩個會必須是在右邊(但你已經佔用了整個屏幕寬度)。

你想要這三個ProgramListViews垂直堆疊嗎?然後,mainManager應該更改爲VerticalFieldManager。用VerticalFieldManager,撥打add()三個不同的ProgramListView子字段會自動將它們垂直排列。