2012-04-03 77 views
4

我想自定義顯示在樹視圖 左側出現的(+)號是否有可能 我希望把圖像在那個地方定製黑莓Treefield

我曾嘗試定製和搜索論壇也黑莓論壇,其中一人表示,其不可能的,但隨後
我有一個鏈接,這是低於說,這是可能的

http://supportforums.blackberry.com/t5/Java-Development/Custom-TreeField/td-p/354901

其中HardwareDevice類似乎想着g

任何人都可以使用鏈接或任何其他答案解釋這個概念 請建議?

回答

7

[修訂]如果你想CUSTOMIZE的( +) - 每行的符號和其他屬性TreeField而不是寫你自己的樹領域那麼你可以嘗試重新編寫的TreeFieldCallbackdrawTreeItem方法如下所示:

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.system.Display; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.component.TreeField; 
import net.rim.device.api.ui.component.TreeFieldCallback; 
import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.ui.container.VerticalFieldManager; 

public class TreeDemo extends MainScreen { 
    int parent[] = {1,2,3,4,5,6,7,8,9}; 
    int child[][] = new int [10][10]; 
    int child_child[][][] = new int [10][10][10]; 

    int rowHeight = 27; 

    CustomTreeFieldCallback treeCallback = new CustomTreeFieldCallback(); 
    VerticalFieldManager vm = new VerticalFieldManager(Field.FOCUSABLE | VERTICAL_SCROLL | VERTICAL_SCROLLBAR); 
    TreeField myTree = new TreeField(treeCallback, Field.FOCUSABLE); 

    public TreeDemo() { 
     vm.add(new LabelField("Table:")); 
     myTree.setRowHeight(rowHeight); 
     myTree.setIndentWidth(15); 
     myTree.setDefaultExpanded(false); 
     for(int i = parent.length-1; i >= 0 ; i--) { 
      parent[i] = myTree.addChildNode(0, "Parent_" + (i+1)); 
      child[i] = new int[4]; 
      for(int j = child[i].length-1; j >=0 ; j--) { 
       child[i][j] = myTree.addChildNode(parent[i], "Child_"+ (i+1) + "_" + (j+1)); 
       child_child[i][j] = new int[3]; 
       for(int k = child_child[i][j].length-1; k >= 0 ; k--) { 
        child_child[i][j][k] = myTree.addChildNode(child[i][j], "Child_of_Child_"+ (i+1) + "_" + (j+1)+ "_" + (k+1)); 
       } 
      } 
     } 
     vm.add(myTree); 
     add(vm); 
    } 

    private class CustomTreeFieldCallback implements TreeFieldCallback { 

     public void drawTreeItem(TreeField treeField, Graphics graphics, int node, 
       int y, int width, int indent) { 
      // TODO Auto-generated method stub 
      String string = (String) treeField.getCookie(node); 
      int preservedColor = graphics.getColor(); 

      if(treeField.getCurrentNode() == node) { 
       graphics.setColor(0x0CCCC0); 
      } else { 
       graphics.setColor(0x404040); 
      } 
      graphics.fillRect(0, y, Display.getWidth(), treeField.getRowHeight()); 

      Bitmap iconImage; 
      int iconImageWidth = 0; 
      indent -= 20; // decrease the extra indentation for all nodes. 
      if(treeField.getFirstChild(node) != -1){ // if the node is not a leaf node 
       if(treeField.getExpanded(node)) { 
        iconImage = Bitmap.getBitmapResource("icon_arrow_down.png"); 
        iconImageWidth = iconImage.getWidth(); 
       } else { 
        iconImage = Bitmap.getBitmapResource("icon_arrow_right.png"); 
        iconImageWidth = iconImage.getWidth(); 
       } 
       graphics.drawBitmap(indent, y, indent+iconImageWidth, treeField.getRowHeight(), iconImage, 0, 0); 
      } 

      if(treeField.getCurrentNode() == node) { 
       graphics.setColor(0x404040);    
      } else { 
       graphics.setColor(0x0CCCC0); 
      } 
      graphics.drawText(string, indent+iconImageWidth, y); 

      graphics.setColor(preservedColor); 
     } 

    } 
} 

Customizing TreeField in BlackBerry

