带有多行(自动换行)项目渲染器的列表 - 如何滚动到底部?有测试用例和截图

时间:2013-04-16 18:51:11

标签: actionscript-3 flex flex4.5 itemrenderer flex4.7

在Flex 4 Web应用程序中,我尝试使用spark.components.List进行聊天(出于各种原因 - 它已经在我的Flex移动应用程序中运行良好),但因为我使用了一个项目渲染器,它可以是多线的(即包裹太长的线)我有问题,我不能通过调用它的ensureIndexIsVisible方法将列表滚动到底部:

enter image description here

我准备了一个非常简单的测试。这些只是2个文件,当您将它们放入Flash Builder中的新Flex项目时,它将立即生效 -

enter image description here

myApp.mxml在

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"
    applicationComplete="init()">

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

            private static const MONTHS:ArrayList = new ArrayList([
                "1 January is a beautyful month", 
                "2 February is a very very very very very very very very very very very very very very very beautyful month", 
                "3 March is a beautyful month", 
                "4 April is a beautyful month", 
                "5 May is a beautyful month", 
                "6 June is a beautyful month", 
                "7 July is a beautyful month", 
                "8 August is a beautyful month", 
                "9 September is a beautyful month", 
                "10 October is a beautyful month", 
                "11 November is a beautyful month",
                "12 December is a beautyful month"
            ]);

            private function init():void {
                myList.ensureIndexIsVisible(MONTHS.length - 1); 
            }
        ]]>

    </fx:Script>

    <s:List id="myList"
            horizontalCenter="0"
            verticalCenter="0"
            width="100"
            height="300"
            dataProvider="{MONTHS}"
            itemRenderer="MyRenderer"
            horizontalScrollPolicy="off">

        <s:layout>
            <s:VerticalLayout variableRowHeight="true" 
                              horizontalAlign="justify"/>
        </s:layout>
    </s:List>
</s:Application>

MyRenderer.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns:s="library://ns.adobe.com/flex/spark"
    autoDrawBackground="false">

    <fx:Script> 
        <![CDATA[    
            [Bindable]
            public var myColor:uint = 0xFFFFFF;

            override public function set data(value:Object):void {
                var label:String = value as String;
                labelDisplay.text = label; 

                if (label.indexOf("June") >= 0)
                    myColor = 0xFFEEEE;
                else if (label.indexOf("July") >= 0)
                    myColor = 0xEEFFEE;
                else if (label.indexOf("August") >= 0)
                    myColor = 0xEEEEFF;
                else 
                    myColor = 0xFFFFFF;
            } 
        ]]> 
    </fx:Script>

    <s:Rect width="100%" height="100%">
        <s:fill>
            <s:SolidColor color="{myColor}" />
        </s:fill>
    </s:Rect>

    <s:Label id="labelDisplay" 
             width="100%"
             left="4" 
             top="4" />
</s:ItemRenderer>

请看一下并告诉我如何将列表滚动到最底部。

我应该以某种方式使用myList.dataGroup吗?或者是layout.verticalScrollPosition

我也试过callLater(myList.ensureIndexIsVisible, [MONTHS.length - 1]),但这没有用。

1 个答案:

答案 0 :(得分:1)

我在Flexponential博客Scrolling to the bottom of a spark List找到了答案:

    public static function scrollToBottom(list:List):void {
        // update the verticalScrollPosition to the end of the List
        // virtual layout may require us to validate a few times
        var delta:Number = 0;
        var count:int = 0;

        while (count++ < 10) {
            list.validateNow();
            delta = list.layout.getVerticalScrollPositionDelta(NavigationUnit.END);
            list.layout.verticalScrollPosition += delta;

            if (delta == 0)
                break;
        }
    }