Fixed #9382 : ui::ListView->getCurSelectedIndex() is out of range.

This commit is contained in:
Vincent Yang 2015-07-20 17:50:02 +08:00
parent 371053305e
commit cb5e2d59c3
1 changed files with 22 additions and 9 deletions

View File

@ -35,7 +35,7 @@ ListView::ListView():
_model(nullptr), _model(nullptr),
_gravity(Gravity::CENTER_VERTICAL), _gravity(Gravity::CENTER_VERTICAL),
_itemsMargin(0.0f), _itemsMargin(0.0f),
_curSelectedIndex(0), _curSelectedIndex(-1),
_refreshViewDirty(true), _refreshViewDirty(true),
_listViewEventListener(nullptr), _listViewEventListener(nullptr),
_listViewEventSelector(nullptr), _listViewEventSelector(nullptr),
@ -230,14 +230,7 @@ void ListView::insertDefaultItem(ssize_t index)
{ {
return; return;
} }
Widget* newItem = _model->clone(); insertCustomItem(_model->clone(), index);
_items.insert(index, newItem);
ScrollView::addChild(newItem);
remedyLayoutParameter(newItem);
_refreshViewDirty = true;
} }
@ -285,6 +278,18 @@ void ListView::removeChild(cocos2d::Node *child, bool cleaup)
Widget* widget = dynamic_cast<Widget*>(child); Widget* widget = dynamic_cast<Widget*>(child);
if (nullptr != widget) if (nullptr != widget)
{ {
if (-1 != _curSelectedIndex)
{
auto removedIndex = getIndex(widget);
if (_curSelectedIndex > removedIndex)
{
_curSelectedIndex -= 1;
}
else if (_curSelectedIndex == removedIndex)
{
_curSelectedIndex = -1;
}
}
_items.eraseObject(widget); _items.eraseObject(widget);
} }
@ -300,10 +305,18 @@ void ListView::removeAllChildrenWithCleanup(bool cleanup)
{ {
ScrollView::removeAllChildrenWithCleanup(cleanup); ScrollView::removeAllChildrenWithCleanup(cleanup);
_items.clear(); _items.clear();
_curSelectedIndex = -1;
} }
void ListView::insertCustomItem(Widget* item, ssize_t index) void ListView::insertCustomItem(Widget* item, ssize_t index)
{ {
if (-1 != _curSelectedIndex)
{
if (_curSelectedIndex >= index)
{
_curSelectedIndex += 1;
}
}
_items.insert(index, item); _items.insert(index, item);
ScrollView::addChild(item); ScrollView::addChild(item);