In this blog i ll show you how to rearrange a ListField in Blackberry.
Like Android we can achieve the DragNDrop custom view in Blackberry.
For more reference click this Link
Screen-1)
Create a Blackberry project.
Write the below code in your MainScreen.java
package com.amit.rearrange.listfield;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class RearrangeMainScreen extends MainScreen {
private VerticalFieldManager mainManager = new VerticalFieldManager();
public RearrangeMainScreen() {
mainManager.add(new LabelField("Welcome To Amit Gupta Blackberry Blog"));
mainManager.add(new LabelField("Rearranging ListField Example!!"));
mainManager.add(new MyList());
add(mainManager);
}
}
class MyList extends VerticalFieldManager implements ListFieldCallback {
private int mouseDownY, mouseUpY;
private static int rowHeight = 45;
private int mouseDownRowIndex, mouseUpRowIndex;
int firstSelectedRow = -1, secondSelectedRow = -1;
private int touchX;
private int touchY;
private boolean showShadow = false;
private ObjectListField list;
private Object[] listData = new Object[] { "Android", "Blackberry", "Windows Phone",
"Iphone", "Sembian", "Ubuntu Phone", "Motorola", "Samsung", "HTC", "Sony Experia",
"LG", "Karbon Mobile", "MicroMax" };
public MyList() {
init();
setBackground(BackgroundFactory.createLinearGradientBackground(
0x0FCFC0, 0x0CCCC0, 0x09C9C0, 0x01C1C0));
}
private void init() {
list = new ObjectListField(FOCUSABLE);
list.setRowHeight(rowHeight);
list.setCallback(this);
list.set(listData);
add(list);
}
private void interchangeRows(int r1, int r2) {
Object temp = listData[r1];
listData[r1] = listData[r2];
listData[r2] = temp;
invalidate();
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
graphics.drawText(listData[index].toString(), 25, y + 10);
}
public Object get(ListField listField, int index) {
return listData[index];
}
public int getPreferredWidth(ListField listField) {
return getPreferredWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
public int indexOfRowAt(int posY) {
int index = (int) Math.floor(posY / rowHeight * 1.0);
return index;
}
protected boolean keyChar(char ch, int status, int time) {
if (ch == Characters.SPACE) {
if (firstSelectedRow == -1) {
firstSelectedRow = list.getSelectedIndex();
return true;
} else {
secondSelectedRow = list.getSelectedIndex();
if (firstSelectedRow == secondSelectedRow) {
firstSelectedRow = secondSelectedRow = -1;
invalidate();
return true;
} else {
interchangeRows(firstSelectedRow, secondSelectedRow);
firstSelectedRow = secondSelectedRow = -1;
return true;
}
}
}
return super.keyChar(ch, status, time);
}
protected void paint(Graphics graphics) {
if (firstSelectedRow != -1) {
int x = 0, y = firstSelectedRow * rowHeight;
int savedColor = graphics.getColor();
int preAlpha = graphics.getGlobalAlpha();
graphics.setGlobalAlpha(90);
graphics.setColor(0xFF22FF);
graphics.fillRect(0, y, getWidth(), rowHeight);
graphics.setColor(savedColor);
graphics.setGlobalAlpha(preAlpha);
}
super.paint(graphics);
if (showShadow && mouseDownRowIndex != -1) {
int preAlpha = graphics.getGlobalAlpha();
graphics.setGlobalAlpha(100);
graphics.drawText(listData[mouseDownRowIndex].toString(), 25,
touchY);
graphics.setGlobalAlpha(preAlpha);
}
}
protected boolean touchEvent(TouchEvent message) {
int eventCode = message.getEvent();
// Get the screen coordinates of the touch event
touchX = message.getX(1);
touchY = message.getY(1);
if (eventCode == TouchEvent.DOWN) {
mouseDownY = touchY;
mouseDownRowIndex = indexOfRowAt(mouseDownY);
showShadow = true;
invalidate();
return true;
} else if (eventCode == TouchEvent.UP) {
showShadow = false;
mouseUpY = touchY;
mouseUpRowIndex = indexOfRowAt(mouseUpY);
if (mouseDownRowIndex != mouseUpRowIndex) {
interchangeRows(mouseDownRowIndex, mouseUpRowIndex);
mouseDownRowIndex = mouseUpRowIndex = -1;
return true;
}
mouseDownRowIndex = mouseUpRowIndex = -1;
invalidate();
} else if (eventCode == TouchEvent.MOVE) {
invalidate();
return true;
}
return super.touchEvent(message);
}
}
Note:-
Like Android we can achieve the DragNDrop custom view in Blackberry.
It will work on
Torch (9800), Storm (9550), Bold (9700):
For more reference click this Link
Screen-1)
Screen 2) After rearranging the ListField item.
Create a Blackberry project.
Write the below code in your MainScreen.java
package com.amit.rearrange.listfield;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class RearrangeMainScreen extends MainScreen {
private VerticalFieldManager mainManager = new VerticalFieldManager();
public RearrangeMainScreen() {
mainManager.add(new LabelField("Welcome To Amit Gupta Blackberry Blog"));
mainManager.add(new LabelField("Rearranging ListField Example!!"));
mainManager.add(new MyList());
add(mainManager);
}
}
class MyList extends VerticalFieldManager implements ListFieldCallback {
private int mouseDownY, mouseUpY;
private static int rowHeight = 45;
private int mouseDownRowIndex, mouseUpRowIndex;
int firstSelectedRow = -1, secondSelectedRow = -1;
private int touchX;
private int touchY;
private boolean showShadow = false;
private ObjectListField list;
private Object[] listData = new Object[] { "Android", "Blackberry", "Windows Phone",
"Iphone", "Sembian", "Ubuntu Phone", "Motorola", "Samsung", "HTC", "Sony Experia",
"LG", "Karbon Mobile", "MicroMax" };
public MyList() {
init();
setBackground(BackgroundFactory.createLinearGradientBackground(
0x0FCFC0, 0x0CCCC0, 0x09C9C0, 0x01C1C0));
}
private void init() {
list = new ObjectListField(FOCUSABLE);
list.setRowHeight(rowHeight);
list.setCallback(this);
list.set(listData);
add(list);
}
private void interchangeRows(int r1, int r2) {
Object temp = listData[r1];
listData[r1] = listData[r2];
listData[r2] = temp;
invalidate();
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
graphics.drawText(listData[index].toString(), 25, y + 10);
}
public Object get(ListField listField, int index) {
return listData[index];
}
public int getPreferredWidth(ListField listField) {
return getPreferredWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
public int indexOfRowAt(int posY) {
int index = (int) Math.floor(posY / rowHeight * 1.0);
return index;
}
protected boolean keyChar(char ch, int status, int time) {
if (ch == Characters.SPACE) {
if (firstSelectedRow == -1) {
firstSelectedRow = list.getSelectedIndex();
return true;
} else {
secondSelectedRow = list.getSelectedIndex();
if (firstSelectedRow == secondSelectedRow) {
firstSelectedRow = secondSelectedRow = -1;
invalidate();
return true;
} else {
interchangeRows(firstSelectedRow, secondSelectedRow);
firstSelectedRow = secondSelectedRow = -1;
return true;
}
}
}
return super.keyChar(ch, status, time);
}
protected void paint(Graphics graphics) {
if (firstSelectedRow != -1) {
int x = 0, y = firstSelectedRow * rowHeight;
int savedColor = graphics.getColor();
int preAlpha = graphics.getGlobalAlpha();
graphics.setGlobalAlpha(90);
graphics.setColor(0xFF22FF);
graphics.fillRect(0, y, getWidth(), rowHeight);
graphics.setColor(savedColor);
graphics.setGlobalAlpha(preAlpha);
}
super.paint(graphics);
if (showShadow && mouseDownRowIndex != -1) {
int preAlpha = graphics.getGlobalAlpha();
graphics.setGlobalAlpha(100);
graphics.drawText(listData[mouseDownRowIndex].toString(), 25,
touchY);
graphics.setGlobalAlpha(preAlpha);
}
}
protected boolean touchEvent(TouchEvent message) {
int eventCode = message.getEvent();
// Get the screen coordinates of the touch event
touchX = message.getX(1);
touchY = message.getY(1);
if (eventCode == TouchEvent.DOWN) {
mouseDownY = touchY;
mouseDownRowIndex = indexOfRowAt(mouseDownY);
showShadow = true;
invalidate();
return true;
} else if (eventCode == TouchEvent.UP) {
showShadow = false;
mouseUpY = touchY;
mouseUpRowIndex = indexOfRowAt(mouseUpY);
if (mouseDownRowIndex != mouseUpRowIndex) {
interchangeRows(mouseDownRowIndex, mouseUpRowIndex);
mouseDownRowIndex = mouseUpRowIndex = -1;
return true;
}
mouseDownRowIndex = mouseUpRowIndex = -1;
invalidate();
} else if (eventCode == TouchEvent.MOVE) {
invalidate();
return true;
}
return super.touchEvent(message);
}
}
Note:-
For touch-supported devices: a shadow of text of the first selected item moves along with the cursor.
For non-touch devices: I've used SPACEBAR to select the items to interchange. Select an item, press SPACEBAR, select the second item, press SPACEBAR again, items interchanged !! PressingSPACEBAR on an already selected item will deselect that item.
Download Full Source code Click DragNDropListField

No comments:
Post a Comment