PuzzleSDK
UIListViewTest_Horizontal类 参考

#include <UIListViewTest.h>

+ 类 UIListViewTest_Horizontal 继承关系图:
+ UIListViewTest_Horizontal 的协作图:

Public 成员函数

 CREATE_FUNC (UIListViewTest_Horizontal)
 
 UIListViewTest_Horizontal ()
 
 ~UIListViewTest_Horizontal ()
 
virtual bool init () override
 
void selectedItemEvent (cocos2d::Ref *sender, cocos2d::ui::ListView::EventType type)
 
virtual void update (float dt) override
 
- Public 成员函数 继承自 UIScene
 UIScene ()
 
 ~UIScene ()
 
virtual void onEnter () override
 
- Public 成员函数 继承自 TestCase
 TestCase ()
 
 ~TestCase ()
 
virtual std::string title () const
 
virtual std::string subtitle () const
 
virtual Type getTestType () const
 
virtual float getDuration () const
 
virtual std::string getExpectedOutput () const
 
virtual std::string getActualOutput () const
 
virtual void restartTestCallback (cocos2d::Ref *sender)
 
virtual void nextTestCallback (cocos2d::Ref *sender)
 
virtual void priorTestCallback (cocos2d::Ref *sender)
 
virtual void onBackCallback (cocos2d::Ref *sender)
 
void setTestSuite (TestSuite *testSuite)
 
TestSuitegetTestSuite () const
 
float getRunTime () const
 
void setTestCaseName (const std::string &name)
 
std::string getTestCaseName () const
 
const cocos2d::Label * getSubtitleLable () const
 
const cocos2d::MenuItemImage * getRestartTestItem () const
 

Protected 成员函数

float getItemPositionXInView (cocos2d::ui::Widget *item) const
 
void updateItem (int itemID, int templateID)
 

Protected 属性

cocos2d::ui::Text * _displayValueLabel = nullptr
 
int _spawnCount = 4
 
int _totalCount = 50
 
float _bufferZone = 140.f
 
float _itemTemplateWidth = 0.f
 
float _updateInterval = 1.0f / 24
 
float _updateTimer = 0.f
 
float _lastContentPosX = 0.f
 
float _reuseItemOffset = 0.f
 
cocos2d::ui::ListView * _listView = nullptr
 
std::vector< std::string > _array
 
- Protected 属性 继承自 UIScene
cocos2d::Layer * _uiLayer
 
cocos2d::ui::Layout * _widget
 
cocos2d::ui::Text * _sceneTitle
 
- Protected 属性 继承自 TestCase
cocos2d::MenuItemImage * _priorTestItem
 
cocos2d::MenuItemImage * _restartTestItem
 
cocos2d::MenuItemImage * _nextTestItem
 
cocos2d::Label * _titleLabel
 
cocos2d::Label * _subtitleLabel
 

额外继承的成员函数

- Public 类型 继承自 TestCase
enum class  Type { ROBUSTNESS , UNIT , GRAPHICAL_STATIC , MANUAL }
 
- Public 属性 继承自 TestCase
CC_CONSTRUCTOR_ACCESS __pad0__: virtual bool init() override
 

详细描述

在文件 UIListViewTest.h72 行定义.

构造及析构函数说明

◆ UIListViewTest_Horizontal()

UIListViewTest_Horizontal::UIListViewTest_Horizontal ( )

在文件 UIListViewTest.cpp296 行定义.

297{
298}

◆ ~UIListViewTest_Horizontal()

UIListViewTest_Horizontal::~UIListViewTest_Horizontal ( )

在文件 UIListViewTest.cpp300 行定义.

301{
302}

成员函数说明

◆ CREATE_FUNC()

UIListViewTest_Horizontal::CREATE_FUNC ( UIListViewTest_Horizontal  )

◆ getItemPositionXInView()

float UIListViewTest_Horizontal::getItemPositionXInView ( cocos2d::ui::Widget *  item) const
protected

在文件 UIListViewTest.cpp409 行定义.

410{
411 auto worldPos = item->getParent()->convertToWorldSpaceAR(item->getPosition());
412 auto viewPos = this->_listView->convertToNodeSpaceAR(worldPos);
413 return viewPos.x;
414}
cocos2d::ui::ListView * _listView

引用了 _listView.

被这些函数引用 update().

+ 这是这个函数的调用关系图:

◆ init()

bool UIListViewTest_Horizontal::init ( )
overridevirtual

重载 UIScene .

在文件 UIListViewTest.cpp304 行定义.

305{
306 if (UIScene::init())
307 {
308 Size widgetSize = _widget->getContentSize();
309
310 _displayValueLabel = Text::create("There are 50 items, but we only create 5 templates",
311 "fonts/Marker Felt.ttf",
312 20);
313
314 _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
315 _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f,
316 widgetSize.height / 2.0f
317 + _displayValueLabel->getContentSize().height * 2 + 20));
318
319 _uiLayer->addChild(_displayValueLabel);
320
321
322 Text* alert = Text::create("ListView horizontal", "fonts/Marker Felt.ttf", 30);
323 alert->setColor(Color3B(159, 168, 176));
324 alert->setPosition(Vec2(widgetSize.width / 2.0f,
325 widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
326 _uiLayer->addChild(alert);
327
328 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
329
330 Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel"));
331 Size backgroundSize = background->getContentSize();
332
333
334 // create listview data
335 //The data is usually fetch from the server
336 for (int i = 0; i < _totalCount; ++i)
337 {
338 std::string ccstr = StringUtils::format("listview_item_%d", i);
339 _array.push_back(ccstr);
340 }
341
342
343 // Create the list view ex
344 _listView = ListView::create();
345 // set list view ex direction
346 _listView->setDirection(ui::ScrollView::Direction::HORIZONTAL);
347 _listView->setTouchEnabled(true);
348 _listView->setBounceEnabled(true);
349 _listView->setBackGroundImage("cocosui/green_edit.png");
350 _listView->setBackGroundImageScale9Enabled(true);
351 _listView->setContentSize(Size(240.0f, 130.0f));
352 _listView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f +
353 (backgroundSize.width - _listView->getContentSize().width) / 2.0f,
354 (widgetSize.height - backgroundSize.height) / 2.0f +
355 (backgroundSize.height - _listView->getContentSize().height) / 2.0f));
356 _listView->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(UIListViewTest_Horizontal::selectedItemEvent, this));
357 _listView->setScrollBarPositionFromCorner(Vec2(7, 7));
358 _uiLayer->addChild(_listView);
359
360
361 // create model
362 Button* default_button = Button::create("cocosui/backtotoppressed.png",
363 "cocosui/backtotopnormal.png");
364 default_button->setName("Title Button");
365
366 Layout *default_item = Layout::create();
367 default_item->setTouchEnabled(true);
368 default_item->setContentSize(default_button->getContentSize());
369 default_button->setPosition(Vec2(default_item->getContentSize().width / 2.0f,
370 default_item->getContentSize().height / 2.0f));
371 default_item->addChild(default_button);
372
373 // set model
374 _listView->setItemModel(default_item);
375
376 // set all items layout gravity
377 _listView->setGravity(ListView::Gravity::CENTER_VERTICAL);
378
379 //initial the data
380 for (int i = 0; i < _totalCount; ++i) {
381 if (i < _spawnCount) {
382 Widget* item = default_item->clone();
383 item->setTag(i);
384 Button* btn = (Button*)item->getChildByName("Title Button");
385 btn->setTitleText(_array.at(i));
386 _listView->pushBackCustomItem(item);
387 }
388 }
389
390 // set items margin
391 float spacing = 4;
392 _listView->setItemsMargin(spacing);
393 _itemTemplateWidth = default_item->getContentSize().width;
394 this->_reuseItemOffset = (_itemTemplateWidth + spacing) * _spawnCount;
395
396 _listView->forceDoLayout();
397 float totalWidth = _itemTemplateWidth * _totalCount + (_totalCount - 1) * 4;
398 _listView->getInnerContainer()->setContentSize(Size(totalWidth, _listView->getInnerContainerSize().height));
399
400 //update listview data
401 this->scheduleUpdate();
402
403 return true;
404 }
405
406 return false;
407}
void selectedItemEvent(cocos2d::Ref *sender, cocos2d::ui::ListView::EventType type)
std::vector< std::string > _array
cocos2d::ui::Text * _displayValueLabel
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45

引用了 _array, _displayValueLabel, _itemTemplateWidth, _listView, _reuseItemOffset, _spawnCount, _totalCount, UIScene::_uiLayer, UIScene::_widget, UIScene::init() , 以及 selectedItemEvent().

+ 函数调用图:

◆ selectedItemEvent()

void UIListViewTest_Horizontal::selectedItemEvent ( cocos2d::Ref *  sender,
cocos2d::ui::ListView::EventType  type 
)

在文件 UIListViewTest.cpp463 行定义.

464{
465 switch (type)
466 {
467 case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_START:
468 {
469 ListView* listView = static_cast<ListView*>(pSender);
470 auto item = listView->getItem(listView->getCurSelectedIndex());
471 log("select child start index = %d", item->getTag());
472 break;
473 }
474 case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_END:
475 {
476 ListView* listView = static_cast<ListView*>(pSender);
477 auto item = listView->getItem(listView->getCurSelectedIndex());
478 log("select child end index = %d", item->getTag());
479 break;
480 }
481 default:
482 break;
483 }
484}

被这些函数引用 init().

+ 这是这个函数的调用关系图:

◆ update()

void UIListViewTest_Horizontal::update ( float  dt)
overridevirtual

在文件 UIListViewTest.cpp425 行定义.