+0

好!!!! ..酷代碼@HeartBeat:但是,如果有子節點裏面的子節點會被重寫嗎? – Yatin 2012-04-18 10:01:46

+1

@Yatin K,前面的代碼也會支持「子節點內的子節點」,但是我之前沒有考慮到縮進問題。現在我已經更新了代碼並修復了這個問題,請檢查它! – 2012-04-24 09:25:00

+0

嗨,請回答下面的問題它與這個問題有關。 http://stackoverflow.com/questions/17723328/parent-node-or-child-node-in-blackberry-tree-field – Yatin 2013-07-18 12:26:31

5

試試這個下面的代碼敵人與圖像定製treeField

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.system.Display; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 
import net.rim.device.api.ui.Manager; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.component.SeparatorField; 
import net.rim.device.api.ui.container.HorizontalFieldManager; 
import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.ui.container.VerticalFieldManager; 

/** 
* A class extending the MainScreen class, which provides default standard 
* behavior for BlackBerry GUI applications. 
*/ 
public final class MyScreen extends MainScreen implements FieldChangeListener 
{ 
    /** 
    * Creates a new MyScreen object 
    */ 
    private VerticalFieldManager main_manager; 
    private HorizontalFieldManager parentNodes; 
    private LabelField parent_lables[]; 
    private Bitmap bitmap,upbitmap; 
    private BitmapField bitmap_field[]; 
    private VerticalFieldManager submanager[]; 
    private int sizeOfParentNodes=3; 
    private int sizeOfChildNodes=5; 
    private static boolean flag[]; 

    public MyScreen() 
    {   
     // Set the displayed title of the screen  
     bitmap=Bitmap.getBitmapResource("arrow.png"); 
     upbitmap=Bitmap.getBitmapResource("uparrow.png"); 
     main_manager=new VerticalFieldManager(Manager.VERTICAL_SCROLL|VERTICAL_SCROLLBAR){ 
      protected void sublayout(int maxWidth, int maxHeight) { 
       super.sublayout(Display.getWidth(), Display.getHeight()); 
       setExtent(Display.getWidth(), Display.getHeight()); 
      }; 
     }; 
     parent_lables=new LabelField[sizeOfParentNodes]; 
     flag=new boolean[sizeOfParentNodes]; 
     submanager=new VerticalFieldManager[sizeOfParentNodes]; 
     bitmap_field=new BitmapField[sizeOfParentNodes]; 
     for(int i=0;i<sizeOfParentNodes;i++) 
     { 
      submanager[i]=new VerticalFieldManager(); 
      updateGUI(i); 
      main_manager.add(submanager[i]); 
     } 

     add(main_manager); 


    } 
    public void fieldChanged(Field field, int context) { 
     // TODO Auto-generated method stub 
     synchronized (UiApplication.getEventLock()) { 
      for(int i=0;i<sizeOfParentNodes;i++) 
      { if(field==parent_lables[i]) 
       { 
        if(flag[i]==true){ 
         flag[i]=false; 
         submanager[i].deleteAll(); 
         updateGUI(i); 
         parent_lables[i].setFocus(); 
        }else{ 
         flag[i]=true; 
         bitmap_field[i].setBitmap(upbitmap); 
         for(int j=0;j<sizeOfChildNodes;j++) 
         { 
          submanager[i].add(new LabelField("  Child :"+i+","+j,Field.FOCUSABLE)); 
          submanager[i].add(new SeparatorField()); 
         } 
         submanager[i].invalidate(); 
        } 

       } 
      } 
     } 


    } 
    public void updateGUI(int index) 
    { 
     parentNodes=new HorizontalFieldManager(USE_ALL_WIDTH); 
     bitmap_field[index]=new BitmapField(bitmap); 

     parentNodes.add(bitmap_field[index]); 
     parent_lables[index]=new LabelField("Day"+index,Field.FOCUSABLE){ 
      protected boolean navigationClick(int status, int time) { 
       fieldChangeNotify(1); 
       return true; 
      }; 
     }; 
     parentNodes.add(parent_lables[index]); 
     parent_lables[index].setChangeListener(this); 
     submanager[index].add(parentNodes); 
    } 
} 

你可以得到輸出

enter image description here