ItemRenderer切换URLLoader图像

时间:2011-09-07 04:53:08

标签: flex actionscript-3 air blackberry-playbook

我甚至不知道如何解释这种行为,但我会尝试。我正在从需要基本身份验证的外部URL加载图像,因此我使用URLLoader从唯一ID加载图像。 ID将传递给itemrenderer,然后继续加载图像。但是当我滚动时,图像会自行切换。如果我加载的图像超过7张左右,它就开始重复图像......

Youtube错误视频: http://www.youtube.com/watch?v=ZYoqlS14gWQ

相关代码:

<s:ItemRenderer name="RandomItemRenderer" creationComplete="init();"
            xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx" 
            autoDrawBackground="false">
<s:states>
    <s:State name="normal" />
    <s:State name="hovered" />
    <s:State name="selected" />
</s:states>

<fx:Script>
    <![CDATA[
        import flash.net.URLLoader;
        import flash.net.URLLoaderDataFormat;
        import flash.net.URLRequest;
        import flash.net.URLRequestHeader;
        import flash.net.URLRequestMethod;

        import mx.utils.ObjectProxy;

        import customclasses.Settings;

        [Bindable] private var coverArtImage:Image;
        private var myCoverArtLoader:URLLoader;

        [Bindable] private var coverArtSource:String;

        private function init():void {
            get_coverArt();
        }

        private function get_coverArt(): void {
            if (!data.coverArt) {
                set_nullCoverArt();
            } else {
                var requestString:String = "/rest/getCoverArt.view?v=1.5.0&c=AirSub&id=" + data.coverArt;
                var requestURL:String = Settings.ServerURL + requestString;

                myCoverArtLoader = new URLLoader();
                var myRequest:URLRequest = new URLRequest();

                var authHeader:URLRequestHeader = new URLRequestHeader();
                authHeader.name = 'Authorization';
                authHeader.value = 'Basic ' + Settings.EncryptedCreds();

                myRequest.requestHeaders.push(authHeader);
                myRequest.url = requestURL;
                myRequest.method = URLRequestMethod.GET;
                myCoverArtLoader.dataFormat = URLLoaderDataFormat.BINARY;

                myCoverArtLoader.addEventListener(Event.COMPLETE, set_coverArt);
                myCoverArtLoader.addEventListener(IOErrorEvent.IO_ERROR, set_failedCoverArt);
                myCoverArtLoader.load(myRequest);
            }
        }

        private function set_coverArt(evt:Event) : void {
            coverArtImage = new Image();
            coverArtImage.source = myCoverArtLoader.data;           
            myCoverArtLoader.removeEventListener(Event.COMPLETE, set_coverArt);
        }

        private function set_nullCoverArt() : void {
            coverArtImage = new Image();
            coverArtImage.source = "assets/nullCoverArt.jpg";
        }

        private function set_failedCoverArt() : void {
            coverArtImage = new Image();
            coverArtImage.source = "assets/nullCoverArt.jpg";
            myCoverArtLoader.addEventListener(IOErrorEvent.IO_ERROR, set_nullCoverArt);
        }

    ]]>
</fx:Script>

<s:Image source.normal="assets/coverOutline.png" source.selected="assets/coverOutlineYellow.png" source.hovered="assets/coverOutlineYellow.png"
         height="226" width="226" />

<s:VGroup top="4.5" bottom="5" width="200" horizontalAlign="center" letterSpacing="10"
          paddingBottom="5" paddingTop="9" verticalAlign="middle" x="13.5">
    <s:Image id="ui_imgCoverArt" width="200" height="200" source="{coverArtImage.source}"/>
    <s:Label text="{data.title}" width="160" styleName="RandomList" />
</s:VGroup>

1 个答案:

答案 0 :(得分:2)

ItemRenderers是可重用和缓存的,即在List中创建的数量有限,无法填充其区域(rowCount + - couple)。当您滚动时,新的渲染器不会被实例化,而是滚动的一个渲染器上升或下降并填充新数据。

这就是为什么你不能依赖creationComplete事件,它只会为每个渲染器实例触发一次。

解决方案是覆盖data setter并在那里构建所需的行为:

override public function set data(value:Object):void
{
    super.data = value;
    get_coverArt();
}

有用的链接:How flex itemRenderer works ? (their life cycle)