Parent & Child Node with different images & Clickable Event - Treeview Blackberry -
i using tree view in app show client server data in blackberry. same thing achieved in android app using expandable listview items. here i'm facing 2 issues
one is:
i want add parent node icon folder icon & child node must have different icon. example if parent item images child nodes must have images icon, if parent item video child have video icons.
second:
when click on child node (like image child node), node opens in new screen & shows clickable item whether click on image or video.
here code used 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); } }
and
public class filesmanager extends mainscreen { public filesmanager() { // set 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); } }
i attached screenshot made in android. 1 give me guideline achieve thing in bb?
you off start.
to right icons, need detect tree "nodes" folders, movies, songs, images, etc. either treefield#getfirstchild()
or checking cookie/text, each node, inside drawtreeitem()
.
to handle clicks on movie, image, or song rows, override navigationclick()
.
for example, starting the treefielddemo 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 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 on leaf node. default action or else fall through. // note: detect empty folders, might or // might not app has 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); // assumes filenames contain '.' character! if (text.indexof(".") > 0) { // leaf node, 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); } } }
my navigation click handler coded accept clicks on entire movie or song row, not "play" button itself. think easier on touch devices, since user's finger doesn't have hit small touch target. can change if like, though.
results
note: didn't bother hook icon image files ... should able now.
Comments
Post a Comment