is there an built in method in spark tile list to get an item from a given point?
Thanks
is there an built in method in spark tile list to get an item from a given point?
Thanks
As @www.Flextras.com suggested, looking at the source code can be useful. TileLayout
has a method named getElementNearestScrollPosition()
that will give you the index of the element that is located closest to a Point
that you specify. This method is hidden in Flex's mx_internal
namespace, however, so it gets excluded from the ASDocs.
Here's a simple example that seems to be working. I'm not showing the itemRenderer.
Main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:local="*">
<fx:Script>
<![CDATA[
import mx.core.mx_internal;
use namespace mx_internal;
protected function list1_clickHandler(event:MouseEvent):void
{
var localPointInList:Point = list.globalToLocal(new Point(event.stageX, event.stageY));
trace(tileLayout.getElementNearestScrollPosition(localPointInList));
}
]]>
</fx:Script>
<fx:Declarations>
<s:ArrayCollection id="foo">
<fx:Object date="Jan 1, 2012"/>
<fx:Object date="Jan 1, 2013"/>
<fx:Object date="Jan 1, 2014"/>
</s:ArrayCollection>
</fx:Declarations>
<s:layout>
<s:HorizontalLayout />
</s:layout>
<s:Button label="I'm a button"/>
<s:List id="list" dataProvider="{foo}" itemRenderer="TestRenderer" click="list1_clickHandler(event)">
<s:layout>
<s:TileLayout id="tileLayout" requestedColumnCount="2" />
</s:layout>
</s:List>
</s:Application>
Note that I'm doing some conversion between local and global coordinates. You need to do this convert the global coordinates in the MouseEvent
to the coordinate space of the List
. I've also added a Button
to the main app, solely to test this conversion ... the presence of the button makes it so the list's local coordinates do not match the global coordinates. If the list was placed at the origin (0,0) this conversion between coordinate spaces would not be necessary ... but that rarely happens in real world apps.