Results 1 to 3 of 3
- 01-08-2009, 07:46 PM
Thread Author #1
Scrolling AND selecting items in a ListField on the Storm?
Background info:
BlackBerry Storm 9500
BlackBerry JDE 4.7.0
Problem:
I am using a ListField to display multiple items in a list. I am also using the touchEvent(TouchEvent message) method to listen for a click (TouchEvent.CLICK). Everything seems to work just fine unless I need to scroll the list. It seems that by using the touchEvent method, I cannot scroll the list. If I remove the touchEvent method, scrolling works but I am unable to use the touch interface to select an item in the list by clicking on it.
Question:
Is there a way to get both working? That is, can I enable scrolling in the ListField AND allow the user to select an item by clicking on it?
Code:
private class SlideList extends MainScreen
{
// Fields
private ListField _list;
private ListCallback _callback;
// Constructor
public SlideList()
{
super();
//add a screen title
LabelField title = new LabelField(getTitle(), LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
setupList();
}
private void setupList()
{
if (_list != null)
delete(_list);
// Get all slides and put their titles and subtitles into an array
String[] slideTitles = getSlideTitles();
_list = new ListField();
_callback = new ListCallback();
_list.setCallback(_callback);
// Build exercise list.
for (int index=0; index<slideTitles.length; index++)
{
_list.insert(index);
_callback.insert(slideTitles[index], index);
}
_list.setSelectedIndex(currentSlideNum);
add(_list);
}
// Get touch input.
protected boolean touchEvent(TouchEvent message)
{
if (message.getEvent() == TouchEvent.CLICK)
{
// Check if a list item is selected.
if (_list.getSelectedIndex() > -1)
{
// Dialog.alert("You selected: "+_list.getSelectedIndex());
itemSelected(_list.getSelectedIndex());
return true;
}
else
return false;
}
else
return false;
}
private void itemSelected(int listIndex)
{
currentSlideNum = listIndex;
displaySlide();
UiApplication.getUiApplication().popScreen(this); // Remove this screen to display the program.
}
private class ListCallback implements ListFieldCallback {
private Vector listElements = new Vector();
public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{
String text = (String)listElements.elementAt(index);
g.drawText(text, slideIcons[index].getWidth()+10, y, 0, w);
}
public Object get(ListField list, int index) {
return listElements.elementAt(index);
}
public int indexOfList(ListField list, String p, int s) {
//return listElements.getSelectedIndex();
return listElements.indexOf(p, s);
}
public int getPreferredWidth(ListField list) {
Screen sr = UiApplication.getUiApplication().getActiveScreen() ;
return sr.getWidth();
}
public void insert(String toInsert, int index) {
listElements.insertElementAt(toInsert, index);
}
public void erase() {
listElements.removeAllElements();
}
}
} - 04-08-2009, 02:33 AM #2
Having never done this specifically, I'm not certain if there is an easier way... but...
I would try detecting the region of the touch event on the screen. If its not on the right side, select the list item. If its on the right side, scroll.
Of course, you could also consider making both "margins" scroll areas and only the center a "click area". - 04-08-2009, 03:30 AM #3
Perhaps that, because the touch events in 4.7 essentially are mappings to trackwheel actions (to maintain backwards compatibility), you should try extend ListField and override trackwheelClick. It's just a guess, though.
Reply
















