JList类似于flex的dataGroup控件,可以自己定义循环的内容
构造函数为:
JList(listData:*=null, cellFactory:ListCellFactory=null)
listData
数据源,可以是一个VectorListModel或Array
cellFactory
列表的CellFactory,一般调用GeneralListCellFactory
GeneralListCellFactory的构造函数是:
GeneralListCellFactory(listCellClass:Class, shareCelles:Boolean=true, sameHeight:Boolean=true, height:int=22)
listCellClass
实现ListCell接口的渲染器类,类似于dataGroup的itemRenderer,需要实现4个接口方法:
function setCellValue(v:*):void -- 设置item的显示内容,这里的参数v即为该item对应的数据
function getCellValue():* -- 获取item的相关内容,一般简单返回数据内容即可
function setListCellStatus(list:JList, isSelected:Boolean, index:int):void -- 设置item选中/未选中的事件
function getCellComponent():Component -- 返回item对应的组件,这里的返回值就是最终的item呈现内容
shareCelles
是否共享格子内容,默认为true(共享),就是当有多项的时候,只加载可见区域里的内容,当用下拉框滚动内容时,用共享的格子再来加载下一部分内容;设置为false就是加载所有内容,这样加载时间会长一点,但是可以避免共享格子时带来的闪动
sameHeight
所有的格子是否拥有同样的高度,默认为true
height
格子的高度,只有当sameHeight设置为true时才有效,默认为22
JList有两个很有用的方法:setVisibleCellWidth(设置格子宽度)和setVisibleRowCount(设置可见行数)
package { import flash.display.Sprite; import org.aswing.AsWingManager; import org.aswing.FlowLayout; import org.aswing.GeneralListCellFactory; import org.aswing.JList; import org.aswing.JScrollPane; import org.aswing.JWindow; import org.aswing.VectorListModel; import org.aswing.border.LineBorder; public class Main extends Sprite { public function Main() { AsWingManager.initAsStandard(this); var model:VectorListModel = new VectorListModel(); for(var j:int=1;j<10;j++) { var item:MyItem = new MyItem("条目"+j,"pic00"+j+".jpg"); model.append(item); } var list:JList = new JList(model,new GeneralListCellFactory(MyRenderer,false,true,60)); list.setVisibleCellWidth(180); list.setVisibleRowCount(6); //容器 var window:JWindow = new JWindow(); window.setSizeWH(400,400); window.setLocationXY(100,100); window.setBorder(new LineBorder()); window.getContentPane().setLayout(new FlowLayout()); window.getContentPane().append(new JScrollPane(list)); window.show(); } } }
package { import flash.events.Event; import flash.net.URLRequest; import org.aswing.ASColor; import org.aswing.AssetPane; import org.aswing.Component; import org.aswing.FlowLayout; import org.aswing.JLabel; import org.aswing.JList; import org.aswing.JLoadPane; import org.aswing.JPanel; import org.aswing.ListCell; import org.aswing.geom.IntDimension; public class MyRenderer implements ListCell { private var value:*; private var panel:JPanel; private var label:JLabel; private var loader:JLoadPane; public function MyRenderer() { //构造函数做初始化工作 panel = new JPanel(new FlowLayout()); panel.setOpaque(true); panel.setBackground(ASColor.GRAY); //文字 label = new JLabel(); //图片 loader = new JLoadPane(); loader.setScaleMode(AssetPane.SCALE_FIT_PANE); loader.setPreferredSize(new IntDimension(60,60)); loader.addEventListener(Event.COMPLETE,onLoadComplete); panel.append(loader); panel.append(label); } private function onLoadComplete(event:Event):void { //这里必须要设置,不然加载的图片显示不正常 loader.doLayout(); } public function setCellValue(v:*):void { value = MyItem(v); //图片 loader.load(new URLRequest(value.getImage)); //文字 label.setText(value.getLabel); } public function getCellValue():* { return value; } public function setListCellStatus(list:JList, isSelected:Boolean, index:int):void { if(isSelected) { panel.setBackground(list.getSelectionBackground()); } else { panel.setBackground(list.getBackground()); } } public function getCellComponent():Component { return panel; } } }
package { public class MyItem { private var _label:String; private var _image:String; public function MyItem(label:String,image:String) { _label = label; _image = image; } public function toString():String { return "label:" + _label + ",image:" + _image; } /** * getter/setter */ public function get getLabel():String { return _label; } public function set setLabel(value:String):void { _label = value; } public function get getImage():String { return _image; } public function set setImage(value:String):void { _image = value; } } }