我在这个工程中,为了限制客户端访问网络,限制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的绑定源。
- <?xml version="1.0" encoding="utf-8"?>
- <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="230" height="60" horizontalGap="0" verticalGap="0" horizontalAlign="center" verticalAlign="middle">
- <mx:Script>
- <![CDATA[
- import layer.DataLocator;
- import mx.events.FlexEvent;
- [Bindable]
- private var bmp:Bitmap;
- override public function set data( value:Object ) : void {
- if(value==null) return;
- super.data = value;
- bmp= new Bitmap(data.userImage);
- if(data.isOnline) com_RenderBox.setStyle("backgroundColor",0x2e97ea);
- else com_RenderBox.setStyle("backgroundColor",0x92a8b3);
- }
- ]]>
- </mx:Script>
- <mx:HBox id="com_RenderBox" height="50" width="230" horizontalAlign="center">
- <mx:Image id="com_UserHeadImg" source="{bmp}" width="50" height="50" scaleContent="true" />
- <mx:VBox id="com_FriendInfoBox" width="100%" height="50" verticalGap="0" horizontalGap="0" >
- <mx:Label id="com_FriendName" width="100%" height="20" text="{data.userName}" fontSize="14"/>
- <mx:Label id="com_FriendStateTxt" width="100%" height="18" text="{data.userCurStatue}" fontSize="11" themeColor="#92A8B3" />
- </mx:VBox>
- </mx:HBox>
- </mx:HBox>
这样就呈现正确了。
但是,如果我把source改成是source="{new Bitmap(data.userImage)}",或者是直接将data中的BitmapData更改为提供Bitmap而不在ItemRenderer里面新建一个Bimap,就依然出现上述问题。希望有人能解答一下。
转载于:https://blog.51cto.com/encorearon/434741