0

is there an built in method in spark tile list to get an item from a given point?

Thanks

zero323
  • 322,348
  • 103
  • 959
  • 935
Eran
  • 1,628
  • 16
  • 31
  • What do you mean by "Point"? Are you referring to x,y coordinates? Or something else? As far as I know, there is no method to get an item based on x,y coordinates. You could review the ASDocs and the source code, though. – JeffryHouser Jan 01 '13 at 15:59

1 Answers1

1

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.

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • 1
    I don't think you _have_ to extend the class in order to access a namespaced method. Just importing the `mx_internal` namespace will do. – RIAstar Jan 02 '13 at 14:27
  • You are so right sir, as usual! Editing my answer to remove the unnecessary class :) – Sunil D. Jan 02 '13 at 16:30
  • Thanks Sunil , works great , wonder why they named it getElementNearestScrollPosition, its more like getElementUnderPoint(p:Point) . – Eran Jan 03 '13 at 15:24