我在app中使用树视图来显示黑莓中的客户端服务器数据。我通过使用可扩展的listview项目在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中制作的screenShot。有人给我指导在BB中实现这个目标吗?
答案 0 :(得分:3)
你有一个良好的开端。
要获得正确的图标,您只需要检测哪些树“节点”是文件夹,电影,歌曲,图像等。使用TreeField#getFirstChild()
或通过检查cookie /文本,为每个节点执行此操作,在drawTreeItem()
内。
处理电影,图片或歌曲行的点击次数override navigationClick()
。
例如,从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);
}
}
}
我的导航点击处理程序被编码为接受整个电影或歌曲行的点击,而不仅仅是“播放”按钮本身。我认为这在触摸设备上更容易,因为用户的手指不必触及小的触摸目标。不过,如果你愿意,你可以改变它。
注意:我没有费心去连接图片文件的图标......你现在应该可以这样做了。