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
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need to scroll list field horizontally

[ Edited ]

"Actually  That screen i have only one listfield ..."

 

Oppss.  So it does.  My mistake. 

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need to scroll list field horizontally

Mean?

i am not able to understand..

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need to scroll list field horizontally

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Need to scroll list field horizontally

[ Edited ]

In general, you can achieve horizontal scrolling in two ways:

 

1) overriding getFocusRect and changing it's return values in case of horizontal navigation;

2) manually setting the Manager's scrolling position using setHorizontalScroll (should be really easy but will probably reset on each vertical movement so I don't recommend it in your case).

 

Also remember that you have your two lists in two different Managers (listFieldManager and listFieldManager_2) so they can be scrolled separately. When you switch between the two, though, you'll probably see the unfocused one snap back to its "default" scrolling position of (0, 0). See if you can cure that with NO_SCROLL_RESET (if you want to keep their scrolling, that is).

 

Anyway, back to the task at hand:

If you choose method #1, you can create a method in your ListField to accept dx as a parameter and do the following:

1) call focusRemove() to tell the framework that your ListField's focus area is going to change;

2) change the values returned by getFocusRect according to dx and the current focus values;

3) call focusAdd() to tell frameworks that the focus area update is complete.

 

Call that method from navigationMovement in your MainScreen when dx is not zero.

 

The values returned by getFocusRect should be like this:

x - the horizontal position of the column you want to display

y - the vertical position of the current row (be careful here - you want this to change automatically as you navigate through the list vertically, so make sure to utilize getSelectedIndex() in your calculations)

width - the column's width, screen width would be the natural pick for this in your implementation

height - the height of the current row; however, it might not be that easy if you allow multi-select. In that case, calling getSelection() and calculating the whole height would be safer.

 

Good luck!

 

Edit: Peter beat me to the getFocusRect example. I hope these two posts should make some sense together...

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need to scroll list field horizontally

HorizontalScrollTestScreen.java 6 KB  example is not working on simulator 9700 ...

 

i copy the whole code only listfield is display .when i scroll the trackpad it is not scrolling...

can you please help me..

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need to scroll list field horizontally

 
Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Need to scroll list field horizontally

i  am changing my code 

 

package com.black.applicationloader;

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

import java.util.Vector;

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?
		_requiredColumnWidth = this.getFont().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.  
			public void paintBackground(Graphics g) {
				int currentBackgroundColor = g.getBackgroundColor();
				try {
					g.setBackgroundColor(0X00CCCCCC);
					g.clear();
				} finally {
					g.setBackgroundColor(currentBackgroundColor);
				}
			}
		};
		//! 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;
			}
			protected boolean navigationMovement(int dx, int dy, int status, int time) {
				
				if(dx>0){
					removeFocus();
					//this.getManager().getHorizontalScroll();
				
					focusAdd(true);
				}
				return true;
			};
			
			
		};
		_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;
	}

}

 

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need to scroll list field horizontally

Please use plain text.