flex 4 list ItemRenderer: how can i have different itemrenderers for different rows?

左心房为你撑大大i 提交于 2019-12-04 19:41:09

The spark List control that comes with Flex 4 allows you to assign a different itemRenderer depending on some logic.

You can create a custom item renderer function by setting the itemRendererFunction property.

    <fx:Script>
    <![CDATA[
        import renderers.*;

        import mx.core.ClassFactory;
        import spark.skins.default.DefaultItemRenderer;

        private function list_itemRendererFunc(item:Object):ClassFactory {
            var cla:Class = DefaultItemRenderer;
            switch (item.type) {
                case "employee":
                    cla = EmployeeItemRenderer;
                    break;
                case "manager":
                    cla = ManagerItemRenderer;
                    break;
                default:
                    break;
            }
            return new ClassFactory(cla);
        }
    ]]>
</fx:Script>

<s:List id="list"
        labelField="name"
        itemRendererFunction="list_itemRendererFunc"
        horizontalCenter="0"
        verticalCenter="0">

If the different data is embedded in the corresponding row of the dataProvider, you can check for the data in the overriden public set data method and set the itemRenderer's background color accordingly. If you need more controls in that particular row, you can use states. Just set this.currentState = "currentUser";

override public function set data(item:Object):void
{
  if(item.user == SomeGlobal.currentUser)//or outerDocument.currentUser
  {
    this.currentState = "currentUser";
  }
  else
  {
    //reset to default state, coz item renderers are reused
    this.currentState = "";
  } 
}

If you aren't familiar with states, there are lot of examples out there on using states in Flex

You could build a component to be the itemrenderer and have it provide different views depending on the data in the row using states - switching to the approriate state on creation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!