Flex List选择和渲染问题

时间:2011-06-15 00:31:53

标签: flex list rendering

我的自定义列表存在一些奇怪的问题。

我定义了需要显示员工列表的列表。在我的自定义列表中,我想提供selectedId属性,以便选择(突出显示)具有具体ID的员工。 列表是根据[http://blog.flexdevelopers.com/2010/02/flex-examples-binding-value-to-combobox.html]创建的。

这是我自定义列表的代码......

<mx:List ....>
<mx:Script>
<![CDATA[

private var _selectedId:int;

public function get selectedId():int
{
    //...
}
public function set selectedId(id:int):void
{
    _selectedId=id;
    invalidateProperties();
    invalidateDisplayList();
}
override protected function commitProperties():void
{
    super.commitProperties();
    if(dataProvider!=null)
        setSelectedId();    
}
protected function setSelectedId():void
{
    var subordinates:ArrayCollection=dataProvider as ArrayCollection;
    var subordinate:EmployeeSimpleDTO=null;
    for(var i:int=0;i<subordinates.length;i++)
    {
        var sub:EmployeeSimpleDTO=(subordinates.getItemAt(i))as EmployeeSimpleDTO;
        if(sub.employeeId==_selectedId)
        {
            subordinate=sub;
        _selectedId=sub.employeeId;
        break;
        }
    }               
    selectedItem=subordinate;   
}
override protected function collectionChangeHandler(event:Event):void
{
    super.collectionChangeHandler(event);
    invalidateProperties();
    invalidateDisplayList();
}]]>
<mx:Script>
<mx:itemRenderer>
    <mx:Component>
        <view:SubordinatesListItem employeeId       = "{data.employeeId}"
                                   employeeName     = "{data.employeeName}"
                                   employeeSurname  = "{data.employeeSurname}"
                                   employeeUsername = "{data.employeeUsername}"/>
    </mx:Component>
</mx:itemRenderer>
</mx:List>

修改
这是我的项目渲染器的代码

<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%">
<mx:Script>
    <![CDATA[

        [Embed(source="/assets/employeeIcon.png")]
        [Bindable]
        private var employeeIcon:Class;

    [Embed(source="/assets/infoIcon.png")]
        [Bindable]
        private var infoIcon:Class;


        //-----------------------------
        // private var declarations
        //-----------------------------

        private var _employeeId:int;

        public function get employeeId():int
        {
            return _employeeId;
        }
        public function set employeeId(id:int):void
        {
            _employeeId=id;
        }

    private var _employeeName:String;

    [Bindable]
        public function get employeeName():String
        {
            return _employeeName;
        }
        public function set employeeName(name:String):void
        {
            _employeeName=name;
        }

    private var _employeeSurname:String;

    [Bindable]
        public function get employeeSurname():String
        {
            return _employeeSurname;
        }
        public function set employeeSurname(surname:String):void
        {
            _employeeSurname=surname;
        }

    private var _employeeUsername:String;

    [Bindable]
        public function get employeeUsername():String
        {
            return _employeeUsername;
        }
        public function set employeeUsername(username:String):void
        {
            _employeeUsername=username;
        }           

    private function dispatchShowSubordinateDetails():void
    {
    dispatchEvent(new Event("showSubordinateDetails",true));
        }           

  ]]>
</mx:Script>
<mx:Metadata>
    [Event(name="showSubordinateDetails", type="flash.events.Event")]
</mx:Metadata>
<mx:Image source="{employeeIcon}" scaleX="0.35" scaleY="0.35"/>
<mx:Label text="{employeeName} {employeeSurname}"/>
<mx:Label text=" [{employeeUsername}]"  color="#8D8D8D"/>
<mx:Spacer width="100%"/>
<mx:LinkButton icon="{infoIcon}" click="dispatchShowSubordinateDetails()" width="30"/>
</mx:HBox>

在我的代码中,我在自定义列表的selectedId属性上绑定了一些值(列表放在popup - TitleWindow中)。

现在,仅在我的自定义列表的第一次显示 员工,其ID与selectedId相同,未在视觉上选择(彩色)< / strong>在列表中。当我直观地说,我的意思是当我获取自定义列表的selectedItem或selectedIndex属性时,我得到正确的对象和索引。

更多,在关键的首次亮相时,当我将鼠标悬停在应该选择的项目上时,它会在我推出后保持选中状态(应该是这样)。

随后的节目表现符合预期。

我试图对我的问题进行“塑料”描述,所以请原谅我,如果我提供的信息很少。我只是不明白问题所在。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最后,我发现了我的错误。

因为我的自定义列表在父组件的表单内,所以我创建了在弹出显示和绑定发生之前重置所有表单元素的函数。在该代码中,我放了myCustomList.selectedItem=null,当我删除该行时,一切都按预期工作。

不过,我不确定早先行为的原因。