Multilined itemRenderer for a spark.components.List - with test case and screenshot

点点圈 提交于 2019-12-09 03:21:28

问题


In the simple Flex 4 web application below -

is it possible to change the custom item renderer MyRenderer, so that it wraps the too long lines?

TestApp.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">

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

            private static const MONTHS:ArrayList = new ArrayList([
                "1 January is a beautyful month", 
                "2 February is a 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"
            ]);
        ]]>

    </fx:Script>

    <s:List id="myList"
            width="60"
            dataProvider="{MONTHS}"
            itemRenderer="MyRenderer" /> 
</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 label(value:String):void 
            { 
                super.label = value; 
                labelDisplay.text = label; 

            // var list:List = owner as List;
            // if (list)
                // labelDisplay.width = list.width;

                if (label.indexOf("June") >= 0)
                    myColor = 0xFF0000;
                else if (label.indexOf("July") >= 0)
                    myColor = 0x00FF00;
                else if (label.indexOf("August") >= 0)
                    myColor = 0x0000FF;
                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" />
</s:ItemRenderer>

UPDATE 2:

I need same as in the Creating multi-line list rows with variable row heights blog - but for the spark.components.List which doesn't have the wordWrap property anymore.

UPDATE 3:

Actually after RIAStar answered my question (thanks!), my problems have just started - when I now call the myList.ensureIndexIsVisible(MONTHS.length - 1); it doesn't really scroll to the bottom.

I have prepared a new question: List with multilined (word wrapping) item renderer - how to scroll to the bottom? With test case and screenshots


回答1:


You can use VerticalLayout's variableRowHeight property for that. Something like this:

<s:List id="myList" width="60" dataProvider="{MONTHS}" itemRenderer="MyRenderer">
    <s:layout>
        <s:VerticalLayout variableRowHeight="true" 
                          horizontalAlign="justify" 
                          requestedMinRowCount="5"/>
    </s:layout>
</s:List>

Furthermore, the Label in your ItemRenderer can take up all the horizontal space it wants. You should constrain its width. For example:

<s:Label width="100%"/>


来源:https://stackoverflow.com/questions/15949378/multilined-itemrenderer-for-a-spark-components-list-with-test-case-and-screens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!