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 To download the latest nightly build of the Flex 4 SDK, see

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"?> 
<!-- -->
<s:Application name="Spark_VGroup_firstIndexInView_test"
            import spark.layouts.VerticalLayout;
            private function init():void {
                scrllr.verticalScrollBar.addEventListener("valueCommit", scrllrVSB_change);
            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 + ")";
    <s:Panel title="Spark Panel"
            horizontalCenter="0" verticalCenter="0">
        <s:Scroller id="scrllr"
                width="300" height="100"
            <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%" />
            <mx:Form styleName="plain" backgroundAlpha="0.0">
                <mx:FormItem label="firstIndexInView:">
                    <s:Label id="lbl1" />
                <mx:FormItem label="lastIndexInView:">
                    <s:Label id="lbl2" />

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.