0

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

在此处输入图像描述

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

在此处输入图像描述

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]),但没有帮助。

4

1 回答 1

1

自己在 Flexponential 博客滚动到火花列表底部找到了答案:

    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;
        }
    }           
于 2013-04-17T05:58:04.073 回答