其实看了这个我们并没有求甚解,要知道授人予鱼,不如授人予渔。我们再来看下为什么通过cellrenderer可以给List、DataGrid、Tree 和 Menu 组件以及其它列表组件增强单元格内容显示。
List 类由行构成。这些行显示滑过和选区突出显示,用作行选区的点击状态,并在滚动中扮演重要的角色。除了选区突出显示和图标(如节点图标和 Tree 组件的展开箭头)之外,行还包含一个单元格(或者,如果是 DataGrid,则包含多个单元格)。在默认情况下,这些单元格是实现 CellRenderer API 的 TextField 对象。但是,您可以让 List 使用不同的组件类作为每一行的单元格。唯一的要求是该类必须实现 List 用于与单元格通信的 CellRenderer API。
List 类使用一种非常复杂的算法进行滚动。一个列表只会列出它一次能显示的最多行数,超出 rowCount 属性的值的项目根本不会获得行。在列表滚动时,它会将所有行上下移动(取决于滚动方向)。然后,列表将重复使用滚出视图的行;列表会重新初始化这些行,并使用它们作为正在滚入视图的新行,方法是将旧行的值设置为视图中的新行,然后将旧行移到新项目滚入视图的位置。
function createChildren(Void) : Void {
label = createObject("Label", "label", 1, { styleName:this, owner:this });
label.html = true;
size();
}
// setSize is implemented by UIComponent and calls size(), after setting
// __width and __height
function size(Void) : Void {
label.setSize(__width, __height);
// make sure the label field is in the top-left corner
// of the row
label._x = 0;
label._y = 0;
}
function setValue(str:String, item:Object, sel:Boolean) : Void {
// hide the label if no data to display
label._visible = (item!=undefined);
// this line actually sets htmlText
label.text = item.label;
}
function getPreferredHeight(Void) : Number {
// this is the height with the default font, you might
// need to adjust this to suit your needs
return 18;
}
function getPreferredWidth(Void) : Number {
// default to the width of the listbox
return __width;
}
其中用来显示html标记的就是setValue方法了。
我们定义了这个类后,剩下就是对List组件来设置新的单元格渲染器了。使用下面的代码:
1 : //LabelCellRenderer就是指定单元格渲染的连接ID
2 : myList.cellRenderer = "LabelCellRenderer";
也许看完这些,你可以做出更好效果的渲染器来。要注意的是,只有flashMX2004的List组件才支持。