.h
#include "cocos2d.h"
#include "cocos-ext.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
void selectedItemEvent(Ref *pSender, ListViewEventType type);
.cpp
layout = Layout::create();
layout->setSize(Size(widgetSize.width, widgetSize.height));
//横向排列,这里类似Android里的线性布局
//layout->setLayoutType(LAYOUT_RELATIVE);
/*以图片为背景*/
layout->setBackGroundImageScale9Enabled(true);
layout->setBackGroundImage("green_edit.png");
layout->setPosition(Point(0,0));
addChild(layout);
alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30 );
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Point(widgetSize.width / 2.0f,
widgetSize.height / 2.0f - alert->getSize().height * 3.075f));
layout->addChild(alert);
/******************************************************************************************/
std::vector<std::string> _array;
for (int i = 0; i < 20; i++) {
_array.push_back( StringUtils::format("listView_item_%d",i));
}
ListView* listView = ListView::create();
//SCROLLVIEW_DIR_VERTICAL SCROLLVIEW_DIR_HORIZONTAL
listView->setDirection(SCROLLVIEW_DIR_VERTICAL);
listView->setTouchEnabled(true);
listView->setBounceEnabled(true);
listView->setBackGroundImage("green_edit.png");
listView->setBackGroundImageScale9Enabled(true);
listView->setSize(Size(480, 260));
listView->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
listView->addEventListenerListView(this, listvieweventselector(LayoutTest::selectedItemEvent));
layout->addChild(listView);
//create model
Button* default_button = Button::create("backtotoppressed.png","backtotopnormal.png");
default_button->setName("Title Button");
Layout* default_item = Layout::create();
default_item->setTouchEnabled(true);
default_item->setSize(default_button->getSize());
default_button->setPosition(Point(default_item->getSize().width / 2.0f, default_item->getSize().height / 2.0f));
default_item->addChild(default_button);
//set model
listView->setItemModel(default_item);
//add default item
ssize_t count = _array.size();
for (int i = 0; i < count / 4; ++i) {
listView->pushBackDefaultItem();
}
//insert default item
for (int i = 0; i < count / 4; ++i) {
listView->insertDefaultItem(0);
}
//add custom item
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->setSize(default_button->getSize());
Layout* custom_item = Layout::create();
custom_item->setSize(custom_button->getSize());
custom_button->setPosition(Point(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f));
custom_item->addChild(custom_button);
listView->pushBackCustomItem(custom_item);
}
//insert custom item
Vector<Widget*>& items = listView->getItems();
ssize_t items_count = items.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->setSize(default_button->getSize());
Layout* custom_item = Layout::create();
custom_item->setSize(custom_button->getSize());
custom_button->setPosition(Point(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f));
custom_item->addChild(custom_button);
listView->insertCustomItem(custom_item, items_count);
}
//set item data
items_count = items.size();
for (int i = 0; i < items_count; ++i) {
Widget* item = listView->getItem(i);
Button* button = static_cast<Button*>(item->getChildByName("Title Button"));
size_t index = listView->getIndex(item);
button->setTitleText(static_cast<std::string>(_array.at(index)).c_str());
}
listView->removeLastItem();
// listView->removeAllItems();
listView->setGravity(LISTVIEW_GRAVITY_CENTER_VERTICAL);
listView->setItemsMargin(15.0f);
void LayoutTest::selectedItemEvent(cocos2d::Ref *pSender, ListViewEventType type)
{
switch (type) {
case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_START:
{
ListView* listView = static_cast<ListView*>(pSender);
log("%ld",listView->getCurSelectedIndex());
}
break;
case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_END:
{
ListView* listView = static_cast<ListView*>(pSender);
log("%ld",listView->getCurSelectedIndex());
}
break;
default:
break;
}
}