Flex DataGrid Itemrenderer 图像不显示
所有,我有一个数据网格,其中一个项目渲染器分配给一个货币列(字符串)的列。渲染器的目的是显示货币的标志,例如;对于美元,它应该显示美元旗帜图像等。目前该列显示为空白,没有图像。我有以下渲染器(它扩展了 UIComponent)。我在 commitProperties() 方法中动态加载图像。目前,我已将其硬编码到美元映像以使其正常工作 - 但没有运气。任何帮助将不胜感激。
public class CenteredEmbedImage extends UIComponent implements IListItemRenderer,IDropInListItemRenderer
{
private var _loader:Loader;
private var _img:Image;
public function CenteredEmbedImage()
{
super();
}
private var _data:Object;
[Bindable("dataChange")]
[Inspectable(environment="none")]
public function get data():Object
{
return _data;
}
public function set data(value:Object):void
{
var newText:*;
_data = value;
invalidateProperties();
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
private var _listData:BaseListData;
[Bindable("dataChange")]
[Inspectable(environment="none")]
public function get listData():BaseListData
{
return _listData;
}
public function set listData(value:BaseListData):void
{
_listData = value;
}
override protected function commitProperties():void
{
super.commitProperties();
if (listData)
{
// remove the old child if we have one
if (_img)
removeChild(_img);
_img= new Image();
//source code of the second way
_loader = new Loader();
//notice: NOT _loader.addEventListener,is _loader.contentLoaderInfo.addEventListener
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{_img.source = e.currentTarget.content;});
_loader.load(new URLRequest(encodeURI("assets/images/flags/usd.gif")));
addChild(_img);
}
}
override protected function measure():void
{
super.measure();
if (_img)
{
measuredHeight = _img.height;
measuredWidth = _img.width;
}
}
override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
if (_img)
{
_img.x = (w - _img.width) / 2;
}
}
}
}
All, I have a Datagrid with an ItemRenderer assigned to a column which is a Currency column(String). The renderer is mean to display the Flag of the currency eg; for USD it should display a USD flag image etc. At the moment the column is appearing Blank without an image. I have the following renderer (which extends UIComponent). I am dynamically loading the images in the commitProperties() method. At the moment I have hard-coded it to the USD image to get it to work - but no luck. Any help would be greatly appreaciated.
public class CenteredEmbedImage extends UIComponent implements IListItemRenderer,IDropInListItemRenderer
{
private var _loader:Loader;
private var _img:Image;
public function CenteredEmbedImage()
{
super();
}
private var _data:Object;
[Bindable("dataChange")]
[Inspectable(environment="none")]
public function get data():Object
{
return _data;
}
public function set data(value:Object):void
{
var newText:*;
_data = value;
invalidateProperties();
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
private var _listData:BaseListData;
[Bindable("dataChange")]
[Inspectable(environment="none")]
public function get listData():BaseListData
{
return _listData;
}
public function set listData(value:BaseListData):void
{
_listData = value;
}
override protected function commitProperties():void
{
super.commitProperties();
if (listData)
{
// remove the old child if we have one
if (_img)
removeChild(_img);
_img= new Image();
//source code of the second way
_loader = new Loader();
//notice: NOT _loader.addEventListener,is _loader.contentLoaderInfo.addEventListener
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{_img.source = e.currentTarget.content;});
_loader.load(new URLRequest(encodeURI("assets/images/flags/usd.gif")));
addChild(_img);
}
}
override protected function measure():void
{
super.measure();
if (_img)
{
measuredHeight = _img.height;
measuredWidth = _img.width;
}
}
override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
if (_img)
{
_img.x = (w - _img.width) / 2;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你做错了很多事情。首先,不要每次都删除并重新创建图像,而是在 createChildren() 方法中创建一次,然后仅更改源属性。其次,我没有看到您在任何地方设置图像的高度或宽度。请务必执行此操作,通常是在 updateDisplayList 中。第三,在测量方法中,我建议使用图像的测量高度和测量宽度设置测量高度和测量宽度。我通常使用 getExplicitOrMeasuredHeight 和 getExplicitOrMeasuredWidth 方法。
第四,为什么要使用 URL 加载器?只需使用图像标签并设置源即可。
这不是经过测试的代码,但我可能会像这样更改您的 itemRenderer:
通过创建扩展图像类的 itemRenderer ,您很有可能使您的生活变得更加轻松。像这样的东西:
It looks like you're doing a lot of things wrong. First, don't remove and re-create the image each time, create it once in the createChildren() method and just change the source property. Second, I don't see you setting the height or width of the image anywhere. Be sure to do that, usually in updateDisplayList. Third, in the measure method I would recommend setting measuredHeight and measuredWidth with the measuredHeight and measuredWidth of the image. I usually use the getExplicitOrMeasuredHeight and getExplicitOrMeasuredWidth methods.
Fourth why are you using a URL Loader? Just use the Image tag and set the source.
This isn't tested code, but I might change your itemRenderer something like this:
There is a good chance you could make your life a lot easier by just creating an itemRenderer that extended the image class. Something like this: