2

我在应用程序中使用树视图在黑莓中显示客户端服务器数据。我通过使用可扩展的列表视图项目在 android 应用程序中实现了相同的目标。但在这里我面临两个问题

一种是:

我想添加父节点图标,如文件夹图标和子节点必须有不同的图标。例如,如果父项是图像,则子节点必须具有图像图标,如果父项是视频,则子节点必须具有视频图标。

第二:

当我单击任何子节点(如图像子节点)时,此节点会在新屏幕中打开并显示可单击项,无论我单击图像还是视频。

这是我用来获取所需结果的代码:

class CustomTreeFieldCallback implements TreeFieldCallback {
    public void drawTreeItem(TreeField _tree, Graphics g, int node, int y,
            int width, int indent) {
        // FontFamily
        FontFamily fontFamily[] = FontFamily.getFontFamilies();
        Font font = fontFamily[1].getFont(FontFamily.CBTF_FONT, 18);
        g.setFont(font);
        String text = (String) _tree.getCookie(node);
        Bitmap b = Bitmap.getBitmapResource("images.png");
        g.drawText(text, indent + b.getWidth(), y);
        g.drawBitmap(indent, y - 15, b.getWidth(), b.getHeight(), b, 0, 0);
    }
}

public class FilesManager extends MainScreen {

    public FilesManager() {

        // Set the linear background.
        Bitmap background = Bitmap.getBitmapResource("background.png");
        Background bg = BackgroundFactory.createBitmapBackground(background);
        this.getMainManager().setBackground(bg);

        String parentNode = new String("Images");
        String firstChild = new String("first child");
        String secondChild = new String("second child");
        String thirdChild = new String("third child");

        CustomTreeFieldCallback myCallback = new CustomTreeFieldCallback();
         myTree = new TreeField(myCallback, Field.FOCUSABLE); 

        int node2 = myTree.addChildNode(0, parentNode);
        myTree.addChildNode(node2, firstChild);
        myTree.addChildNode(node2, secondChild);
        myTree.addChildNode(node2, thirdChild);
        add(myTree);

    }

}

我还附上了我在 android 中制作的屏幕截图。有人给我指导在BB中实现这件事吗?

在此处输入图像描述

4

1 回答 1

3

你有一个好的开始。

要获得正确的图标,您只需要检测哪些树“节点”是文件夹、电影、歌曲、图像等。使用TreeField#getFirstChild()或通过检查每个节点的 cookie/文本来做到这一点drawTreeItem()

要处理对电影、图像或歌曲行的点击,请覆盖navigationClick().

例如,从 BlackBerry 的 TreeFieldDemo开始:

class TreeFieldDemoScreen extends MainScreen
{

   private final Bitmap openIcon = Bitmap.getBitmapResource("folder-open.png");
   private final Bitmap closedIcon = Bitmap.getBitmapResource("folder-closed.png");
   private final Bitmap movieIcon = Bitmap.getBitmapResource("movie.png");
   private final Bitmap songIcon = Bitmap.getBitmapResource("song.png");
   private final Bitmap playIcon = Bitmap.getBitmapResource("play.png");

   public TreeFieldDemoScreen()
   {             
      setTitle("Tree Field Demo");

      TreeCallback myCallback = new TreeCallback();
      TreeField myTree = new TreeField(myCallback, Field.FOCUSABLE) {
         protected boolean navigationClick(int status, int time) {
            // We'll only override unvarnished navigation click behavior
            if ((status & KeypadListener.STATUS_ALT) == 0 &&
                  (status & KeypadListener.STATUS_SHIFT) == 0)
            {
               final int node = getCurrentNode();
               if (getFirstChild(node) == -1) {               
                  // Click is on a leaf node. Do some default action or else fall through.                  

                  // Note:  this will also detect empty folders, which might or 
                  //  might not be something your app has to handle
                  Dialog.alert("clicked " + getCookie(node));
                  // TODO: open player screen, etc.   
                  return true;              
               }
            }
            return super.navigationClick(status, time);
         }
      };

      myTree.setDefaultExpanded(false);
      myTree.setRowHeight(openIcon.getHeight());

      String nodeOne = new String("Video");  // folder
      String nodeTwo = new String("Music");  // folder
      String nodeThree = new String("Images"); // folder
      String nodeFour = new String("song.mp3");
      String nodeFive = new String("movie.m4v");

      int node1 = myTree.addChildNode(0, nodeOne);
      int node2 = myTree.addChildNode(0, nodeTwo);
      int node3 = myTree.addChildNode(0, nodeThree);
      int node4 = myTree.addChildNode(node2, nodeFour);
      int node5 = myTree.addChildNode(node1, nodeFive);

      add(myTree);
   }


   private class TreeCallback implements TreeFieldCallback 
   {
      public void drawTreeItem(TreeField _tree, Graphics g, int node, int y, int width, int indent) 
      {
         final int PAD = 8;
         String text = (String)_tree.getCookie(node);
         Bitmap icon = closedIcon;
         if (text.endsWith(".mp3")) {
            icon = songIcon;
         } else if (text.endsWith(".m4v")) {
            icon = movieIcon;
         } else if (_tree.getExpanded(node)) {
            icon = openIcon;
         }
         g.drawBitmap(indent, y, icon.getWidth(), icon.getHeight(), icon, 0, 0);
         // This assumes filenames all contain '.' character!
         if (text.indexOf(".") > 0) {
            // Leaf node, so this is a playable item (movie or song)
            g.drawBitmap(_tree.getWidth() - playIcon.getWidth() - PAD, y + PAD, 
                  playIcon.getWidth(), playIcon.getHeight(), playIcon, 0, 0);
         }
         int fontHeight = getFont().getHeight();
         g.drawText(text, indent + icon.getWidth() + PAD, y + (_tree.getRowHeight() - fontHeight)/2);
      }
   }
}

我的导航点击处理程序被编码为接受对整个电影或歌曲行的点击,而不仅仅是“播放”按钮本身。我认为这在触摸设备上更容易,因为用户的手指不必碰到小的触摸目标。不过,您可以根据需要更改此设置。

结果

在此处输入图像描述

注意:我没有费心为图像文件连接图标......你现在应该可以做到了。

于 2013-08-30T02:55:03.250 回答