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

by Peter deHaan on October 31, 2009

in Scroller (Spark), VGroup (Spark), VerticalLayout (Spark), beta2

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 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".

<?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.

{ 2 comments… read them below or add one }

1 Mike October 31, 2009 at 2:11 pm

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?)

Reply

2 Peter deHaan October 31, 2009 at 4:52 pm

@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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

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

Next post: Setting the FormItem label width in a Form container in Flex