添加和删​​除列表中的项目Binded Index焦点不正确

时间:2012-11-20 08:42:02

标签: actionscript-3 flex flex3

我有一个列表中的dataProvider。该列表具有来自var -selectedDamageIdx的selecteIndex Binded。我正在改变selectedDamageIdx或多或少,同时我在列表中移动项目arround。当一切都结束时,索引不正确,但selectedDamageIdx上的值是正确的。有什么建议?我确保selectedDamageIdx是公共的和可绑定的。绑定以mxml完成​​。

我的清单是损坏清单。它使用了一个伤害itemRenderer和一个伤害对象。基本上,我正在做的是:我有一个与列表具有相同信息的拇指。当我在缩略图中拖动某些东西来移动项目时,我希望列表更新项目位置。我有一个名为onChangePosition的函数,如下所示,我可以访问_from和_to位置。 所以我做了以下几点:

if(_to > _from)     
{
    (damages.dataProvider as ArrayCollection).addItemAt(damage,_to);
    (damages.dataProvider as ArrayCollection).removeItemAt(_from);
     selectedDamageIdx = _to-1;//because of shift all the items between the _from and _to will move back one position. And the moved item will actually get the index to-1
}

else if (_from > _to)
{
    (damages.dataProvider as ArrayCollection).removeItemAt(_from);
    (damages.dataProvider as ArrayCollection).addItemAt(damage,_to);
    selectedDamageIdx = _to;
}

这在缩略图上工作正常,selectedDamageIdx获得预期值。但是,我认为由于我的添加和删除操作,列表选择了错误的项目,并且selectedIndex是错误的。  在第一种情况下_to> _from列表中的selectedIndex比它应该少一个。  在第二种情况下_to< _from selectedIndex比它应该多一个。

1 个答案:

答案 0 :(得分:0)

好的,我找到的解决方案如下:

if(_to > _from)     
{
    (damages.dataProvider as ArrayCollection).addItemAt(damage,_to);
    damages.addEventListener(FlexEvent.UPDATE_COMPLETE,updateIndex);
    (damages.dataProvider as ArrayCollection).removeItemAt(_from);    
}
else if (_from > _to)
{
    (damages.dataProvider as ArrayCollection).removeItemAt(_from);
    damages.addEventListener(FlexEvent.UPDATE_COMPLETE,updateIndex);
    (damages.dataProvider as ArrayCollection).addItemAt(damage,_to);
}

然后在updateIndex:

  damages.removeEventListener(FlexEvent.UPDATE_COMPLETE,updateIndex);
    if(_to > _from)     
        selectedDamageIdx = _to-1;    
    else if (_from > _to)
        selectedDamageIdx = _to;

由于不仅列表已刷新,而且组件已经看到并更新了其显示列表,绑定也起作用。 希望能帮助别人。

相关问题