mirror of https://github.com/axmolengine/axmol.git
Fixed #9382 : ui::ListView->getCurSelectedIndex() is out of range.
This commit is contained in:
parent
371053305e
commit
cb5e2d59c3
|
@ -35,7 +35,7 @@ ListView::ListView():
|
|||
_model(nullptr),
|
||||
_gravity(Gravity::CENTER_VERTICAL),
|
||||
_itemsMargin(0.0f),
|
||||
_curSelectedIndex(0),
|
||||
_curSelectedIndex(-1),
|
||||
_refreshViewDirty(true),
|
||||
_listViewEventListener(nullptr),
|
||||
_listViewEventSelector(nullptr),
|
||||
|
@ -230,14 +230,7 @@ void ListView::insertDefaultItem(ssize_t index)
|
|||
{
|
||||
return;
|
||||
}
|
||||
Widget* newItem = _model->clone();
|
||||
|
||||
_items.insert(index, newItem);
|
||||
ScrollView::addChild(newItem);
|
||||
|
||||
remedyLayoutParameter(newItem);
|
||||
|
||||
_refreshViewDirty = true;
|
||||
insertCustomItem(_model->clone(), index);
|
||||
}
|
||||
|
||||
|
||||
|
@ -285,6 +278,18 @@ void ListView::removeChild(cocos2d::Node *child, bool cleaup)
|
|||
Widget* widget = dynamic_cast<Widget*>(child);
|
||||
if (nullptr != widget)
|
||||
{
|
||||
if (-1 != _curSelectedIndex)
|
||||
{
|
||||
auto removedIndex = getIndex(widget);
|
||||
if (_curSelectedIndex > removedIndex)
|
||||
{
|
||||
_curSelectedIndex -= 1;
|
||||
}
|
||||
else if (_curSelectedIndex == removedIndex)
|
||||
{
|
||||
_curSelectedIndex = -1;
|
||||
}
|
||||
}
|
||||
_items.eraseObject(widget);
|
||||
}
|
||||
|
||||
|
@ -300,10 +305,18 @@ void ListView::removeAllChildrenWithCleanup(bool cleanup)
|
|||
{
|
||||
ScrollView::removeAllChildrenWithCleanup(cleanup);
|
||||
_items.clear();
|
||||
_curSelectedIndex = -1;
|
||||
}
|
||||
|
||||
void ListView::insertCustomItem(Widget* item, ssize_t index)
|
||||
{
|
||||
if (-1 != _curSelectedIndex)
|
||||
{
|
||||
if (_curSelectedIndex >= index)
|
||||
{
|
||||
_curSelectedIndex += 1;
|
||||
}
|
||||
}
|
||||
_items.insert(index, item);
|
||||
ScrollView::addChild(item);
|
||||
|
||||
|
|
Loading…
Reference in New Issue