. 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); // horizontal arrangement, similar to the linear layout in Android. // layout-> setLayoutType (LAYOUT_RELATIVE ); /* set the image as the background */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
_ Array; for (int I = 0; I <20; I ++) {_ array. push_back (StringUtils: format ("listView_item _ % d", I);} ListView * listView = ListView: create (); // adjust parameters 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 ("backtoppressed.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
& 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
(Item-> getChildByName ("Title Button"); size_t index = listView-> getIndex (item); button-> setTitleText (static_cast
(_ 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
(pSender); log("%ld",listView->getCurSelectedIndex()); } break; case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_END: { ListView* listView = static_cast
(pSender); log("%ld",listView->getCurSelectedIndex()); } break; default: break; }}