黑莓垂直区域管理器固定大小:滚动问题

3
我尝试创建一个全屏UI,其中包括一个固定头部(一个带有一些字段的管理器)和可滚动内容(自定义字段列表)。想法是模拟一种可滚动列表。
为此,我制作了一个自定义的VerticalFieldManager,它接受一个maxHeight(屏幕高度-标题高度)。
我遇到了以下问题:
- 滚动箭头从未显示过 - 在OS 4.7(Storm)上,我可以滚动到最后一个项目之下,直到屏幕上只剩下标题。
我的代码需要使用JDE 4.2.1和4.7进行编译,并在Pearl和Storm上运行。 (最坏的情况下,我可以有两个版本的这个类)
我怀疑这两个问题有关。 我可能做错了什么。 我查看了一些示例/论坛,并始终发现类似的解决方案/代码。 你们能告诉我我做错了什么吗?
/**
 *  custom class, so we can set a max height (to keep the header visible)
 */
class myVerticalFieldManager extends VerticalFieldManager{
private int maxHeight = 0;

myVerticalFieldManager(int _maxHeight){
  super(
   //this provoc an "empty scrollable zone" on Storm
   // but if you don't put it, on other OS, the vertical manager does not scroll at all.
   Manager.VERTICAL_SCROLL 

   | Manager.VERTICAL_SCROLLBAR
   ); 
  maxHeight = _maxHeight;
}


protected void sublayout(int width, int height){
        super.sublayout(width, getPreferredHeight());
        setExtent(width, getPreferredHeight());
}

public int getPreferredWidth() {
    return Graphics.getScreenWidth();
}

/**
 * allow the manager to use all the given height. (vs auto Height)
 */
public boolean forceMaxHeight = false;
public int getPreferredHeight() {
  if (forceMaxHeight) return maxHeight;
  int m = super.getPreferredHeight();
  if (m > maxHeight) m = maxHeight;
  return m;   
}    

////////////////////////////////////////////////////////////////////////////

protected boolean isUpArrowShown(){
  //TODO: does not seem to work (4.2.1 emulator & 4.5 device). (called with good return value but the arrows are not painted)
  int i = getFieldWithFocusIndex();
  //Trace("isUpArrowShown " + i);
  return i > 0;
  // note: algo not correct, cause the up arrow will be visible event when no field are hidden.
  //       but not so bad, so the user "know" that he can go up.
}

protected boolean isDownArrowShown(){
  int i = getFieldWithFocusIndex();
  return i < getFieldCount();
}

////////////////////////////////////////////////////////////////////////////
// note : since 4.6 you can use
// http://www.blackberry.com/developers/docs/4.6.0api/net/rim/device/api/ui/decor/Background.html

public int myBackgroundColor = 0xffffff;
protected void paint(Graphics g){
    g.setBackgroundColor(myBackgroundColor);
    // Clears the entire graphic area to the current background
    g.clear();
    super.paint(g);
}


} 

欢迎任何帮助。

3个回答

4

因此,我提出了这个解决方案来解决STORM上的“空白可滚动区域”问题。虽然它很丑陋并且不允许自定义的ScrollChangeListener,但它在Pearl和Storm上运行良好。

implements ScrollChangeListener

 //in constructor:

  setScrollListener(null);
  setScrollListener(this);


private boolean MY_CHANGING_SCROLL = false;
public void scrollChanged(Manager manager, int newHorizontalScroll, int newVerticalScroll){
  if (!MY_CHANGING_SCROLL){
    MY_CHANGING_SCROLL = true;
    myCheckVerticalScroll();
    MY_CHANGING_SCROLL = false;
  }      
}  


protected int myMaxVerticalScrollPosition(){
  int vh = getVirtualHeight();
  int h = getHeight();
  if (vh < h ) return 0; // no scroll
  return vh - h; // don't scroll lower than limit.
}

protected void invCheckVerticalScroll() {
    int i = getVerticalScroll();
    int m = myMaxVerticalScrollPosition();
    if ( i > m){
        i = m;
        setVerticalScroll(i);
    }
}

我仍在寻找滚动箭头问题的解决方案...... 如果有人有想法...


2
我发现一个更简单的方法可以得到相同的结果:
  • 使用NO_SCROLL标志创建屏幕。(我之前不知道这一点)
  • 将标题添加为普通字段;
  • 添加一个包含所有内容的vfm; --> 就会显示箭头,标题也会留在应该呆的地方。此外,您不必担心“setTitle()区域”下面的丑陋阴影。
- Loda

2

您可以使用方法setBanner()替代add来设置您的标题。然后,您可以将默认的VerticalFieldManager添加到屏幕上,它将正常滚动但不会隐藏标题。请注意,MainScreen委托管理器是一个VerticalScrollManager,因此您可能不需要第二个vfm

HorizontalFieldManager hfm = new HorizontalFieldManager();
setBanner(hfm)

add(new ButtonField("Hello 1");
add(new ButtonField("Hello 2");

...


setTitle()的问题在于库在标题区域下方添加了一个非常难看的4-5像素阴影。我承认:我没有尝试setBanner()方法。 - Loda

0

嘿,我用包含图像和标题的HorizontalFieldManager做了同样的事情。

header_img = Bitmap.getBitmapResource("header.png");
             title = new LabelField("Welcome",LabelField.FIELD_RIGHT);
             header_manager =  new HorizontalFieldManager()
             {
                 protected void paint(net.rim.device.api.ui.Graphics graphics)
                    {
                         int y = this.getVerticalScroll();                                      
                         graphics.drawBitmap( 0, y, header_img.getWidth(), header_img.getHeight(), header_img, 0, 0 );
                         graphics.setColor(Color.LEMONCHIFFON);
                         super.paint( graphics );
                    }

                     protected void sublayout(int maxWidth, int maxHeight) 
                     {                      
                        super.sublayout(Display.getWidth(), 240);

                        Field field = title;
                        layoutChild(field, title.getWidth(), title.getHeight());
                        setPositionChild(field, (Display.getWidth()/2) -10, 13);
                        setExtent(Display.getWidth(),55);

                     }
             };
             header_manager.add(title);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接