In a previous example, “Determining the first and last visible item in a scrolling VGroup container in Flex 4″, we saw how you could 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.

The following example shows how you can determine how much of an item is visible in a scrolling VGroup container in Flex 4 by using the fractionOfElementInView() method.

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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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/31/determining-how-much-of-an-item-is-visible-in-a-scrolling-vgroup-container-in-flex-4/ -->
<s:Application name="Spark_VGroup_layout_fractionOfElementInView_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 vLay:VerticalLayout = vgr.layout as VerticalLayout;
                var fIndex:int = vLay.firstIndexInView;
                var fObj:Object = vgr.getElementAt(fIndex);
                var lIndex:int = vLay.lastIndexInView;
                var lObj:Object = vgr.getElementAt(lIndex);
 
                lbl1.text = vLay.fractionOfElementInView(fIndex).toFixed(2) + " (" + fObj.label + ")";
                lbl2.text = vLay.fractionOfElementInView(lIndex).toFixed(2) + " (" + 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.

 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Determining how much of an item is visible in a scrolling VGroup container in Flex 4

  1. Mike says:

    Is it not
    var fObj:Object = vgr.getElementAt(fIndex);
    rather than
    var fObj:Object = vgr.layout (fIndex);

    (p.s. are you just using FB Beta 2, or newer SDK than that has?)

    • Peter deHaan says:

      @Mike,

      Thanks. Looks like I had a copy/paste error when I copied this from Flash Builder.
      For these examples I almost always use the latest nightly Flex SDK available, so I don’t know if these would compile with the older Beta2 SDK or if you would generally need to download the latest beta SDK.

      Peter

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree