2012-11-20 33 views
0

我有一個列表中的dataProvider。該列表具有從var -selectedDamageIdx綁定的selecteIndex。我正在更改selectedDamageIdx,或多或少地改變列表中的項目。當一切都結束時,索引是不正確的,但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> _來自列表中的selectedIndex比它應該少一個。 在第二種情況下,來自selectedIndex的_to < _比應該多一個。

+0

嗨Dave,你能否提供一些關於你的列表是什麼樣子的代碼,以及你的列表數據提供者的樣子。謝謝! –

回答

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; 

由於不僅名單已經被刷新,而且組件已看到,並更新了其顯示列表綁定的作品。 希望能幫助別人。

相關問題