AKever

导航

Cocos2dx(1) extensions_UIListView

Cocos2dx 2.2.1

此处的UIListView并没有通过Cocostudio获取得到,由纯Code产生,实现如下

ShowGame.h

#ifndef __SHOWGAME_H__
#define __SHOWGAME_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class ShowGame : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();
    CREATE_FUNC(ShowGame);
private:
    ShowGame(void);
    ~ShowGame(void);

    void menuCloseCallback(CCObject* pSender);
    void listViewEvent(CCObject *pSender, ListViewEventType type);//UIListView响应事件
void listViewItemEvent(CCObject *pSender, TouchEventType type); //Item(Layout)点击事件
UIListView
*p_listView;//ListView CCArray* p_array;//存放ListView数据的数组 int m_nCount;//用于初始化UIListView数组,遍历数据 }; #endif

ShowGame.cpp

#include "ShowGame.h"

#include "VisibleRect.h"
USING_NS_CC_EXT;
USING_NS_CC;

ShowGame::ShowGame(void)
{
}

ShowGame::~ShowGame(void)
{
}

cocos2d::CCScene* ShowGame::scene()
{
    CCScene* scene = CCScene::create();
    ShowGame* layer = ShowGame::create();
    scene->addChild(layer);
    return scene;
}

bool ShowGame::init()
{
  CCSize winSize = CCDirector::sharedDirector()->getWinSize();    //获取屏幕大小
//10 UILayer UILayer* ul10 = UILayer::create(); this->addChild(ul10); 
  //create UIListView p_listView
= UIListView::create(); p_listView->setTouchEnabled(true);             //设置触控为true p_listView->setBackGroundImageScale9Enabled(true);          //设置九宫格true p_listView->setBackGroundImage("cocosgui/green_edit.png");     //设置背景 p_listView->setSize(CCSizeMake(240, 130));               //设置大小  p_listView->setPosition(ccp(winSize.width/ 2,             //设置位置 winSize.height/ 2)); float f_listWidth = listView->getSize().width; float f_listHeight = listView->getSize().height;
  //初始化m_array数据,用于存入UIListView数据 m_nCount
= 0; p_array = CCArray::create(); p_array->retain(); for (int i = 0; i < 20; ++i) { CCString* ccstr = CCString::createWithFormat("object_%d", i); m_array->addObject(ccstr); }
  //UIListView显示的数据
for (int i = 0; i < 7; ++i) { UILabel* label = UILabel::create();          //create UILabel label->setText("It's miraculous!!!"); label->setName("TextLabel");
     //label->addTouchEventListener(this, toucheventselector(HelloWorld::listViewItemEvent));//也可以在label添加事件监听,更合适
    //create Layout 并把 UILabel放入 Layout && 设置好他们的Position Layout
*layout = Layout::create();     layout->setName(CCString::createWithFormat("panel_%i", i)->getCString()); layout->setSize(CCSizeMake(label->getSize().width, label->getSize().height));
     layout->addTouchEventListener(this, toucheventselector(HelloWorld::listViewItemEvent)); //Layout点击事件 label
->setPosition(ccp(layout->getSize().width / 2, layout->getSize().height / 2)); layout->addChild(label); CCSize panel_size = layout->getSize();
     //设置layout的间隔,以此设置layout的间距 layout
->setPosition(ccp((f_listWidth - panel_size.width) / 2, (p_listHeight - (panel_size.height + panel_size.height * 0.25)) - i * (panel_size.height + panel_size.height * 0.25))); listView->addChild(layout); //把UILabel添加到Layout中 }
  //配置UIListView的适配函数并使用把m_array数据放入ListView中 p_listView
->addEventListenerListView(this, listvieweventselector(ShowGame::listViewEvent)); p_listView->initChildWithDataLength(p_array->count()); ul10->addWidget(p_listView);  //把ListView添加入UILayer return true; }
// not use
void ShowGame::menuCloseCallback(CCObject* pSender) { CCArmatureDataManager::purge(); SceneReader::sharedSceneReader()->purgeSceneReader(); cocos2d::extension::ActionManager::shareManager()->purgeActionManager(); GUIReader::shareReader()->purgeGUIReader(); CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
//UIListView适配函数
void ShowGame::listViewEvent(CCObject *pSender, ListViewEventType type) { switch(type) { case LISTVIEW_EVENT_INIT_CHILD: {
    //把m_array中的数据放进UIListView中 CCString
* ccstr = static_cast<CCString*>(p_array->objectAtIndex(m_nCount));       UIListView* list = dynamic_cast<UIListView*>(pSender); Layout* layout = dynamic_cast<Layout*>(list->getUpdateChild()); UILabel* label = dynamic_cast<UILabel*>(layout->getChildByName("TextLabel")); label->setText(ccstr->getCString()); m_nCount++; } break; case LISTVIEW_EVENT_UPDATE_CHILD: { UIListView* list = dynamic_cast<UIListView*>(pSender); int index = list->getUpdateDataIndex(); if (index < 0 || index >= list->getDataLength()) { list->setUpdateSuccess(false); } CCString* ccstr = static_cast<CCString*>(p_array->objectAtIndex(index)); Layout* layout = dynamic_cast<Layout*>(list->getUpdateChild()); UILabel* label = dynamic_cast<UILabel*>(layout->getChildByName("TextLabel")); label->setText(ccstr->getCString()); list->setUpdateSuccess(true); } break; default: break; } }

//Item(Layout)事件添加

void HelloWorld::listViewItemEvent(CCObject *pSender, TouchEventType type)
{
  switch(type)
  {
  case cocos2d::extension::TOUCH_EVENT_ENDED:
  {
    CCLog("dddddd");
    CCLog("enter");
  }
    break;
  default:
    break;
  }
}

 

上面实现的效果如下图,凑合着看 囧~~

WARN: 代码切记copy贴入vs,报错

posted on 2014-01-16 23:22  AKever  阅读(2301)  评论(0)    收藏  举报