If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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

Adobe AIR Development

Reply
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

BB10 List with selected longPress

If you have a list and you have a long press to show the context menu, that all works fine.  However, if you have a list that allows items to be deselected and an already selected item gets a long press, it will still get unselected.  Seems that in this case, the long press should enforce the selected item to stay selected. 

 

This class will auto fill in the contect menu sub-title on the label of the pressed item.  If you deal a lot with lists, this will save you some time and lines of code.

 

If you would like to keep this behavior, here is a class to do that:

 

public class List extends qnx.fuse.ui.listClasses.List
{
	
	///////////////////////////////////////////////////////////////////////////
	public function List()
	{
		super();
		this.selectionMode = ListSelectionMode.SINGLE;
		this.allowDeselect = true;
	}
		
	//////////////////////////////////////////////////////////////////////////
	override public function set contextActions(value:Vector.<ActionSet>):void
	{
		super.contextActions = value;
		this.addEventListener( ContextMenuEvent.OPENING, ContextOpening, false, 50 );
		this.addEventListener( ContextMenuEvent.OPENED, ContextOpened, false, 50 );
		this.addEventListener( ContextMenuEvent.CLOSED, ContextClosed, false, 50 );	
	}
		
	////////////////////////////////////////////////////////////////////////
	private function ContextOpening( event : ContextMenuEvent ) : void
	{
		if( this.longPressedItem )
		{
			var set :ActionSet = this.contextActions[0];
			set.subtitle = this.longPressedItem.label;
				
			this.addEventListener(ListEvent.ITEM_CLICKED, ItemSelected, false, 50 );
		}
	}
		
	////////////////////////////////////////////////////////////////////////
	private function ContextOpened( event : ContextMenuEvent ) : void
	{
		if( this.selectedItem != null )
		{
			// will get toggled off if already selected
			this.selectedItem = this.longPressedItem;
		}
	}
		
	////////////////////////////////////////////////////////////////////////
	private function ContextClosed( event : ContextMenuEvent ) : void
	{
		this.removeEventListener( ListEvent.ITEM_CLICKED, ItemSelected );
	}
		
	//////////////////////////////////////////////////////////////////////
	private function ItemSelected( event : Event ) : void
	{
		if( this.selectedItem == null && this.longPressedItem != null )
		{
			this.selectedItem = this.longPressedItem;
		}
	}
}

 

Please use plain text.
Developer
Developer
mdd
Posts: 222
Registered: ‎01-17-2012
My Device: PB
My Carrier: ATT

Re: BB10 List with selected longPress

trying to use your suggestion:

 

seems that even before any longPress the list reports the longPressedItem = selectedItem when any item selected.

 

My list does not allow deselect...

Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: BB10 List with selected longPress

Then that will be true. But if you do allow your list to de-select, then you get into an imbalance with the selection state from a long press.
Please use plain text.