当前位置:编程学习 > wap >>

cocos2d-x-2.0 ListView使用

由于cocos2d-x没有给出例子,所以代码有点长。我都写了注释,应该很容易理解。

ListViewTestLayer.h头文件
[cpp] 
// 
 
//  ListViewTestLayer.h 
 
//  2dxDemo 
 
// 
 
//  Created by Yanghui Liu on 12-6-26. 
 
//  Copyright (c) 2012年 BoyoJoy. All rights reserved. 
 
// 
 
  
 
#ifndef _dxDemo_ListViewTestLayer_h 
 
#define _dxDemo_ListViewTestLayer_h 
 
  
 
#include "cocos2d.h" 
 
#include "CCListView.h" 
 
#include <list.h> 
 
#include <string.h> 
 
  
 
USING_NS_CC; 
 
using namespace cocos2d::extension; 
 
  
 
class ListViewTestLayer : public CCLayer , public CCListViewDelegate { 
 
public: 
 
ListViewTestLayer(); 
 
~ListViewTestLayer(); 
 
virtual bool init(); 
 
LAYER_NODE_FUNC(ListViewTestLayer); 
 
virtual void visit(); 
 
public: 
 
// 继承自CCListViewDelegate所需要实现的方法 
 
virtual void CCListView_numberOfCells(CCListView *listView, CCListViewProtrolData *data); 
 
virtual void CCListView_cellForRow(CCListView *listView, CCListViewProtrolData *data); 
 
virtual void CCListView_didClickCellAtRow(CCListView *listView, CCListViewProtrolData *data); 
 
virtual void CCListView_didScrollToRow(CCListView *listView, CCListViewProtrolData *data); 
 
  
 
private: 
 
//显示list的状态的一个lable 
 
CCLabelTTF *m_InfoLabel; 
 
private: 
 
// 存放的List数据 
 
std::list<std::string> *m_pDataList; 
 
CCListView *m_pListView; 
 
//是否刷新,即reload 
 
bool m_bFresh; 
 
void initData(); 
 
}; 
 
#endif 
cpp的实现:
[cpp] 
// 
 
//  ListViewTestLayer.cpp 
 
//  2dxDemo 
 
// 
 
//  Created by Yanghui Liu on 12-6-26. 
 
//  Copyright (c) 2012年 BoyoJoy. All rights reserved. 
 
// 
 
  
 
#include "ListViewTestLayer.h" 
 
#include "CCListViewCell.h" 
 
  
 
ListViewTestLayer::ListViewTestLayer(){ 
 

 
  
 
ListViewTestLayer::~ListViewTestLayer(){ 
 

 
  
 
void ListViewTestLayer::initData(){ 
 
m_bFresh = true; 
 
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 
 
m_pDataList = new std::list<std::string>; 
 
for (int i=0; i<15; i++) { 
 
char info[20]; 
 
sprintf(info, "My Cell %d", i); 
 
m_pDataList->push_back(info); 
 

 
// 初始化控件ListView 
 
CCListView *listView = CCListView::viewWithMode(CCListViewModeVertical); 
 
listView->setContentSize( CCSizeMake(winSize.width * .5, winSize.height)); 
 
listView->setDelegate(this); 
 
listView->setPosition(CCPointZero); 
 
this->addChild(listView); 
 
m_pListView = listView; 
 
// 初始化控件Label,显示ListView信息 
 
m_InfoLabel = CCLabelTTF::labelWithString("Info", "", 32); 
 
m_InfoLabel->setPosition(ccp(winSize.width * .8, winSize.height *.1)); 
 
this->addChild(m_InfoLabel); 
 

 
  
 
//visit方易做图在每一帧的时候调用,也可以自己注册schedule 
 
void ListViewTestLayer::visit(){ 
 
CCLayer::visit(); 
 
if (m_bFresh) { 
 
m_pListView->reload(); 
 
m_bFresh = false; 
 

 

 
  
 
//返回行数 
 
void ListViewTestLayer::CCListView_numberOfCells(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data){ 
 
data->nNumberOfRows = m_pDataList->size(); 
 

 
  
 
//构造每一个cell 
 
void ListViewTestLayer::CCListView_cellForRow(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data){ 
 
CCSize listSize = m_pListView->getContentSize(); 
 
CCSize cellSize = CCSizeMake(listSize.width, listSize.height / 5); 
 
CCListViewCell *cell = CCListViewCell::node(); 
 
cell->setOpacity(0); 
 
cell->setContentSize(cellSize); 
 
//cell选中颜色 
 
cell->setSelectionColor(ccc4(0, 255, 0, 255)); 
 
data->cell = cell; 
 
std::list<std::string>::iterator it = m_pDataList->begin(); 
 
for (int i=0; i<data->nRow; ++i) { 
 
++it; 
 

 
CCLabelTTF *cellLabel = CCLabelTTF::labelWithString(((std::string) *it).c_str(), "Arial", 32); 
 
cellLabel->setPosition(ccp(cellSize.width / 2, cellSize.height / 2)); 
 
cell->addChild(cellLabel); 
 

 
  
 
//cell被选中 
 
void ListViewTestLayer::CCListView_didClickCellAtRow(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data){ 
 
char info[100]; 
 
sprintf(info, "No. %d Row", data->nRow); 
 
m_InfoLabel->setString(info); 
 

 
  
 
//listView在滑动中 
 
void ListViewTestLayer::CCListView_didScrollToRow(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data){ 
 
m_InfoLabel->setString("Scrolling..."); 
 

 
  
 
bool ListViewTestLayer::init(){ 
 
if (!CCLayer::init()) { 
 
return false; 
 

 
initData(); 
 
return true; 
 

 
  

调用方法:
//list view
    ListViewTestLayer *listViewDemoLayer = ListViewTestLayer::node();
   

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,