blackberry - Vertical scrollbar with jump points - setVerticalScroll locking UI -


i have question blackberry verticalscrollfield , scrolling seems lock or make ui unstable. following code blackberry screen worlds content on left (in scroll field) , jumpbar off right allows clicking content.

when jump letter clicked setverticalscroll method called, performs scroll has unfortunate side effect of rendering ui unstable or unusable. scroll call done on ui thread not clear source of error is. app being tested in 6.0 simulator.

i've included class can copied bb eclipse hacking/testing.

the section kicks of scrolling can found towards bottom following code:

 uiapplication.getuiapplication().invokelater(new runnable(){      public void run() {          scroller.setverticalscroll(y, true);  }}); 

here's full class:

 package test;

import java.util.vector;

import net.rim.device.api.system.applicationmanager; import net.rim.device.api.ui.field; import net.rim.device.api.ui.font; import net.rim.device.api.ui.graphics; import net.rim.device.api.ui.touchevent; import net.rim.device.api.ui.uiapplication; import net.rim.device.api.ui.component.labelfield; import net.rim.device.api.ui.component.status; import net.rim.device.api.ui.container.horizontalfieldmanager; import net.rim.device.api.ui.container.mainscreen; import net.rim.device.api.ui.container.verticalfieldmanager;

public class startup extends uiapplication { private int[] jump; static final string[] words = new string[]{ "auto", "apple", "bear", "car", "farm", "ferret", "gold", "green", "garden", "hedge", "happy", "igloo", "infrared", "jelly", "kangaroo", "lemon", "lion", "marble", "moon", "nine", "opera", "orange", "people", "puppy", "pear", "quince", "race", "run", "sunset", "token", "willow", "zebra" }; private final static string[] alphabet = new string[]{"a","b","c","d","e", "f","g","h","i","j","k","l","m","n","o","p","q","r", "s","t","u","v","w","x","y","z","#"}; private verticalfieldmanager scroller;

public startup() { uiapplication.getuiapplication().invokelater(new runnable() { public void run() { uiapplication.getuiapplication().pushscreen(new scrollscreen()); } }); } public static void main(string[] args) { applicationmanager app = applicationmanager.getapplicationmanager(); while (app.instartup()) { try { thread.sleep(200); } catch (throwable e) {} } startup startup = new startup(); startup.entereventdispatcher(); } /** * screen content in scrollbar left , letters on right * can used jump content. */ class scrollscreen extends mainscreen { public scrollscreen() { super(no_horizontal_scroll | no_vertical_scroll); horizontalfieldmanager hfm = new horizontalfieldmanager(use_all_height | no_vertical_scroll | no_horizontal_scroll){ protected void sublayout(int maxwidth, int maxheight) { field scroll = getfield(0); field alpha = getfield(1); layoutchild(alpha, maxwidth, maxheight); layoutchild(scroll, maxwidth-alpha.getwidth(), maxheight); setpositionchild(scroll, 0, 0); setpositionchild(alpha, maxwidth-alpha.getwidth(), 0); setextent(maxwidth, maxheight); } }; hfm.add(createscrollcontent()); hfm.add(createalphabetjumpbar()); add(hfm); } private field createscrollcontent() { vector vocabulary = new vector(); (int ii=0; ii<alphabet.length; ii++) vocabulary.addelement(alphabet[ii]); scroller = new verticalfieldmanager(vertical_scroll | use_all_width) { protected void sublayout(int maxwidth, int maxheight) { // record jump offsets int y = 0; (int ii=0; ii<getfieldcount(); ii++) { field field = getfield(ii); layoutchild(field, maxwidth, maxheight); setpositionchild(field, 0, y); if (field instanceof wordfield) { wordfield object = (wordfield)field;; char character = object.getword().tolowercase().charat(0); int offset = ((int)character)-(int)alphabet[0].tolowercase().charat(0); if (offset < 0 || offset > jump.length) offset = jump.length-1; while (offset >= 0 && offset < jump.length && jump[offset] == 0) { jump[offset] = y; offset--; } } y += field.getheight(); } int offset = jump.length-1; { jump[offset] = y; offset--; } while (offset >= 0 && jump[offset] == 0); setextent(maxwidth, maxheight); setvirtualextent(maxwidth, y+10); } }; jump = new int[alphabet.length]; font largefont = font.getdefault().derive(font.plain, 46); (int ii=0; ii<words.length; ii++) { wordfield wordfield = new wordfield(words[ii]); wordfield.setfont(largefont); scroller.add(wordfield); } return scroller; } private field createalphabetjumpbar() { verticalfieldmanager vfm = new verticalfieldmanager() { protected void sublayout(int maxwidth, int maxheight) { int y = 0; int width = 0; double allowedalphaheight = (double)maxheight / (double)getfieldcount(); (int ii=0; ii<getfieldcount(); ii++) { wordfield field = (wordfield)getfield(ii); layoutchild(field, maxwidth, (int)allowedalphaheight); setpositionchild(field, 0, y); y += field.getheight(); double paddedy = math.floor(allowedalphaheight*(ii+1)); if (y < paddedy) y = (int)paddedy; width = math.max(width, field.getwidth()); } setextent(width, maxheight); } }; (int ii=0; ii<alphabet.length; ii++) { vfm.add(new alphafield(alphabet[ii]){ protected boolean touchevent(touchevent message) { if (message.getevent() == touchevent.up) { int startoffset = (int)alphabet[0].charat(0); int offset = ((int)getword().charat(0)) - startoffset; final int y = offset == 0 ? 0 : jump[offset - 1]; uiapplication.getuiapplication().invokelater(new runnable(){ public void run() { scroller.setverticalscroll(y, true); }}); } return true; } }); } return vfm; } class wordfield extends labelfield { private final string word; public wordfield(string word) { super(word); this.word = word; } public string getword() { return word; } } font alphafont = null; class alphafield extends wordfield { public alphafield(string word) { super(word); } protected void layout(int width, int height) { if (alphafont == null) alphafont = font.getdefault().derive(font.plain, height); setextent(alphafont.getadvance(getword()), alphafont.getheight()); } protected void paint(graphics graphics) { graphics.setfont(alphafont); graphics.drawtext(getword(), 0, 0); } } /** * debugging. * @see net.rim.device.api.ui.screen#keychar(char, int, int) */ protected boolean keychar(char c, int status, int time) { if ('o' == c) { // shows jump offsets scroll field uiapplication.getuiapplication().invokelater(new runnable(){ public void run() { stringbuffer buf = new stringbuffer(); (int ii=0; ii<jump.length; ii++) { buf.append(alphabet[ii]+"="+jump[ii]); if (ii<jump.length-1) buf.append(","); } status.show("offsets="+buf.tostring()); }}); } return super.keychar(c, status, time); } }

}

you're using uiapplication.invokelater in few places you're on ui event thread, redundant - debug code in keychar , setverticalscroll call touchevent handler. runnable executed synchronously when invokelater ui thread, no delay specified.

are sure want set scroll explicitly? 1 option set focus on wordfield interested in, calling setfocus(), os scrolling events move field on screen you.

if need explicitly set vertical scroll, problem may touch event causing scroll, setting again causes problems. can around specifying 1 millisecond delay invokelater(...). means runnable added event queue, instead of executing synchronously. way scroll won't changed in middle of event call-stack.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -