BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 02-26-2009, 10:42 PM   #1 (permalink)
New Member
 
Join Date: Feb 2009
Model: 8320
PIN: N/A
Carrier: china mobile
Posts: 9
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default scroll a big field?

Please Login to Remove!

I use a custome field. the field height is larger than the screen height.

I override the navigationMovement and getFocusRect method to implement the focus movement inside the field. now i have a problem, when the focus rect is outside the view, the screen do not scroll, so how to implement this behave. just like the RichTextField , when the focus rect is outside the view, the screen will scroll to display the next focus rect.

any help? thanks!

Code:
 public void getFocusRect(XYRect rect) {
		rect.set(0, verticalPadding+headerHeight+rowHeight*selectedRowIndex, getWidth(), rowHeight);
	}

protected boolean navigationMovement(int dx, int dy, int status, int time) {
		 
		if ((status&KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY) { 
			if (dx > 0) {
				 
				xoffset -= 10;
				invalidate();
				return true;
			} else if (dx < 0) {
				 
				xoffset += 10;
				invalidate();
				if(xoffset > 0) {
					xoffset = 0;
					return true;
				}
			} else if (dy > 0) {
				 
				if(selectedRowIndex < this.rowNum) {
					selectedRowIndex ++;
					if(selectedRowIndex == rowNum) {
						selectedRowIndex = rowNum-1;
						return false;
					}
					invalidate();
					return true;
				}
			} else if (dy < 0) {
				 
				if(selectedRowIndex > 0) {
					selectedRowIndex --;
					if(selectedRowIndex < 0) {
						selectedRowIndex = 0;
						return false;
					}
					invalidate();
					return true;
				}
			}
		}  
		return super.navigationMovement(dx, dy, status, time);
	}
Offline  
Old 02-27-2009, 01:20 AM   #2 (permalink)
New Member
 
Join Date: Feb 2009
Model: 8320
PIN: N/A
Carrier: china mobile
Posts: 9
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

the full code :
Code:
package hs.bb;

import net.rim.device.api.system.KeypadListener;
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.XYRect;
import net.rim.device.api.ui.container.MainScreen;
public class TestScreen extends MainScreen {
	public TestScreen() {
		this.setTitle("Test Table Field");
		 String[] header = {"header one","header two","header three"}; 
	        String[][] value = {{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 2 1","value 2 2","value 2 3"},{"value 3 1","value 3 2","value 3 3"}};
	        TableField st = new TableField(header,value,Field.FOCUSABLE);
	        add(st);
	}

	public class TableField extends Field {
		private int fontColor = 0x0;
		private int focusLineColor = 0xFFFFFF;
		private int[] columnWidths;
		private Font valueFont;
		private Font headerFont;
		private int headerColor = 0xFFF000;
		private int headerBgColor = 0x444444;
		private int headerHeight;
		
		private int selectedRowIndex = -1;
		private int selectedColumnIndex = -1; 
		
		private String[] headers;
		private String[][] values;
		
		private int rowPadding = 3;
		private int columnPadding = 3;
		private int horizontalPadding = 5;  
		private int verticalPadding = 5;  
		
		private int rowHeight;
		private int colNum;
		private int rowNum;
		
		public TableField(String[] headers,String[][] values,long style ) {
			super(style);
			if(headers != null && headers.length > 0) {
				this.headers = headers;
				headerFont = Font.getDefault();
				headerHeight = headerFont.getHeight()+rowPadding;
			}
			this.values = values;
			this.valueFont = Font.getDefault();
			rowHeight = this.valueFont.getHeight()+rowPadding;
			this.rowNum = values.length;
			this.colNum = values[0].length;
			int tw = 0;
			columnWidths = new int[colNum];
			for (int i = 0; i < colNum; i++) {
				for (int j = 0; j < rowNum; j++) {
					tw = valueFont.getAdvance(values[j][i]);
					if(tw > columnWidths[i]) {
						columnWidths[i] = tw;
					}
				}
			}
		}

		protected void layout(int width, int height) {
			int pw = getPreferredWidth();
			if(pw < width) {
				pw = (width - pw)/this.colNum;
				for (int i = 0; i < colNum; i++) {
					columnWidths[i] += pw;
				}
			}
			setExtent(width, getPreferredHeight());
		}

		private int xoffset = 0;
		protected void paint(Graphics g) {
			
			int x = horizontalPadding;
			int y = verticalPadding;
			if(headers != null && headers.length > 0) {
				if(headerBgColor != -1) {
					g.setColor(headerBgColor);
					g.fillRect(0, 0, getWidth(), verticalPadding+headerHeight);
				}
				if(headerColor != -1) {
					g.setColor(headerColor);
				}
				for (int i = 0; i < colNum; i++) {
					g.drawText(headers[i], x+xoffset, y);
					x += columnWidths[i] + columnPadding;
				}
				y+= headerHeight;
			}
			
			x = horizontalPadding;
			if(values != null && values.length > 0) {
				if(fontColor != -1) {
					g.setColor(fontColor);
				}
				for (int i = 0; i < colNum; i++) {
					int vy = y;
					for (int j = 0; j < rowNum; j++) {
						g.drawText(values[j][i], x+xoffset, vy);
						vy += rowHeight;
					}
					x+=columnWidths[i]+columnPadding;
				}
			}
			//paint focus row line
			if(focusLineColor != -1 && isFocus()) {
				g.setColor(focusLineColor);
				int fly = y + rowHeight * (selectedRowIndex+1)-rowPadding/2;
				g.drawLine(0, fly, getWidth(), fly);
			}
			g.setColor(fontColor);
			g.drawRect(0, 0, getWidth(), getHeight());
		}

		public void getFocusRect(XYRect rect) {
			rect.set(0, verticalPadding+headerHeight+rowHeight*selectedRowIndex, getWidth(), rowHeight);
		}

		protected int moveFocus(int amount, int status, int time) {
			//TODO  how to implement this method?
			return super.moveFocus(amount, status, time);
		}
		

		protected boolean navigationMovement(int dx, int dy, int status, int time) {
			if ((status&KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY) { 
				if (dx > 0) {
					xoffset -= 10;
					invalidate();
					return true;
				} else if (dx < 0) {
					xoffset += 10;
					invalidate();
					if(xoffset > 0) {
						xoffset = 0;
						return true;
					}
				} else if (dy > 0) {
					if(selectedRowIndex < this.rowNum) {
						selectedRowIndex ++;
						if(selectedRowIndex == rowNum) {
							selectedRowIndex = rowNum-1;
							return false;
						}
						invalidate();
						return true;
					}
				} else if (dy < 0) {
					if(selectedRowIndex > 0) {
						selectedRowIndex --;
						if(selectedRowIndex < 0) {
							selectedRowIndex = 0;
							return false;
						}
						invalidate();
						return true;
					}
				}
			}  
			return super.navigationMovement(dx, dy, status, time);
		}

		protected void onFocus(int direction) {
			if(selectedRowIndex == -1) {
				selectedRowIndex = 0;
			}
			super.onFocus(direction);
		}

		protected void onUnfocus() {
			super.onUnfocus();
			invalidate();
		}

		public int getPreferredHeight() {
			return  headerHeight+(rowHeight*rowNum)+(verticalPadding<<1);
		}

		public int getPreferredWidth() {
			int w = 0;
			for (int i = 0; i < colNum; i++) {
				w += columnWidths[i]+columnPadding;
			}
			if(w > 0) w -= columnPadding;
			w += (horizontalPadding<<1);
			return w;
		}
	}

}
when the selected row is outside the screen , how to scroll the screen to dispaly the row?
Offline  
Old 02-27-2009, 03:46 AM   #3 (permalink)
New Member
 
Join Date: Feb 2009
Model: 8320
PIN: N/A
Carrier: china mobile
Posts: 9
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I change the navigationMovement mothed , set the manager scroll value to scroll the screen . now can display the selected row. But I donot think this's a good solution. Does anyone have a better solution for this?

the code:
Code:
protected boolean navigationMovement(int dx, int dy, int status, int time) {
			Manager manager = getManager();
			XYRect xyrect = this.getExtent();
			
			if ((status&KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY) { 
				if (dx > 0) {
					xoffset -= 10;
					invalidate();
					return true;
				} else if (dx < 0) {
					xoffset += 10;
					invalidate();
					if(xoffset > 0) {
						xoffset = 0;
						return true;
					}
				} else if (dy > 0) {
					if(selectedRowIndex < this.rowNum) {
						selectedRowIndex ++;
						if(selectedRowIndex == rowNum) {
							selectedRowIndex = rowNum-1;
							return false;
						}
						int rowYPosition = xyrect.y + verticalPadding+headerHeight+rowHeight*selectedRowIndex+rowHeight;
						int visiableLastY = manager.getVerticalScroll()+manager.getVisibleHeight();
						if(rowYPosition > visiableLastY) {// 
							manager.setVerticalScroll(getManager().getVerticalScroll()+rowHeight);
						}
						invalidate();
						return true;
					}
				} else if (dy < 0) {
					if(selectedRowIndex > 0) {
						selectedRowIndex --;
						if(selectedRowIndex < 0) {
							selectedRowIndex = 0;
							return false;
						}
						int rowYPosition = xyrect.y + verticalPadding+headerHeight+rowHeight*selectedRowIndex;
						int visiableLastY = manager.getVerticalScroll();
						if(rowYPosition < visiableLastY) { 
							manager.setVerticalScroll(getManager().getVerticalScroll()-rowHeight);
						}
						invalidate();
						return true;
					}
				}
			}  
			return super.navigationMovement(dx, dy, status, time);
		}
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.