当前位置:K88软件开发文章中心编程语言APP编程CrossApp → 文章内容

CAListView(列表)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:51:53

由 ?﹏???ζ???﹏﹏? 创建, 最后一次修改 2016-08-29 类说明CAListView和CAScrollView非常相似,只是其内部成列表状,支持水平方案和竖直方向的滑动。常用于一些列表信息的展示,如:通讯录、新闻列表、目录索引等。CAListView使用起来相对比较复杂,一般我们要同时使用CAListView、CAListViewCell、CAListViewDelegate、CAListViewDataSource来同时构建我们的列表界面,这么我们先分别了解一下它们的作用:CAListView就是列表控件,是显示列表的载体,它是由多个CAListViewCell列组成的。CAListViewCell是组成列表的每一个单元,下面我们都简称为cellCAListViewDelegate是CAListView的交互代理,主要代理选择cell和取消选择cell的事件CAListViewDataSource是CAListView的数据代理,主要代理cell的数量、cell的高度和将cell添加到CAListView显示。CAListView 属性(点击查看方法介绍)属性说明ListViewOrientationlistView的滚动方向ListViewDataSource添加数据代理ListViewDelegate添加交互代理ListHeaderView添加头部视图ListFooterView添加尾部视图SeparatorColor设置cell分割线的颜色ListHeaderHeight设置头部视图的高度ListFooterHeight设置尾部视图的高度SeparatorViewHeight设置cell分割线的高度AllowsHeadAndFootHover允许头和尾的悬停AllowsSelection是否开启cell选择AllowsMultipleSelection是否可以多选cellCAListView 方法(点击查看方法介绍)方法说明setAllowsSelection是否开启cell选择setAllowsMultipleSelection是否可以多选cellsetSelectAtIndex根据索引设置cell为选中状态setUnSelectAtIndex根据索引设置cell为未选中状态setShowsScrollIndicators设置显示滚动条dequeueReusableCellWithIdentifier可以重用单元标示符cellForRowAtIndex通过cell索引获取IndexswitchPCMode开关PC模式ccTouchBegan触摸事件开始时的回调函数ccTouchMoved触摸事件中触点移动时的回调函数ccTouchEnded触摸事件结束时的回调函数ccTouchCancelled触摸非正常结束时的回调函数。(例如:电话或锁屏)mouseMoved鼠标移动mouseMovedOutSide鼠标移出CAListViewDelegate 方法(点击查看方法介绍)方法说明listViewDidSelectCellAtIndex选中cell时调用listViewDidDeselectCellAtIndex取消选择cell时调用CAListViewDataSource 方法(点击查看方法介绍)方法说明numberOfIndexcell的总数量listViewHeightForIndexcell的高度listViewCellAtIndex添加生成celllistViewWillDisplayCellAtIndex回调当前将要显示的CAListViewCAListViewCell 属性(点击查看方法介绍)属性说明ContentView获得内容视图BackgroundView设置背景视图ReuseIdentifier设置重用标识符Index获得重用标识符ControlStateEffect设置控制状态效应AllowsSelectedCAListViewCell是否可以选择CAListViewCell 方法(点击查看方法介绍)方法说明create创建,默认Frame为(0,0,0,0)initWithReuseIdentifier重用标识符初始化了解CAListView的主要函数,我们来实现一个CAListView的列表视图。第一步:创建我们自己的cell我们需要创建一个先的class,我这里创建一个MyCell,并继承CAListViewCell。用于每个列表单元的布局显示,下面看一下MyCell.h和MyCell.cpp的代码实现。#pragma once#include "CrossApp.h"class MyCell : public CAListViewCell{public: MyCell(); ~MyCell(); //创建MyCell static MyCell* create(const std::string& identifier, const DRect& _rect = DRectZero); public: //初始化Cell void initWithCell(); //设置回调 void cellBtnCallback(CAControl* btn, DPoint point); protected: //正常状态 virtual void normalListViewCell(); //高亮状态 virtual void highlightedListViewCell(); //选中状态 virtual void selectedListViewCell(); //禁用状态 virtual void disabledListViewCell(); };MyCell.cpp代码如下:#include "MyCell.h" MyCell::MyCell(){} MyCell::~MyCell(){} MyCell* MyCell::create(const std::string& identifier, const DRect& _rect){ MyCell* listViewCell = new MyCell(); //设置重用标示符 if (listViewCell&&listViewCell->initWithReuseIdentifier(identifier)) { //设置Frame范围 listViewCell->setFrame(_rect); //设置为内存自动释放 listViewCell->autorelease(); return listViewCell; } //如果创建失败安全释放内存 CC_SAFE_DELETE(listViewCell); return NULL;} void MyCell::initWithCell(){ //获得当前的宽度 DSize _size = this->getFrame().size; //创建CALabel CALabel* test = CALabel::createWithCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width*0.8, _size.height)); test->setTextAlignment(CATextAlignmentCenter); test->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter); test->setFontSize(_px(40)); test->setTag(100); this->addSubview(test);} void MyCell::cellBtnCallback(CAControl* btn, DPoint point){ CCLog("MyCell::cellBtnCallback-->");} void MyCell::normalListViewCell(){ this->setBackgroundView(CAView::createWithColor(CAColor_white));} void MyCell::highlightedListViewCell(){ this->setBackgroundView(CAView::createWithColor(CAColor_yellow));} void MyCell::selectedListViewCell(){ this->setBackgroundView(CAView::createWithColor(CAColor_orange));} void MyCell::disabledListViewCell(){ this->setBackgroundView(CAView::createWithColor(CAColor_black));}CAListView 属性介绍ListViewOrientation 类型:CAListViewOrientation*解释:listView的滚动方向。set/get{}。ListViewDataSource类型:CAListViewDataSource*解释:添加数据代理。set/get{}。ListViewDelegate    类型:CAListViewDelegate*解释:添加交互代理。set/get{}。ListHeaderView      类型:CA

[1] [2]  下一页


CAListView(列表)