博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x-2.0 ListView使用
阅读量:2304 次
发布时间:2019-05-09

本文共 3917 字,大约阅读时间需要 13 分钟。

转载自

原文地址: 

我在上一篇文章中介绍了,现在把listView也讲解一下。由于cocos2d-x没有给出例子,所以代码有点长。我都写了注释,应该很容易理解。

ListViewTestLayer.h头文件

////  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 
#include
 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的状态的一个lableCCLabelTTF *m_InfoLabel;private:// 存放的List数据std::list
*m_pDataList;CCListView *m_pListView;//是否刷新,即reloadbool m_bFresh;void initData();};#endif

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
;for (int i=0; i<15; i++) {char info[20];sprintf(info, "My Cell %d", i);m_pDataList->push_back(info);}// 初始化控件ListViewCCListView *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方法会在每一帧的时候调用,也可以自己注册schedulevoid 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();} //构造每一个cellvoid 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
::iterator it = m_pDataList->begin();for (int i=0; i
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();

    addChild(listViewDemoLayer);

 

你可能感兴趣的文章
Web服务实现方案二:SOAP简介
查看>>
Web服务实现方案三:XML-RPC简介
查看>>
Web服务实现方案四:JSON-RPC简介
查看>>
WebService技术实现方案(转)
查看>>
If the server requires more time, try increasing the timeout in the server edito
查看>>
mybatis 里 foreach 里的坑
查看>>
史上最全设计模式导学目录(完整版)转
查看>>
简单工厂模式UML图形及代码
查看>>
策略模式UML图形及代码
查看>>
Eclipse中tomcat启动一直刷内部日志
查看>>
解决ArrayList线程不安全
查看>>
Eclipse下Java Build Path下Libraies中添加 Maven dependencies 失败解决方案
查看>>
POM下plugin报红色,展示错误如:Plugin execution not covered by lifecycle configuration: org...
查看>>
CompletionService 简介(转)
查看>>
Java Map遍历方式方式及性能测试(转)
查看>>
浅谈java分布式系统(转)
查看>>
leftjoinon后and和where后and区别例子
查看>>
用Collections.sort方法对list排序有两种方法(转)
查看>>
Error configuring application listener of class org.springframework.web.context.
查看>>
数据库建表注意事项(持续更新)
查看>>