cocos2d-x-2.0 ListView使用

时间:2022-06-08 12:08:27

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

cocos2d-x-2.0 ListView使用

ListViewTestLayer.h头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
 
//  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>
 
&nbsp;
 
USING_NS_CC;
 
using  namespace  cocos2d::extension;
 
&nbsp;
 
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);
 
&nbsp;
 
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的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
 
//  ListViewTestLayer.cpp
 
//  2dxDemo
 
//
 
//  Created by Yanghui Liu on 12-6-26.
 
//  Copyright (c) 2012年 BoyoJoy. All rights reserved.
 
//
 
&nbsp;
 
#include "ListViewTestLayer.h"
 
#include "CCListViewCell.h"
 
&nbsp;
 
ListViewTestLayer::ListViewTestLayer(){
 
}
 
&nbsp;
 
ListViewTestLayer::~ListViewTestLayer(){
 
}
 
&nbsp;
 
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);
 
}
 
&nbsp;
 
//visit方法会在每一帧的时候调用,也可以自己注册schedule
 
void  ListViewTestLayer::visit(){
 
CCLayer::visit();
 
if  (m_bFresh) {
 
m_pListView->reload();
 
m_bFresh = false ;
 
}
 
}
 
&nbsp;
 
//返回行数
 
void  ListViewTestLayer::CCListView_numberOfCells(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data){
 
data->nNumberOfRows = m_pDataList->size();
 
}
 
&nbsp;
 
//构造每一个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);
 
}
 
&nbsp;
 
//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);
 
}
 
&nbsp;
 
//listView在滑动中
 
void  ListViewTestLayer::CCListView_didScrollToRow(cocos2d::extension::CCListView *listView, cocos2d::extension::CCListViewProtrolData *data){
 
m_InfoLabel->setString( "Scrolling..." );
 
}
 
&nbsp;
 
bool  ListViewTestLayer::init(){
 
if  (!CCLayer::init()) {
 
return  false ;
 
}
 
initData();
 
return  true ;
 
}
 
&nbsp;

调用方法:

//list view

ListViewTestLayer *listViewDemoLayer = ListViewTestLayer::node();

addChild(listViewDemoLayer);