0

我正在使用 Flex 4.6 AIR 应用程序。我有一个平铺列表,并且有一个图像作为 itemRenderer。当我搜索图像时,数据没有被过滤,而是选择了 tilelist 的索引(选定项目),我在 arrayCollection(dataProvider) 中设置了一个自定义属性,然后调用了 updateDisplayList,我在这个函数中启动了一个计时器。其中执行播放所选项目中的内部图像。

现在的问题是,如果我搜索该项目并单击未选择的项目,则计时器不会停止。我怎样才能做到这一点。

itemRenderer 的代码如下

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer 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="true" buttonMode="true"
                click="itemrenderer2_clickHandler(event)"
                doubleClick="itemrenderer1_doubleClickHandler(event)" doubleClickEnabled="true"
                rightClick="itemrenderer1_rightClickHandler(event)"
                rollOut="itemrenderer1_rollOutHandler(event)"
                rollOver="itemrenderer1_rollOverHandler(event)" useHandCursor="true">

    <fx:Script>
        <![CDATA[
            import customs.customcomponents.LibraryReDownloadComponent;
            import customs.customcomponents.VideoUploadBox;

            import dbconnection.Connection;

            import globalData.DataModel;

            import mx.collections.ArrayCollection;
            import mx.core.FlexGlobals;
            import mx.core.UIComponent;
            import mx.events.ItemClickEvent;
            import mx.managers.PopUpManager;
            import mx.utils.ObjectProxy;
            import mx.utils.UIDUtil;    

            private var isLoaded:Boolean = false;
            private var timer:Timer;
            private var rollOverFlag:Boolean = false;           
            private var countThumb:int = 0;
            private var arrThumnail:ArrayCollection;
            private var loaderThumb:Loader;     
            private var request:URLRequest;
            private var requestThumb:URLRequest;            
            private var loader:Loader;
            private var sqlStatStatus:SQLStatement;
            private var downloadVideoPath:String = "";
            private var videoUrlStream:URLStream;
            private var videoFileStream:FileStream;
            private var uploadvidbox:VideoUploadBox = new VideoUploadBox();
            private var dtStartVideo:Date;
            private var videoFileName:String = "";
            private var videoFile:File;
            private var IsRollOver:Boolean = false;                     

            [Bindable]
            private var modellocator:DataModel=DataModel.getInstance();

            private var connection:Connection = Connection.getInstance();

            override public function set data(value:Object):void
            {
                if(value != null)
                {
                    super.data = value;

                    if(timer != null)
                    {
                        timer.stop();
                        timer.removeEventListener(TimerEvent.TIMER, showFrame); 
                        timer = null;
                    }

                    loaderThumb = new Loader();                 
                    rollOverFlag = false;                   
                    countThumb = 0; 

                    if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                    {
                        requestThumb = new URLRequest("file://" + data.Videothumbnail);
                    }
                    else
                    {
                        requestThumb = new URLRequest(data.Videothumbnail);
                    }

                    loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                    loaderThumb.load(requestThumb); 

                    if(data.VideoBuyFlag == "yes")
                    {
                        if(data.VideoIsDeleted == "No")
                        {
                            bContain.setStyle("borderColor", "#00FF18");
                            bContain.alpha = 1;
                        }
                        else
                        {
                            bContain.setStyle("borderColor", "#888888");
                            bContain.alpha = 0.3;
                        }                                               
                    }
                    else
                    {
                        if(data.VideoIsDeleted == "No")
                        {
                            bContain.setStyle("borderColor", "#C50000");
                            bContain.alpha = 1;
                        }
                        else
                        {
                            bContain.setStyle("borderColor", "#888888");
                            bContain.alpha = 0.3;
                        }                                           
                    }

                    if(data.VideoStatus == "Active")
                    {
                        imgActive.source = "assets/btn_Active.jpg";
                        bContain.alpha = 1;
                    }
                    else
                    {
                        imgActive.source = "assets/btn_InActive.jpg";
                        bContain.alpha = 0.3;
                    }

                    arrThumnail = new ArrayCollection();

                    if(data.Videothumbdata.thumbimage != null)
                    {
                        if(data.Videothumbdata.thumbimage.source.item.length > 1)
                        {
                            for(var i:int = 0; i < data.Videothumbdata.thumbimage.source.item.length; i++)
                            {
                                arrThumnail.addItem(data.Videothumbdata.thumbimage.source.item[i]);
                            }
                        }
                        else
                        {
                            arrThumnail.addItem(data.Videothumbdata.thumbimage.source.item);                            
                        }
                    }                   
                }
            }           

            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
            {
                super.updateDisplayList(unscaledWidth, unscaledHeight);             
                /* trace(data.Videoname);
                trace("IsRollOver : " + IsRollOver.toString());
                trace("IsSearching : " + data.IsSearching.toString());
                trace("rollOverFlag : " + rollOverFlag.toString());  */         

                if(data.IsSearching == false)
                {
                    if(IsRollOver == false)
                    {                       
                        if(timer != null)
                        {
                            trace("inner");
                            loaderThumb = new Loader();
                            rollOverFlag = false;                           

                            if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                            {
                                requestThumb = new URLRequest("file://" + data.Videothumbnail);
                            }
                            else
                            {
                                requestThumb = new URLRequest(data.Videothumbnail);
                            }

                            loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                            loaderThumb.load(requestThumb);

                            timer.stop();
                            timer.removeEventListener(TimerEvent.TIMER, showFrame);                         
                            countThumb= 0;  
                            System.gc();
                            System.gc();
                        }                       
                    }
                }
                else
                {                   
                    if(rollOverFlag == false)
                    {
                        rollOverFlag = true;                        
                        trace("Hi");
                        timer = new Timer(1000);
                        timer.addEventListener(TimerEvent.TIMER, showFrame);
                        timer.start();
                    }
                    else
                    {
                        if(timer != null)
                        {
                            trace("SecondInner");
                            loaderThumb = new Loader();
                            rollOverFlag = false;                           

                            if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                            {
                                requestThumb = new URLRequest("file://" + data.Videothumbnail);
                            }
                            else
                            {
                                requestThumb = new URLRequest(data.Videothumbnail);
                            }

                            loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                            loaderThumb.load(requestThumb);

                            timer.stop();
                            timer.removeEventListener(TimerEvent.TIMER, showFrame);                         
                            countThumb= 0;  
                            System.gc();
                            System.gc();
                        }                       
                    }  
                }               
            }                   

            protected function showFrame(event:TimerEvent):void
            {
                var file:File;
                trace("hello");
                if(countThumb < arrThumnail.length)
                {
                    if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                    {
                        file = new File("file://" + arrThumnail[countThumb]);
                    }
                    else
                    {
                        file = new File(arrThumnail[countThumb]);
                    }

                    if(file.exists)
                    {
                        loader = new Loader();

                        if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                        {
                            request=new URLRequest("file://" + arrThumnail[countThumb]);
                        }
                        else
                        {
                            request=new URLRequest(arrThumnail[countThumb]);
                        }

                        loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
                        loader.load(request);

                        if(countThumb == (arrThumnail.length - 1))
                        {                   
                            countThumb = 0;
                        }
                        else
                        {
                            countThumb++;                   
                        }
                    }
                    else
                    {
                        countThumb++;
                    }
                }
                else
                {
                    countThumb = 0;
                }                               
            }

            protected function itemrenderer1_rollOverHandler(event:MouseEvent):void
            {           
                if(data.IsSearching == false)
                {
                    rollOverFlag = true;
                    IsRollOver = true;

                    if(arrThumnail != null && arrThumnail.length > 0)
                    {
                        timer = new Timer(1000);
                        timer.addEventListener(TimerEvent.TIMER, showFrame);
                        timer.start();                                  
                    }
                }               
            }

            private function onComplete(event:Event):void 
            {   
                if(rollOverFlag)
                {
                    imgThumb.source = loader;
                }               
            }

            private function onThumbComplete(event:Event):void
            {
                imgThumb.source = loaderThumb;              
            }

            protected function itemrenderer1_rollOutHandler(event:MouseEvent):void
            {           
                if(data.IsSearching == false)
                {
                    rollOverFlag = false;
                    IsRollOver = false;                 
                    imgThumb.source = loaderThumb;

                    if(timer != null)
                    {
                        timer.stop();
                        timer.removeEventListener(TimerEvent.TIMER, showFrame); 
                        timer = null;
                    }

                    countThumb = 0; 
                    System.gc();
                    System.gc();
                }               
            }       

            protected function itemrenderer2_clickHandler(event:MouseEvent):void
            {               
                for(var i:int=0; i < modellocator.libraryvideoac.length; i++)
                {                                       
                    modellocator.libraryvideoac[i].IsSearching = false;             
                }               

                parentDocument.parentDocument.txt_search.text = resourceManager.getString('languages','lblSearchText');             
                parentDocument.parentDocument.unCheckSelection();

                if(data.VideoIsDeleted == "Yes")
                {
                    var popupReDownload:LibraryReDownloadComponent = new LibraryReDownloadComponent();
                    popupReDownload = PopUpManager.createPopUp(UIComponent(this.parentApplication), LibraryReDownloadComponent, true) as LibraryReDownloadComponent;
                    popupReDownload.addEventListener("downloadMovie", redownloadMovie);
                    PopUpManager.centerPopUp(popupReDownload);
                }               
            }   

        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:NumberFormatter id="numFormat" precision="2"/>
    </fx:Declarations>

    <s:BorderContainer id="bContain" left="2" top="2" width="92" height="67" backgroundAlpha="1"
                       backgroundColor="#000000" borderColor="#030304" borderWeight="3">            
        <s:Image id="imgThumb" width="86" height="61" fillMode="scale" scaleMode="stretch"/>
        <s:Image id="imgActive" right="0" bottom="0" width="15" height="15" buttonMode="true"
                 useHandCursor="true"/>     
    </s:BorderContainer>    
</s:ItemRenderer>
4

1 回答 1

0

您在代码中的不同位置有计时器实例化。我不能确定实际的程序流程,但也许你两次实例化计时器?在这种情况下,程序将覆盖计时器变量,并失去对第一个计时器的引用。因此,当您调用 timer.stop() 时,您实际上只停止了一个计时器,而另一个则继续在后台运行。

你没有在这里设置弱引用属性:

timer.addEventListener(TimerEvent.TIMER, showFrame);

因此,如果您丢失了它的引用,计时器将不会被垃圾收集(即使您两次调用 System.gc() :-))。检查这种情况,并尝试添加一个监听器:

timer.addEventListener(TimerEvent.TIMER, showFrame, false, 0, true);
于 2013-08-27T21:59:51.767 回答