426{
427 this->_updateTimer += dt;
428 if (this->_updateTimer < this->_updateInterval) {
429 return;
430 }
431 float totalWidth = _itemTemplateWidth * _totalCount + (_totalCount - 1) * 4;
432
433 this->_updateTimer = 0;
434 auto isRight = this->_listView->getInnerContainer()->getPosition().x < this->_lastContentPosX;
435 auto items = _listView->getItems();
436
437 for (int i = 0; i < _spawnCount && i < _totalCount; ++i) {
438 auto item = items.at(i);
439 auto itemPos = this->getItemPositionXInView(item);
440 if (isRight) {
441 if (itemPos < -_bufferZone && item->getPosition().x + _reuseItemOffset < totalWidth) {
442 int itemID = item->getTag() + (int)items.size();
443 item->setPositionX(item->getPositionX() + _reuseItemOffset);
444 CCLOG("itemPos = %f, itemID = %d, templateID = %d", itemPos, itemID, i);
445 this->updateItem(itemID, i);
446 }
447 }
448 else {
449 if (itemPos > _bufferZone + _listView->getContentSize().width &&
450 item->getPosition().x - _reuseItemOffset >= 0) {
451
452 item->setPositionX(item->getPositionX() - _reuseItemOffset);
453 int itemID = item->getTag() - (int)items.size();
454 CCLOG("itemPos = %f, itemID = %d, templateID = %d", itemPos, itemID, i);
455 this->updateItem(itemID, i);
456 }
457 }
458 }
459 //update ListView Items
460 this->_lastContentPosX = this->_listView->getInnerContainer()->getPosition().x;
461}
float getItemPositionXInView(cocos2d::ui::Widget *item) const
void updateItem(int itemID, int templateID)

引用了 _bufferZone, _itemTemplateWidth, _lastContentPosX, _listView, _reuseItemOffset, _spawnCount, _totalCount, _updateInterval, _updateTimer, getItemPositionXInView() , 以及 updateItem().

+ 函数调用图:

◆ updateItem()

void UIListViewTest_Horizontal::updateItem ( int  itemID,
int  templateID 
)
protected

在文件 UIListViewTest.cpp416 行定义.

417{
418 auto itemTemplate = _listView->getItems().at(templateID);
419 Button* btn = (Button*)itemTemplate->getChildByName("Title Button");
420 //If you have the item ID and templateID, you could fill in the data here
421 itemTemplate->setTag(itemID);
422 btn->setTitleText(_array.at(itemID));
423}

引用了 _array , 以及 _listView.

被这些函数引用 update().

+ 这是这个函数的调用关系图:

类成员变量说明

◆ _array

std::vector<std::string> UIListViewTest_Horizontal::_array
protected

在文件 UIListViewTest.h103 行定义.

被这些函数引用 init() , 以及 updateItem().

◆ _bufferZone

float UIListViewTest_Horizontal::_bufferZone = 140.f
protected

在文件 UIListViewTest.h94 行定义.

被这些函数引用 update().

◆ _displayValueLabel

cocos2d::ui::Text* UIListViewTest_Horizontal::_displayValueLabel = nullptr
protected

在文件 UIListViewTest.h87 行定义.

被这些函数引用 init().

◆ _itemTemplateWidth

float UIListViewTest_Horizontal::_itemTemplateWidth = 0.f
protected

在文件 UIListViewTest.h95 行定义.

被这些函数引用 init() , 以及 update().

◆ _lastContentPosX

float UIListViewTest_Horizontal::_lastContentPosX = 0.f
protected

在文件 UIListViewTest.h99 行定义.

被这些函数引用 update().

◆ _listView

cocos2d::ui::ListView* UIListViewTest_Horizontal::_listView = nullptr
protected

在文件 UIListViewTest.h101 行定义.

被这些函数引用 getItemPositionXInView(), init(), update() , 以及 updateItem().

◆ _reuseItemOffset

float UIListViewTest_Horizontal::_reuseItemOffset = 0.f
protected

在文件 UIListViewTest.h100 行定义.

被这些函数引用 init() , 以及 update().

◆ _spawnCount

int UIListViewTest_Horizontal::_spawnCount = 4
protected

在文件 UIListViewTest.h90 行定义.

被这些函数引用 init() , 以及 update().

◆ _totalCount

int UIListViewTest_Horizontal::_totalCount = 50
protected

在文件 UIListViewTest.h92 行定义.

被这些函数引用 init() , 以及 update().

◆ _updateInterval

float UIListViewTest_Horizontal::_updateInterval = 1.0f / 24
protected

在文件 UIListViewTest.h97 行定义.

被这些函数引用 update().

◆ _updateTimer

float UIListViewTest_Horizontal::_updateTimer = 0.f
protected

在文件 UIListViewTest.h98 行定义.

被这些函数引用 update().


该类的文档由以下文件生成: