diff --git a/cocos/ui/UIListView.h b/cocos/ui/UIListView.h index 0112cf585a..66e48efcf1 100644 --- a/cocos/ui/UIListView.h +++ b/cocos/ui/UIListView.h @@ -390,8 +390,8 @@ protected: virtual void onItemListChanged(); + virtual void remedyLayoutParameter(Widget* item); void updateInnerContainerSize(); - void remedyLayoutParameter(Widget* item); void remedyVerticalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex); void remedyHorizontalLayoutParameter(LinearLayoutParameter* layoutParameter,ssize_t itemIndex); diff --git a/cocos/ui/UIPageView.cpp b/cocos/ui/UIPageView.cpp index 17904d2c38..3fb95e8093 100644 --- a/cocos/ui/UIPageView.cpp +++ b/cocos/ui/UIPageView.cpp @@ -427,6 +427,11 @@ const Color3B& PageView::getIndicatorSelectedIndexColor() const return _indicator->getSelectedIndexColor(); } +void PageView::remedyLayoutParameter(Widget *item) +{ + item->setContentSize(this->getContentSize()); + ListView::remedyLayoutParameter(item); +} } diff --git a/cocos/ui/UIPageView.h b/cocos/ui/UIPageView.h index c2129ccdda..926eb936c4 100644 --- a/cocos/ui/UIPageView.h +++ b/cocos/ui/UIPageView.h @@ -350,6 +350,7 @@ CC_CONSTRUCTOR_ACCESS: protected: void pageTurningEvent(); + virtual void remedyLayoutParameter(Widget* item)override; virtual void moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack) override; virtual void onItemListChanged() override; virtual void onSizeChanged() override; diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp index 20040dfdb2..7306c0d9a8 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.cpp @@ -13,6 +13,7 @@ UIPageViewTests::UIPageViewTests() ADD_TEST_CASE(UIPageViewJumpToPageTest); ADD_TEST_CASE(UIPageViewVerticalTest); ADD_TEST_CASE(UIPageViewDisableTouchTest); + ADD_TEST_CASE(UIPageViewChildSizeTest); } // UIPageViewTest @@ -876,3 +877,88 @@ bool UIPageViewDisableTouchTest::init() return false; } +// UIPageViewTest +UIPageViewChildSizeTest::UIPageViewChildSizeTest() + : _displayValueLabel(nullptr) +{ + +} + +UIPageViewChildSizeTest::~UIPageViewChildSizeTest() +{ +} + +bool UIPageViewChildSizeTest::init() +{ + if (UIScene::init()) + { + Size widgetSize = _widget->getContentSize(); + + // Add a label in which the dragpanel events will be displayed + _displayValueLabel = Text::create("Move by horizontal direction", "fonts/Marker Felt.ttf", 32); + _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); + _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, + widgetSize.height / 2.0f + + _displayValueLabel->getContentSize().height * 1.5)); + _uiLayer->addChild(_displayValueLabel); + + // Add the black background + Text* alert = Text::create("PageView", "fonts/Marker Felt.ttf", 30); + alert->setColor(Color3B(159, 168, 176)); + alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); + _uiLayer->addChild(alert); + + Layout* root = static_cast(_uiLayer->getChildByTag(81)); + + Layout* background = dynamic_cast(root->getChildByName("background_Panel")); + + // Create the page view + Size size(240, 130); + PageView* pageView = PageView::create(); + pageView->setDirection(PageView::Direction::HORIZONTAL); + pageView->setContentSize(size); + Size backgroundSize = background->getContentSize(); + pageView->setPosition((widgetSize - pageView->getContentSize()) / 2.0f); + pageView->removeAllItems(); + pageView->setIndicatorEnabled(true); + + int pageCount = 4; + for (int i = 0; i < pageCount; ++i) + { + ImageView* imageView = ImageView::create("cocosui/scrollviewbg.png"); + imageView->setScale9Enabled(true); + + Text* label = Text::create(StringUtils::format("page %d", (i + 1)), "fonts/Marker Felt.ttf", 30); + label->setColor(Color3B(192, 192, 192)); + label->setAnchorPoint(Vec2::ZERO); + imageView->addChild(label); + + pageView->insertCustomItem(imageView, i); + } + + pageView->addEventListener(CC_CALLBACK_2(UIPageViewChildSizeTest::pageViewEvent, this)); + + _uiLayer->addChild(pageView); + + return true; + } + return false; +} + +void UIPageViewChildSizeTest::pageViewEvent(Ref *pSender, PageView::EventType type) +{ + switch (type) + { + case PageView::EventType::TURNING: + { + PageView* pageView = dynamic_cast(pSender); + + _displayValueLabel->setString(StringUtils::format("page = %ld", pageView->getCurrentPageIndex() + 1)); + } + break; + + default: + break; + } +} + diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h index 466b95673a..bc17231970 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIPageViewTest/UIPageViewTest.h @@ -139,4 +139,20 @@ protected: cocos2d::ui::Text* _displayValueLabel; }; +class UIPageViewChildSizeTest : public UIScene +{ +public: + CREATE_FUNC(UIPageViewChildSizeTest); + + UIPageViewChildSizeTest(); + ~UIPageViewChildSizeTest(); + virtual bool init() override; + + void pageViewEvent(cocos2d::Ref* sender, cocos2d::ui::PageView::EventType type); + +protected: + + cocos2d::ui::Text* _displayValueLabel; +}; + #endif /* defined(__TestCpp__UIPageViewTest__) */