0

I have a dataProvider from a list. The list has the selecteIndex Binded from a var -selectedDamageIdx. I am changing the selectedDamageIdx and more or less and at the same I move items arround in the list. When everything is over the index is not correct but the value on the selectedDamageIdx is correct. Any suggestions? I made sure the selectedDamageIdx is public and bindable. The Binding is done in mxml.

My list is a damage list. It uses a damage itemRenderer and a damage Object. Basically, what I am doing is: I have a thumbail with the same information as the list. When I drag something around in the thumbnail to move items, I want the list to update the item positions. I have a function called onChangePosition as following where I have access to the _from and _to positions. So I do the following:

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;
}

This works fine on the thumbnail and the selectedDamageIdx gets the expected value. But, I think because of my add and remove operation the list selects the wrong item, and the selectedIndex is wrong. On the first case where _to > _from the selectedIndex on the list is one less than it should be. On the second case where _to < _from the selectedIndex is one more than it should be.

4

1 回答 1

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);
}

然后在更新索引上:

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

由于不仅列表已被刷新,而且组件也已经看到并更新了它的显示列表,因此绑定工作。希望这会帮助某人。

于 2012-11-20T13:47:56.933 回答