Determining the first and last visible item in a scrolling VGroup container in Flex 4

The following example shows how you can determine the first and last item in a Spark VGroup container’s view by using the firstIndexInView and lastIndexInView properties on the VGroup container’s layout property.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?> 
<!-- http://blog.flexexamples.com/2009/10/30/determining-the-first-and-last-visible-item-in-a-scrolling-vgroup-container-in-flex-4/ -->
<s:Application name="Spark_VGroup_firstIndexInView_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo"> 
 
    <fx:Script>
        <![CDATA[
            import spark.layouts.VerticalLayout;
 
            private function init():void {
                scrllr.verticalScrollBar.addEventListener("valueCommit", scrllrVSB_change);
                scrllrVSB_change(null);
            }
 
            private function scrllrVSB_change(evt:Event):void {
                var fIndex:int = VerticalLayout(vgr.layout).firstIndexInView;
                var fObj:Object = vgr.getElementAt(fIndex);
                var lIndex:int = VerticalLayout(vgr.layout).lastIndexInView;
                var lObj:Object = vgr.getElementAt(lIndex);
 
                lbl1.text = fIndex.toString() + " (" + fObj.label + ")";
                lbl2.text = lIndex.toString() + " (" + lObj.label + ")";
            }
        ]]>
    </fx:Script>
 
    <s:Panel title="Spark Panel"
            horizontalCenter="0" verticalCenter="0">
        <s:Scroller id="scrllr"
                width="300" height="100"
                creationComplete="init();">
            <s:VGroup id="vgr"
                    paddingLeft="5" paddingRight="5" paddingTop="5" paddingBottom="5">
                <s:Button id="ch1" label="One" width="100%" />
                <s:Button id="ch2" label="Two" width="100%" />
                <s:Button id="ch3" label="Three" width="100%" />
                <s:Button id="ch4" label="Four" width="100%" />
                <s:Button id="ch5" label="Five" width="100%" />
                <s:Button id="ch6" label="Six" width="100%" />
                <s:Button id="ch7" label="Seven" width="100%" />
                <s:Button id="ch8" label="Eight" width="100%" />
                <s:Button id="ch9" label="Nine" width="100%" />
            </s:VGroup>
        </s:Scroller>
        <s:controlBarContent>
            <mx:Form styleName="plain" backgroundAlpha="0.0">
                <mx:FormItem label="firstIndexInView:">
                    <s:Label id="lbl1" />
                </mx:FormItem>
                <mx:FormItem label="lastIndexInView:">
                    <s:Label id="lbl2" />
                </mx:FormItem>
            </mx:Form>
        </s:controlBarContent>
    </s:Panel>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

4 thoughts on “Determining the first and last visible item in a scrolling VGroup container in Flex 4

  1. hi , nice script… :)
    I’d like to know what Script-Highlighter you’ve used to display the MXML scripts?

    thanks in advance :)

  2. Hm seems I found my answer.. The list has a “visibleData” array containing the itemRenderers of the visible items. :)

Comments are closed.