欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

对itemrenderer的一些认知(2)-与父容器的交互

程序员文章站 2022-07-07 19:37:21
...
在编写itemrenderer时,会有时候用到与父容器的交互的需求,实现的方法不难

比如在一个datagrid里,相邻的行之前的数据属性有联系,这里给一个例子是上一行的背景颜色是下一行的决定的:


override public function set data(value:Object):void {
super.data = value;
this.dataProvider = null;

//在data setter里调用自己写的方法
checkActionSelect();
}

public function checkActionSelect():void{

 currentOwner = new DataGrid();
//获取父容器,这里是DataGrid
currentOwner = owner as DataGrid;
//取出父容器的dataProvider,这里是arraycollection
currentOwnerDataProvider = currentOwner.dataProvider as ArrayCollection;
//取出这行data在父容器的dataprovider里的index
index = currentOwnerDataProvider.getItemIndex(data);

//这里就可以得到下一行的数据了,index + 1就行
var obj:Object = currentOwnerDataProvider.getItemAt(index + 1);

//根据下一行的数据来改变颜色
if(obj.selectedAction == "Approved"){
this.setStyle("chromeColor", "green");
}else if(obj.selectedAction == "Rejected"){
this.setStyle("chromeColor", "red");
}else this.setStyle("chromeColor", "white");
}





另外再提一下validateNow()这个方法,如果想要重画这个itemrenderer,可以override这个方法,当然你也可以用invalidate系列方法去做,但如果你不是对这个itemrenderer掌控那么细致,validateNow()就相对方便,比如:


override public function validateNow():void {
super.validateNow();
this.y += 7;
this.x += 5;
this.backgroundColor = 0xcccccc;
this.wordWrap = true;
this.height = 40;
this.width = 9
this.toolTip = labelStr;
this.setStyle("textAlign", "left");
this.alpha = 0.8;
}


但是也不要过度使用,本来itemrenderer就是一个会拖performance的东西,用的不谨慎甚至会stack overflow.