ListView listView = ListView::create(); --创建列表
listView->setDirection(ui::ScrollView::Direction::VERTICAL);
--设置列表方向
listView->setBounceEnabled(true); --设置弹力
listView->setBackGroundImage("cocosui/green_edit.png");
--设置背景图片
listView->setBackGroundImageScale9Enabled(true);
--设置是否允许九宫格
listView->setContentSize(Size(240, 130);
--设置大小
listView->setPosition(Vec2());
listView->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(类名::回调函数1, this));
//注册事件处理函数
listView->addEventListener((ui::ListView::ccScrollViewCallback)CC_CALLBACK_2(类名:回调函数2, this)); //注册事件处理函数
void 类名::回调函数1(Ref * pSender, ListView::EventType type)
--注意这里的参数类型
{
switch (type)
{
case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_START:
ListView * listView = static_cast(ListView *)(pSender);
listView->getCurSelectedIndex();
break;
case cocs2d::ui::ListView::EventType::ON_SELECTED_ITEM_END:
ListView * listView = static_cast(ListView *)(pSender);
listView->getCurSelectedIndex();
break;
default:
break;
}
}
void 类名::回调函数2(Ref * pSender, ui::ScrollView::EventType type)
{
switch (type)
{
case ui::ScrollView::EventType::SCROLL_TO_BOTTOM:
break;
case ui::ScrollView::EventType::SCROLL_TO_TOP:
break;
default:
break;
}
}
Layout * default _item = Layout::create();
//创建模型
listView->setItemModel(default_item); //设置模型
listView->pushBackDefaultItem(); //添加默认元素
listView->insertDefaultItem(index); //插入默认元素
Layout * custom_item = Layout::create();
listView->addChild(custom_item); //添加常规元素
listView->insertCustomItem(custom_item, index)
//插入常规元素
Vector<Widget*>& items = listView->getItems(); //返回保存元素的容器
Widget* item = listView->getItem(i); //获取某个索引的元素
ssize_t index = listView->getIndex(item);
//获取某个元素的索引
listView->removeChildByTag(1); //继承自CCNode的函数
listView->removeItem(items_count - 1); //删除某个索引的元素
listView->setGravity(ListView::Gravity::CENTER_VERTICAL); //设置重力
listView->setItemsMargin(2.0f); //改变两个元素之间的边界