void selectedItemEventScrollView(Ref* pSender,cocos2d::ui::ScrollView::EventType type);
----------------------------------------------------------------------------------------------------------------------------------------
ize winSize = CCDirector::getInstance()->getVisibleSize();
Layout* root = Layout::create();
root->setContentSize(Size(500,400));
root->setBackGroundColorType(LAYOUT_COLOR_SOLID);
root->setAnchorPoint(Point(0.5f,0.5f));
root->setPosition(Point(winSize.width/2,winSize.height/2));
this->addChild(root);
std::vector<std::string> _array;
for (int i = 0; i < 20; i++)
{
std::string ccstr = StringUtils::format("ListView_Item_%d",i);
_array.push_back(ccstr);
}
ListView* listView = ListView::create();
listView->setDirection(ui::ScrollView::Direction::HORIZONTAL);//设置水平还是 垂直
listView->setBackGroundImage("green_edit.png");
listView->setBackGroundImageScale9Enabled(true);
listView->setAnchorPoint(Point(0.5f,0.5f));
listView->setContentSize(Size(490,390));
listView->setPosition(Vec2(winSize.width / 2,winSize.height / 2));
listView->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(HelloWorld::selectedItemEvent,this));、//添加监听函数
listView->addEventListener((ui::ListView::ccScrollViewCallback)CC_CALLBACK_2(HelloWorld::selectedItemEventScrollView,this));
this->addChild(listView);
//root->addChild(listView);
Sprite* testSprite = Sprite::create("backtotoppressed.png");
testSprite->setPosition(Vec2(200,200));
listView->addChild(testSprite);
// add custom item
ssize_t count = _array.size();
for (int i = 0; i < count / 4; ++i)
{
Button* custom_button = Button::create("button.png","buttonHighlighted.png");
custom_button->setName("Title Button");
custom_button->setScale9Enabled(true);
custom_button->setContentSize(Size(300,50));
Layout *custom_item = Layout::create();//要通过Layout添加到ListView中
custom_item->setContentSize(custom_button->getContentSize());
custom_button->setPosition(Vec2(custom_item->getContentSize().width / 2.0f,custom_item->getContentSize().height / 2.0f));
custom_item->addChild(custom_button);
listView->addChild(custom_item);
}
//Vector<Widget*>& items = listView->getItems();
//int item_count = items.size();
//CCLOG("liubin---------->%d",item_count);
// insert custom item //插入item
Vector<Widget*>& items = listView->getItems();
ssize_t items_count = items.size();
CCLOG("liubin------111---->%d",items_count);
for (int i = 0; i < count / 4; ++i)
{
Button* custom_button = Button::create("button.png","buttonHighlighted.png");
custom_button->setName("Title Button");
custom_button->setScale9Enabled(true);
//custom_button->setTitleText(_array[i]);
custom_button->setContentSize(Size(300,50));
Layout *custom_item = Layout::create();
custom_item->setContentSize(custom_button->getContentSize());
custom_button->setPosition(Vec2(custom_item->getContentSize().width / 2.0f,custom_item->getContentSize().height / 2.0f));
custom_item->addChild(custom_button);
custom_item->setTag(1);
listView->insertCustomItem(custom_item,items_count);
}
//Vector<Widget*>& itemss = listView->getItems();
//int iiii = itemss.size();
//CCLOG("liubin------222---->%d",iiii);
int itemSize = listView->getItems().size();
for (size_t i = 0; i < itemSize; i++)
{
Widget* item = listView->getItem(i);//获取其中的某个项,然后转换成Button 进行设置操作
Button* button = static_cast<Button*>(item->getChildByName("Title Button"));
ssize_t index = listView->getIndex(item);
button->setTitleText(_array[i]);
}
listView->removeChildByTag(1);//删除最后一个
listView->removeItem(2);//删除指定项
listView->setGravity(ListView::Gravity::CENTER_VERTICAL);
listView->setItemsMargin(15.0f);//设置listView间距
void HelloWorld::selectedItemEvent(Ref* pSender,cocos2d::ui::ListView::EventType type) { switch (type) { case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_START: { ListView* listView = static_cast<ListView*>(pSender); CC_UNUSED_PARAM(listView); CCLOG("select child start index = %ld",listView->getCurSelectedIndex()); break; } case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_END: { ListView* listView = static_cast<ListView*>(pSender); CC_UNUSED_PARAM(listView); CCLOG("select child end index = %ld",listView->getCurSelectedIndex()); break; } default: break; } } void HelloWorld::selectedItemEventScrollView(Ref* pSender,ui::ScrollView::EventType type) { switch (type) { case ui::ScrollView::EventType::SCROLL_TO_BOTTOM://滑动到底部 CCLOG("SCROLL_TO_BOTTOM"); break; case ui::ScrollView::EventType::SCROLL_TO_TOP://滑动到头部 CCLOG("SCROLL_TO_TOP"); break; default: break; } }
原文链接:/cocos2dx/340179.html