issue #5176, readd addChild methods into UIListView

This commit is contained in:
andyque 2014-05-22 17:49:19 +08:00
parent c8d9ab8122
commit 1165ba8bc3
5 changed files with 70 additions and 23 deletions

View File

@ -676,7 +676,9 @@ void Layout::addChild(Node * child, int zOrder)
void Layout::addChild(Node *child, int zOrder, int tag)
{
supplyTheLayoutParameterLackToChild(static_cast<Widget*>(child));
if (dynamic_cast<Widget*>(child)) {
supplyTheLayoutParameterLackToChild(static_cast<Widget*>(child));
}
Widget::addChild(child, zOrder, tag);
_doLayoutDirty = true;
}

View File

@ -251,7 +251,6 @@ void ListView::pushBackDefaultItem()
return;
}
Widget* newItem = _model->clone();
_items.pushBack(newItem);
remedyLayoutParameter(newItem);
addChild(newItem);
_refreshViewDirty = true;
@ -264,25 +263,71 @@ void ListView::insertDefaultItem(ssize_t index)
return;
}
Widget* newItem = _model->clone();
_items.insert(index, newItem);
ScrollView::addChild(newItem);
remedyLayoutParameter(newItem);
addChild(newItem);
_refreshViewDirty = true;
}
void ListView::pushBackCustomItem(Widget* item)
{
_items.pushBack(item);
remedyLayoutParameter(item);
addChild(item);
_refreshViewDirty = true;
}
void ListView::addChild(cocos2d::Node *child)
{
ListView::addChild(child, child->getZOrder(), child->getTag());
}
void ListView::addChild(cocos2d::Node *child, int zOrder)
{
ListView::addChild(child, zOrder, child->getTag());
}
void ListView::addChild(cocos2d::Node *child, int zOrder, int tag)
{
ScrollView::addChild(child, zOrder, tag);
Widget* widget = dynamic_cast<Widget*>(child);
if (widget)
{
_items.pushBack(widget);
}
}
void ListView::removeChild(cocos2d::Node *child, bool cleaup)
{
Widget* widget = dynamic_cast<Widget*>(child);
if (widget) {
_items.eraseObject(widget);
}
ScrollView::removeChild(child, cleaup);
}
void ListView::removeAllChildren()
{
this->removeAllChildrenWithCleanup(true);
}
void ListView::removeAllChildrenWithCleanup(bool cleanup)
{
ScrollView::removeAllChildrenWithCleanup(cleanup);
_items.clear();
}
void ListView::insertCustomItem(Widget* item, ssize_t index)
{
_items.insert(index, item);
ScrollView::addChild(item);
remedyLayoutParameter(item);
addChild(item);
_refreshViewDirty = true;
}
@ -293,8 +338,8 @@ void ListView::removeItem(ssize_t index)
{
return;
}
_items.eraseObject(item);
removeChild(item);
removeChild(item, true);
_refreshViewDirty = true;
}
@ -305,7 +350,6 @@ void ListView::removeLastItem()
void ListView::removeAllItems()
{
_items.clear();
removeAllChildren();
}

View File

@ -162,6 +162,12 @@ public:
float getItemsMargin();
virtual void sortAllChildren() override;
virtual void addChild(Node * child) override;
virtual void addChild(Node * child, int zOrder) override;
virtual void addChild(Node* child, int zOrder, int tag) override;
virtual void removeAllChildren() override;
virtual void removeAllChildrenWithCleanup(bool cleanup) override;
virtual void removeChild(Node* child, bool cleaup = true) override;
ssize_t getCurSelectedIndex() const;
@ -186,18 +192,7 @@ CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
protected:
virtual void addChild(Node* child) override{ScrollView::addChild(child);};
virtual void addChild(Node * child, int zOrder) override{ScrollView::addChild(child, zOrder);};
virtual void addChild(Node* child, int zOrder, int tag) override{ScrollView::addChild(child, zOrder, tag);};
virtual void removeChild(Node* widget, bool cleanup = true) override{ScrollView::removeChild(widget, cleanup);};
virtual void removeAllChildren() override{removeAllChildrenWithCleanup(true);};
virtual void removeAllChildrenWithCleanup(bool cleanup) override {ScrollView::removeAllChildrenWithCleanup(cleanup);};
virtual Vector<Node*>& getChildren() override{return ScrollView::getChildren();};
virtual const Vector<Node*>& getChildren() const override{return ScrollView::getChildren();};
virtual ssize_t getChildrenCount() const override {return ScrollView::getChildrenCount();};
virtual Node * getChildByTag(int tag) const override {return ScrollView::getChildByTag(tag);};
virtual Widget* getChildByName(const std::string& name) override {return ScrollView::getChildByName(name);};
void updateInnerContainerSize();
void remedyLayoutParameter(Widget* item);
virtual void onSizeChanged() override;

View File

@ -153,7 +153,7 @@ void ScrollView::initRenderer()
{
Layout::initRenderer();
_innerContainer = Layout::create();
Layout::addChild(_innerContainer,1,1);
addProtectedChild(_innerContainer, 1, 1);
}
void ScrollView::onSizeChanged()

View File

@ -57,7 +57,6 @@ bool UIListViewTest_Vertical::init()
ListView* listView = ListView::create();
// set list view ex direction
listView->setDirection(ui::ScrollView::Direction::VERTICAL);
listView->setTouchEnabled(true);
listView->setBounceEnabled(true);
listView->setBackGroundImage("cocosui/green_edit.png");
listView->setBackGroundImageScale9Enabled(true);
@ -96,6 +95,12 @@ bool UIListViewTest_Vertical::init()
listView->insertDefaultItem(0);
}
listView->removeAllChildren();
Sprite* testSprite = Sprite::create("cocosui/backtotoppressed.png");
testSprite->setPosition(Vec2(200,200));
listView->addChild(testSprite);
// add custom item
for (int i = 0; i < count / 4; ++i)
{
@ -109,7 +114,7 @@ bool UIListViewTest_Vertical::init()
custom_button->setPosition(Vec2(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f));
custom_item->addChild(custom_button);
listView->pushBackCustomItem(custom_item);
listView->addChild(custom_item);
}
// insert custom item
Vector<Widget*>& items = listView->getItems();
@ -125,6 +130,7 @@ bool UIListViewTest_Vertical::init()
custom_item->setSize(custom_button->getSize());
custom_button->setPosition(Vec2(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f));
custom_item->addChild(custom_button);
custom_item->setTag(1);
listView->insertCustomItem(custom_item, items_count);
}
@ -140,7 +146,7 @@ bool UIListViewTest_Vertical::init()
}
// remove last item
listView->removeLastItem();
listView->removeChildByTag(1);
// remove item by index
items_count = items.size();