Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
New Contributor
Posts: 2
Registered: ‎08-13-2010
My Device: Not Specified

Problem on Custom List when the focus is obtained

Hello! Firstly sorry by my english!!.Smiley Tongue

 

I created a Custom list where the rows can be of diferent heights.
This custom list extend from Field.
My problem is when the focus is lose going down (it's on the last item and passes the focus to the next field, which is fine); when I come back to going up, the manager send my list to draw the first item instead the last.
I tried to force the manager that set the vertical scroll position overriding onFocus method, but it didn't work.

Here is my code:

 

 

public class CustomList extends Field implements DrawStyle {

	private IRowDrawCustomList rowDraw;
	private Vector elements;
	private XYRect oldDesv;
	private int[] yPosition;
	private int pos;

	public CustomList() {
		this(0);
	}

	public CustomList(long style) {
		super(style | STATUS_MOVE_FOCUS_VERTICALLY);

		pos = 0;
		elements = new Vector();
		oldDesv = new XYRect();
	}

	public void setRowDrawCustomList(IRowDrawCustomList rowDraw) {
		this.rowDraw = rowDraw;
	}

	protected void layout(int width, int height) {
		int auxHeight = 0;
		yPosition = new int[elements.size() + 1];
		int i;
		rowDraw.beginCalculate(this);
		for (i = 0; i < elements.size(); i++) {
			yPosition[i] = auxHeight;
			auxHeight += rowDraw.getHeigth(this, elements.elementAt(i), i);
		}
		yPosition[i] = auxHeight;
		width = Math.min(width, getPreferredWidth());
		height = Math.max(auxHeight, getPreferredHeight());
		setExtent(width, height);
	}

	protected void drawFocus(Graphics graphics, boolean on) {
		// super.drawFocus(graphics, on);
	}

	public boolean isFocusable() {
		return true;
	}

	protected void onFocus(int direction) {
		if (direction == -1)
			pos = elements.size() - 1;
		else
			pos = 0;
		super.onFocus(direction);
	}

	public int getPreferredWidth() {
		return Graphics.getScreenWidth();
	}

	public int getPreferredHeight() {
		return 0;
	}

	private int move(int amount) {
		int left = 0;
		if (amount < 0) {
			if (pos + amount < 0) {
				left = pos + amount;
				pos = 0;

			} else {
				pos += amount;

			}
		} else {
			if (pos + amount > elements.size() - 1) {
				left = pos + amount - (elements.size() - 1);
				pos = elements.size() - 1;

			} else {
				pos += amount;

			}
		}
		invalidate();
		return left;
	}

	private int getNewScrollPosition() {
		final int yFocus = yPosition[pos];
		final int hFocus = yPosition[pos + 1] - yFocus;
		if (oldDesv.y > yFocus)
			return yFocus;
		else {
			final int total = oldDesv.y + oldDesv.height;
			final int totalNew = yFocus + hFocus;
			if (oldDesv.y <= yFocus && total >= totalNew)
				return oldDesv.y;
			else

				return oldDesv.y + (totalNew - total);
		}
	}

	protected boolean navigationMovement(int dx, int dy, int status, int time) {
		final int i = move(dy);
		getManager().setVerticalScroll(getNewScrollPosition());
		if (i == 0)
			return true;
		else
			return super.navigationMovement(dx, dy, status, time);
	}

	protected boolean trackwheelRoll(int arg0, int arg1, int arg2) {
		final int i = move(arg0);
		getManager().setVerticalScroll(getNewScrollPosition());
		if (i == 0)
			return true;
		else
			return super.trackwheelRoll(arg0, arg1, arg2);
	}

	protected void paint(Graphics graphics) {

		XYRect focused = graphics.getClippingRect();
		int startLine = 0;
		int endLine = 0;
		final int endFocus = focused.y + focused.height;
		for (int j = 0; j < yPosition.length - 1; j++) {
			if (yPosition[j] < focused.y)
				startLine = j;
			if (yPosition[j] < endFocus)
				endLine = j;
			else
				break;
		}

		int textY = yPosition[startLine];
		final int width = getWidth();
		final boolean isFocus = isFocus();
		for (; startLine <= endLine; ++startLine) {
			int height = yPosition[startLine + 1] - yPosition[startLine];
			rowDraw.drawRow(this, graphics, elements.elementAt(startLine), startLine, pos == startLine && isFocus, 0, textY, width, height);
			textY += height;
		}
		if (isFocus)
			oldDesv = focused;
	}

	protected boolean keyChar(char character, int status, int time) {
		switch (character) {
		case Characters.ENTER:
			fieldChangeNotify(0);
			return true;
		default:
			return false;
		}
	}

	protected boolean invokeAction(int action) {
		switch (action) {
		case ACTION_INVOKE:
			fieldChangeNotify(0);
			return true;
		default:
			return super.invokeAction(action);
		}
	}

	public void setList(Vector items) {
		elements = items;
		pos = 0;
		updateLayout();
	}

	public Object getObject(int position) {
		if (position > 0 && position < elements.size())
			return elements.elementAt(position);
		return null;
	}

	public Object getSelected() {
		if (pos >= 0 && pos < elements.size())
			return elements.elementAt(pos);
		return null;
	}

	public int getPositionSelected() {
		return pos;
	}

	

}

 

Thanks!