家长和放大器;子节点用不同的图片及点击事件 - 树视图黑莓 [英] Parent & Child Node with different images & Clickable Event - Treeview Blackberry

查看:114
本文介绍了家长和放大器;子节点用不同的图片及点击事件 - 树视图黑莓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的应用程序树形视图来显示黑莓客户端服务器上的数据。同样的事情在我的Andr​​oid应用,通过使用扩展列表视图的项目来实现的。但在这里我面临两个问题

I am using tree view in app to show the client server data in blackberry. Same thing i achieved in android app by using expandable listview items. But here i'm facing two issues

我要添加父节点图标像一个文件夹图标和放大器;子节点必须有不同的图标。例如,如果父产品的图像则子节点必须有图片的图标,如果父产品视频,然后孩子有视频图标。

I want to add parent node icon like a folder icon & Child node must have different icon. For example if Parent item is images then child nodes must have Images icon, if parent item is video Then child have video icons.

在我的子节点(如图片子节点)点击,这个节点在新画面+打开;显示可点击的项目是否我点击图像或视频。

When i click on any child node (Like image child node), this node opens in new screen & shows the clickable item whether i click on image or video.

下面是我的code,我用得到期望的结果:

Here is my code that i used to Get the desired result:

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这件事?

I also attached the screenShot that i made in android. Any one give me guideline to achieve this thing in BB?

推荐答案

您是一个良好的开端。

要得到正确的图标,你只需要检测哪个树节点是文件夹,电影,歌曲,图片等,做,要么与 TreeField#getFirstChild()或通过检查该cookie /文,对每个节点,在 drawTreeItem()

To get the right icons, you just need to detect which tree "nodes" are folders, movies, songs, images, etc. Do that either with TreeField#getFirstChild() or by checking the cookie/text, for each node, inside drawTreeItem().

要处理的影片,图像或歌曲点击行,<一个href=\"http://supportforums.blackberry.com/t5/Java-Development/TreeField-and-navigationClick-for-expand-collapse/td-p/284795\"相对=nofollow>覆盖 navigationClick()

To handle clicks on the movie, image, or song rows, override navigationClick().

例如,从 rel=\"nofollow\">的TreeFieldDemo:

For example, starting with the TreeFieldDemo from BlackBerry:

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);
      }
   }
}

我的导航点击处理程序是codeD接受对整个电影或歌曲排点击,而不仅仅是播放按钮本身。我认为这是在触摸设备上更容易,因为用户的手指不必打小碰的目标。如果你喜欢,你可以改变这一点,虽然。

My navigation click handler is coded to accept clicks on the entire movie or song row, not just the "play" button itself. I think this is easier on touch devices, since the user's finger doesn't have to hit a small touch target. You can change this if you like, though.

注意:我没有刻意去挂钩图像文件的图标......你应该能够做到这一点,现在

Note: I didn't bother to hook up an icon for image files ... you should be able to do that now.

这篇关于家长和放大器;子节点用不同的图片及点击事件 - 树视图黑莓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