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
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: ListField that scrolls Horizontally

hello  sir every  thing is fine in os greater than 5 ..i check 9360 (non touch )..but it possible to make this for 9700 ?

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: ListField that scrolls Horizontally

OK, so just to confirm, do you agree that the supplied code works fine of OS 6 and above, on all devices?

 

So the problem is the non touch screen OS 5.0 devices only.

 

Correct?

Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: ListField that scrolls Horizontally

yes...thanks but i need for os 5.0 9700 ..

1 more question..

can you sir explain briefly what is LDAP

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: ListField that scrolls Horizontally

One question per Thread please.  And LDAP is not a BlackBerry term - use Google for that one.

 

Here is some updated code that I believe works in OS 5.0 as well as OS 6.0+.  Give it a go.

 

Unfortunately I am not available for the next two days, if this code doesn't work you will have to hope someone else can help you.  Good luck.

 

//! see comment lines starting with "//!"
//! for important points regarding this.

import java.util.Vector;

import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYRect;
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.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

public class HorizontalScrollTestScreen  extends MainScreen implements ListFieldCallback{

	private static int NUMBER_OF_ROWS_TO_ADD = 50;
	private static int NUMBER_OF_COLUMNS_TO_DISPLAY = 6;

	private Vector _listElements = new Vector();
	// We use a pretty crude method to supply the items to be displayed
	private ListField _listField;
	private int _requiredColumnWidth;
	
