如何检测列表是否滚动?

时间:2011-06-13 09:25:36

标签: flex actionscript-3 flex4

有没有办法检测列表是否滚动,例如list.isScrolling

4 个答案:

答案 0 :(得分:5)

所以,@ Khaled展示了使用MX组件的方法。如果您使用Spark组件,则该事件不起作用。相反,您可以在myList.scroller.viewport.verticalScrollPositionhorizontalScrollPosition上收听更改。

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

在这两种情况下,你都不知道列表何时停止滚动(我不确定你是否想要它)。你可能需要设置一个计时器,任何时候计时器关闭而没有任何滚动事件,你不再滚动?

不幸的是,你没有解释你想要完成什么,我们无法充分回答你的问题。

答案 1 :(得分:4)

或者你可以在列表itemrenderer中做这样的somme:

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;
    }
}

答案 2 :(得分:2)

您可以使用ScrollEvent.SCROLL:

import mx.events.ScrollEvent

myList.addEventListener(ScrollEvent.SCROLL, scrollHandler);

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

答案 3 :(得分:1)

或者你可以这样做火花组件!

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

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