08
Nov
07

Scrolling to a specific index in a Flex List control

The following example shows how you can scroll to a specific item index in Flex by using the scrollToIndex() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/08/scrolling-to-a-specific-index-in-a-flex-list-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            private var arrColl:ArrayCollection;

            private function init():void {
                var arr:Array = [];
                var n:uint = 50;
                var i:uint;
                for (i = 0; i < n; i++) {
                    arr.push({label:"item " + i, data:i});
                }
                arrColl = new ArrayCollection(arr);
                list.dataProvider = arrColl;
                numericStepper.maximum = arrColl.length - 1;
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="scrollToIndex(n):">
                <mx:NumericStepper id="numericStepper"
                        change="list.scrollToIndex(event.value);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:List id="list"
            rowCount="5"
            width="100" />

</mx:Application>

View source is enabled in the following example.


2 Responses to “Scrolling to a specific index in a Flex List control”


  1. 1 Nate Nov 9th, 2007 at 10:18 am

    Is there anyway to make it so the list kinda “iPhone” like scrolls to the selected index? I have no idea how to do this, but it would be cool :)

  2. 2 List of Nov 18th, 2007 at 5:42 pm

    Sweet, I have been looking for some way to do this.
    Helpful post, thanks.

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".