Naveen
Naveen

Reputation: 141

How to detect whether a list is scrolling or not?

Is there any way to detect whether a list is scrolling or not,likelist.isScrolling

Upvotes: 1

Views: 3141

Answers (4)

user1790998
user1790998

Reputation: 41

Or you can do somme thing like this in a list itemrenderer :

import spark.components.List;

[Bindable]
private var calcWidth:Number=195;   
private var listVerticalScroll:Boolean;
private var listHorizontalScroll:Boolean;

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
    var ownerVerticalScroll:Boolean=List(owner).scroller.verticalScrollBar.visible;
    var ownerHorizontalScroll:Boolean=List(owner).scroller.horizontalScrollBar.visible;
    if(ownerVerticalScroll!=listVerticalScroll){
        listVerticalScroll=ownerVerticalScroll;
        scrollBarChange()
    }

    super.updateDisplayList(unscaledWidth,unscaledHeight);
}

private function scrollBarChange():void {       
    if(listVerticalScroll){
        var newWidth:Number=195-(listVerticalScroll?15:0);
        calcWidth=newWidth;
    }
}

Upvotes: 4

Khaled Garbaya
Khaled Garbaya

Reputation: 1497

Or you can do it like this for spark component!

http://blog.flexexamples.com/2009/05/31/detecting-when-the-vertical-scroll-bar-is-scrolled-on-a-spark-list-control-in-flex-4/ -->

    <fx:Script>
        <![CDATA[
            import spark.components.VScrollBar;

            private function init():void {
                list.scroller.verticalScrollBar.addEventListener(Event.CHANGE, list_verticalScrollBar_change);
            }

            private function list_verticalScrollBar_change(evt:Event):void {
                var vsb:VScrollBar = evt.currentTarget as VScrollBar;
                var obj:Object = {};
                obj.type = evt.type;
                obj.val = vsb.value;
                obj.max = vsb.maximum;
                arrColl.addItem(obj);
                callLater(dgScroll);
            }

            private function dgScroll():void {
                dataGrid.verticalScrollPosition = dataGrid.maxVerticalScrollPosition;
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:ArrayCollection id="arrColl" />
    </fx:Declarations>

    <s:HGroup horizontalCenter="0" verticalCenter="0">
        <s:List id="list"
                creationComplete="init();">
            <s:layout>
                <s:VerticalLayout gap="0"
                        horizontalAlign="contentJustify"
                        requestedRowCount="4" />
            </s:layout>
            <s:dataProvider>
                <s:ArrayList>
                    <fx:String>The</fx:String>
                    <fx:String>Quick</fx:String>
                    <fx:String>Brown</fx:String>
                    <fx:String>Fox</fx:String>
                    <fx:String>Jumps</fx:String>
                    <fx:String>Over</fx:String>
                    <fx:String>The</fx:String>
                    <fx:String>Lazy</fx:String>
                    <fx:String>Dog</fx:String>
                </s:ArrayList>
            </s:dataProvider>
        </s:List>
        <mx:DataGrid id="dataGrid"
                dataProvider="{arrColl}"
                width="200"
                verticalScrollPolicy="on">
            <mx:columns>
                <mx:DataGridColumn dataField="type" />
                <mx:DataGridColumn dataField="val" />
                <mx:DataGridColumn dataField="max" />
            </mx:columns>
        </mx:DataGrid>
    </s:HGroup>

</s:Application>

Upvotes: 1

Brian Genisio
Brian Genisio

Reputation: 48137

So, @Khaled showed a way to do it with the MX component. If you are using the Spark component, that event doesn't work. Instead, you can listen to changes on myList.scroller.viewport.verticalScrollPosition or horizontalScrollPosition.

<fx:Declarations>
    <fx:int id="scrollingCount" />
</fx:Declarations>

<s:initialize>
    BindingUtils.bindSetter(function(x:*):void { scrollingCount++; }, myList.scroller.viewport, "verticalScrollPosition");
</s:initialize>

<s:VGroup>
    <s:Label text="Scrolling: {scrollingCount}" />
    <s:List id="myList" height="200" dataProvider="{myData}" />
</s:VGroup>

In neither of these cases do you get to know when the list stops getting scrolled (I'm not sure if you want it or not). You might have to set a timer and any time the timer goes off without any scrolling events, you are no longer scrolling?

Unfortunately, you haven't explained what you are trying to accomplish, wo we can't adequately answer your question.

Upvotes: 5

Khaled Garbaya
Khaled Garbaya

Reputation: 1497

you can use the ScrollEvent.SCROLL :

import mx.events.ScrollEvent

myList.addEventListener(ScrollEvent.SCROLL, scrollHandler);

function scrollHandler(e:ScrollEvent):void
{
//myList is scrolling
}

Upvotes: 2

Related Questions