我在这个工程中,为了限制客户端访问网络,限制Image组件每次在set data完毕后都读取一个网络地址并验证其是否相同,我在本地的DataLocator中建立了一个本地头像缓存。在一个List中,我需要显示用户的在线列表,包括其头像,状态等。我使用一个自定义的ItemRenderer,其中包含Image等组件。但问题就出现了,无论是绑定函数返回Bitmap还是直接绑定ArrayCollection中的元素作为Image的source,在运行的时候,都只有最后一个Item的Image是显示正常的,我调试过发现其他的Image的source应该都set NULL。

由于始终查不到呈现器的工作流程,我也只好暴力点重写data这个setter方法。如果得到的数据不为null,则return,否则将data中的userImage(一个BitmapData类型)的数据读取出来,并新建一个Bitmap作为source的绑定源。

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="230" height="60" horizontalGap="0" verticalGap="0" horizontalAlign="center" verticalAlign="middle"> 
  3.     <mx:Script> 
  4.         <![CDATA[ 
  5.             import layer.DataLocator; 
  6.              
  7.             import mx.events.FlexEvent; 
  8.             [Bindable] 
  9.             private var bmp:Bitmap; 
  10.  
  11.  
  12.             override public function set data( value:Object ) : void { 
  13.                 if(value==null) return; 
  14.                 super.data = value;  
  15.                 bmp= new Bitmap(data.userImage); 
  16.                 if(data.isOnline) com_RenderBox.setStyle("backgroundColor",0x2e97ea); 
  17.                 else com_RenderBox.setStyle("backgroundColor",0x92a8b3); 
  18.             }  
  19.  
  20.         ]]> 
  21.     </mx:Script> 
  22.     <mx:HBox id="com_RenderBox" height="50" width="230" horizontalAlign="center"> 
  23.         <mx:Image id="com_UserHeadImg" source="{bmp}"   width="50" height="50" scaleContent="true" /> 
  24.         <mx:VBox id="com_FriendInfoBox" width="100%" height="50" verticalGap="0" horizontalGap="0" > 
  25.             <mx:Label id="com_FriendName" width="100%" height="20" text="{data.userName}" fontSize="14"/> 
  26.             <mx:Label id="com_FriendStateTxt" width="100%" height="18" text="{data.userCurStatue}" fontSize="11"  themeColor="#92A8B3" /> 
  27.         </mx:VBox> 
  28.     </mx:HBox> 
  29.      
  30.  
  31. </mx:HBox> 

这样就呈现正确了。

但是,如果我把source改成是source="{new Bitmap(data.userImage)}",或者是直接将data中的BitmapData更改为提供Bitmap而不在ItemRenderer里面新建一个Bimap,就依然出现上述问题。希望有人能解答一下。