	HorizontalScrollTestScreen(){

		//! how wide will we make the columns?
		Font ourFont = this.getFont();
		_requiredColumnWidth = ourFont.getAdvance(" column 8 ");
		//! this example has fixed column widths.  They do not have to be.  Fixed is just simple to demonstrate.

		this.add(new LabelField("Above ListField", LabelField.FOCUSABLE));
		this.add(new SeparatorField());
		//! Add the ListField to a HorizontalFieldMaanger that can scroll horizontally.
		//! It is this that does the scrolling for us.
		HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL|Manager.HORIZONTAL_SCROLLBAR) {
			// Override paintBackground just so we can see the extent of this Manager
			// Not necessary, just done to make it easier to see.  
			private int osLevel = -1;
			private int pixelsToScroll = -1;
			public void paintBackground(Graphics g) {
				int currentBackgroundColor = g.getBackgroundColor();
				try {
					g.setBackgroundColor(0X00CCCCCC);
					g.clear();
				} finally {
					g.setBackgroundColor(currentBackgroundColor);
				}
			}
			//! For OS 5.0 and below we have to override navigationMovememnt and
			//! do the horizontal scroll ourselves.
			protected boolean navigationMovement(int dx, int dy, int status, int time) {
				if ( osLevel < 0 ) {
					//! Only get the software version once
					try {
						String softwareLevel = DeviceInfo.getSoftwareVersion();
						int endOfVersion = softwareLevel.indexOf('.');
						osLevel = Integer.parseInt(softwareLevel.substring(0,endOfVersion));
					} catch (Exception e) {
						//! Error - manage it ourselves....
						osLevel = 5;
					}
				}
				if ( dx != 0 && osLevel < 6 ) {
					//! OS 5.0 or lower - manage horizontal scrolling ourselves
					if ( pixelsToScroll < 1 ) {
						pixelsToScroll = Display.getWidth() / 10; 
						//! This is a purely arbitrary value - set this as per your requirement.
						//! One option is to set this to match the column width.  
					}
					int currScrollPostion = this.getHorizontalScroll();
					int newScrollPosition = currScrollPostion + dx*pixelsToScroll;
					if ( newScrollPosition < 0 ) {
						newScrollPosition = 0;
					} else {
						if ( newScrollPosition > this.getVirtualWidth() - this.getWidth() ) {
							newScrollPosition = this.getVirtualWidth() - this.getWidth();
						}
					}
					if ( newScrollPosition != currScrollPostion ) {
						System.out.println("Scrolling to: " + Integer.toString(newScrollPosition));
						this.setHorizontalScroll(newScrollPosition);
					}
					return true;
				} else {
					return super.navigationMovement(dx, dy, status, time);
				}
			}
		};
		//! Note the overridden methods in our ListField.
		_listField = new ListField() {
			//! OVerride layout so that you get the ListField defined the width that you want.
			protected void layout(int maxWidth, int maxHeight) {
				int requiredWidth = Math.min(maxWidth, this.getPreferredWidth());
				super.layout(requiredWidth, maxHeight);
			}
			//! Override getFocusRect otherwise the scroll up or down the LisstField will move the
			//! focus back to the beginning of the row.  
			//! Remove this and see what happens.  
			public void getFocusRect(XYRect focusRect) {
				super.getFocusRect(focusRect);
				focusRect.x = this.getManager().getHorizontalScroll();
				focusRect.width = Math.min(Display.getWidth(), this.getWidth() - focusRect.x);
			}
			//! Use getPreferredWidth() to set the required width.
			//! In fact, in my testing, this method is ONLY called from our
			//! layout method, though the "framework" can, in theory,
			//! call getPreferredWidth() whenever it wants.  
			public int getPreferredWidth() {
				return _requiredColumnWidth * NUMBER_OF_COLUMNS_TO_DISPLAY;
			}
		};
		_listField.setCallback(this);
		_listField.setSize(NUMBER_OF_ROWS_TO_ADD);
		// setSearchable(true) so a key stroke will invoke indexOfList().  Try it
		_listField.setSearchable(true);
		for(int count = 0; count < NUMBER_OF_ROWS_TO_ADD; ++count) {
			String listItem = Integer.toString(count) + ".";
			_listElements.insertElementAt(listItem, count);
      }
		hfm.add(_listField);
		this.add(hfm);
		this.add(new SeparatorField());
		this.add(new LabelField("Below ListField", LabelField.FOCUSABLE));
	}

	// Following methods are required by the ListFieldCallback interface

	public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) {

		int columnWidth = width/NUMBER_OF_COLUMNS_TO_DISPLAY;
		//! Use the width that we have, not the width we requested.
		//! There is a chance that they might be different, though I suspect this would only
		//! be seen during development when perhaps changes have not be made consistently. 
		   
		int xpos = 0;

		// First column test data supplied from program
		String suppliedData = (String) this.get(listField, index);
		graphics.setColor(Color.BLACK);
		graphics.drawText(suppliedData, xpos, y);
		xpos +=  columnWidth;

		// Dummy up the other columns.....
		graphics.setColor(Color.RED); 
		for ( int i = 1; i < NUMBER_OF_COLUMNS_TO_DISPLAY; i++ ) {
			graphics.drawText("column" + Integer.toString(i), xpos, y);
			xpos +=  columnWidth;
		}

	}

	public Object get(ListField listField, int index) {
		 return _listElements.elementAt(index);
	}

	//! In theory, the "framework" can call this, so it should return the same 
	//! value as the overridden getPreferredWidth().  But my testing suggests this is 
	//! is actually never called.  But for consistency, set it correctly!
	public int getPreferredWidth(ListField litfield) {
		return _requiredColumnWidth * NUMBER_OF_COLUMNS_TO_DISPLAY;
	}

	// People don't know how to use this, so I'm coding this up as an example
	// In the sample code you can use the digits to find things...
	public int indexOfList(ListField listField, String prefix, int start) {
		// Search from where we currently are forward.
		for ( int i = start; i < _listElements.size(); i++ ) {
			String element = (String) this.get(listField, i);
			if ( element != null && prefix != null ) {
				// Just being careful
				if ( element.startsWith(prefix) ) {
					return i;
				}
			}
		}
		// Not found, search from beginning to where we are currently
		for ( int i = 0; i < start; i++ ) {
			String element = (String) this.get(listField, i);
			if ( element != null && prefix != null ) {
				// Just being careful
				if ( element.startsWith(prefix) ) {
					return i;
				}
			}
		}
		// Didn't find it, don't move....
		return start;
	}

}

 

Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: ListField that scrolls Horizontally

Thanks sir nice solution ...got it  ..Thanks