之前写的ExpandedListVIEw版本由于版本升级这里提供Cocos2d-x 3.3Bate0 版本
代码下载:http://download.csdn.net/detail/qqmcy/8033343
下面介绍下如何使用。
先介绍下
DJDataObject存放数据模型的类
DayReportlistadapter 自定义的adapter或是iOS中的cell
ListVIEwTest 示例类。
下面上代码啦
DJDataObject.h
//// DJDataObject.h// testthirdone//// Created by 杜甲 on 14-6-7.////#ifndef __testthirdone__DJDataObject__#define __testthirdone__DJDataObject__#include "cocos2d.h"USING_NS_CC;class DJDataObject :public Ref{ public: CREATE_FUNC(DJDataObject); virtual bool init(); std::string name; std::vector<DJDataObject*> children; voID initWithnameAndChildren(std::string name,std::vector<DJDataObject*> data_vec); };#endif /* defined(__testthirdone__DJDataObject__) */
DJDataObject.cpp
//// DJDataObject.cpp// testthirdone//// Created by 杜甲 on 14-6-7.////#include "DJDataObject.h"bool DJDataObject::init(){ bool bRet = false; do { bRet = true; } while (0); return bRet;}voID DJDataObject::initWithnameAndChildren(std::string name,std::vector<DJDataObject*> data_vec){ this->name = name; this->children = data_vec; }
DayReportlistadapter.h
//// DayReportlistadapter.h// //// Created by 杜甲 on 14-6-4.////#ifndef __ht_mobile_cpp__DayReportlistadapter__#define __ht_mobile_cpp__DayReportlistadapter__#include "cocos2d.h"#include "ui/CocosGUI.h"#include "DJTreeNodeInfo.h"USING_NS_CC;class DayReportlistadapter :public ui::Layout{ public: CREATE_FUNC(DayReportlistadapter); virtual bool init(); ui::Text* organname; ui::Text* prem_day; ui::Text* prem_month; ui::ImageVIEw* in_Image; voID setBackGround(int treeDepthLevel); Vec2 preVec2; float preOffsetX; typedef std::function<voID(Ref*,Vec2 offset_vec2)> ccAdapterCallback; voID addEventListener(const ccAdapterCallback& callback); ccAdapterCallback _eventCallback; bool isMoving_HORIZONTAL; Size winSize; };#endif /* defined(__ht_mobile_cpp__DayReportlistadapter__) */
DayReportlistadapter.cpp
//// DayReportlistadapter.cpp// //// Created by 杜甲 on 14-6-4.////#include "DayReportlistadapter.h"#include "VisibleRect.h"bool DayReportlistadapter::init(){ bool bRet = false; do { CC_BREAK_IF(!ui::Layout::init()); setLayoutType(cocos2d::ui::Layout::Type::relative); setBackGroundcolorType(ui::Layout::BackGroundcolorType::SolID); winSize = Director::getInstance()->getWinSize(); settouchEnabled(true); float topLength = 30; in_Image = ui::ImageVIEw::create("Closenormal.png"); in_Image->setScale(VisibleRect::getimageScale()); in_Image->settouchEnabled(true); addChild(in_Image); auto rp_image = ui::relativeLayoutParameter::create(); rp_image->setrelativename("rp_image"); float offset = -in_Image->getContentSize().wIDth * 2/3 *(1- VisibleRect::getimageScale()); rp_image->setmargin(ui::margin(-in_Image->getContentSize().wIDth * 2/3 *(1- VisibleRect::getimageScale()),0)); rp_image->setAlign(cocos2d::ui::relativeLayoutParameter::relativeAlign::PARENT_left_CENTER_VERTICAL); in_Image->setLayoutParameter(rp_image); organname = ui::Text::create(); organname->setFontSize(38 * VisibleRect::getimageScale()); organname->setcolor(color3B::BLACK); addChild(organname); auto rp_organname = ui::relativeLayoutParameter::create(); rp_organname->setrelativename("rp_organname"); rp_organname->setrelativeToWidgetname("rp_image"); rp_organname->setmargin(ui::margin(1 * VisibleRect::getimageScale() + offset,topLength,0)); rp_organname->setAlign(cocos2d::ui::relativeLayoutParameter::relativeAlign::LOCATION_RIGHT_OF_CENTER); organname->setLayoutParameter(rp_organname); prem_month = ui::Text::create(); prem_month->setFontSize(38 * VisibleRect::getimageScale()); prem_month->setTextHorizontalAlignment(cocos2d::TextHAlignment::left); prem_month->setcolor(color3B::BLACK); addChild(prem_month); auto rp_prem_month = ui::relativeLayoutParameter::create(); rp_prem_month->setAlign(cocos2d::ui::relativeLayoutParameter::relativeAlign::PARENT_top_RIGHT); rp_prem_month->setrelativename("rp_prem_month"); rp_prem_month->setmargin(ui::margin(0,50*VisibleRect::getimageScale(),0)); prem_month->setLayoutParameter(rp_prem_month); auto center = ui::Layout::create(); center->setSize(Size(1,1)); addChild(center); auto rp_center = ui::relativeLayoutParameter::create(); rp_center->setrelativename("rp_center"); rp_center->setAlign(cocos2d::ui::relativeLayoutParameter::relativeAlign::CENTER_IN_PARENT); center->setLayoutParameter(rp_center); prem_day = ui::Text::create(); prem_day->setTextHorizontalAlignment(cocos2d::TextHAlignment::left); prem_day->setFontSize(38 * VisibleRect::getimageScale()); prem_day->setAnchorPoint(Vec2::ANCHOR_MIDDLE_left); prem_day->setSize(Size(100,50)); prem_day->setcolor(color3B::BLACK); addChild(prem_day); auto rp_prem_day = ui::relativeLayoutParameter::create(); rp_prem_day->setrelativeToWidgetname("rp_center"); rp_prem_day->setAlign(cocos2d::ui::relativeLayoutParameter::relativeAlign::PARENT_top_CENTER_HORIZONTAL); // rp_prem_day->setrelativeToWidgetname("rp_prem_month"); rp_prem_day->setmargin(ui::margin(30,0)); prem_day->setLayoutParameter(rp_prem_day); auto bottom_color = ui::Layout::create(); bottom_color->setSize(Size(winSize.wIDth,1)); bottom_color->setBackGroundcolorType(ui::Layout::BackGroundcolorType::SolID); bottom_color->setBackGroundcolor(color3B::BLACK); addChild(bottom_color); auto rp_bottom_color = ui::relativeLayoutParameter::create(); rp_bottom_color->setAlign(cocos2d::ui::relativeLayoutParameter::relativeAlign::PARENT_BottOM_CENTER_HORIZONTAL); rp_bottom_color->setmargin(ui::margin(0,1)); bottom_color->setLayoutParameter(rp_bottom_color); bRet = true; } while (0); return bRet;}voID DayReportlistadapter::setBackGround(int treeDepthLevel){ log("treeDepthLevel = %d",treeDepthLevel); switch (treeDepthLevel) { case 0: setBackGroundcolor(color3B(209,238,252)); break; case 1: setBackGroundcolor(color3B(224,248,216)); break; case 2: break; default: break; }}voID DayReportlistadapter::addEventListener(const ccAdapterCallback &callback){ _eventCallback = callback; }
ListVIEwTest.h
//// ListVIEwTest.h// testthirdone//// Created by 杜甲 on 14-6-9./////* * 示例Demo */#ifndef __testthirdone__ListVIEwTest__#define __testthirdone__ListVIEwTest__#include "cocos2d.h"#include "ui/CocosGUI.h"#include "DJDataObject.h"#include "DJListVIEw.h"USING_NS_CC;using namespace ui;class ListVIEwTest : public ui::Layout,public DJListVIEwDataSource,public DJListVIEwDelegate{public: CREATE_FUNC(ListVIEwTest); virtual bool init(); private: std::vector<DJDataObject*> data_vec; virtual ssize_t treeVIEwNumberOfChildrenOfItem(DJListVIEw *treeVIEw,voID *item) ; virtual ui::Layout *treeVIEwAndCellForItemAndTreeNodeInfo(DJListVIEw *treeVIEw,voID *item,DJTreeNodeInfo *treeNodeInfo); virtual voID* treeVIEwAndChildOfItem(DJListVIEw *treeVIEw,int index,voID *item); Size winSize; virtual voID treeVIEwWilldisplayCellForItemTreeNodeInfo(DJListVIEw *treeVIEw,ui::Layout *adapter,DJTreeNodeInfo *treeNodeInfo) ; };#endif /* defined(__testthirdone__ListVIEwTest__) */
ListVIEwTest.cpp
//// ListVIEwTest.cpp// testthirdone//// Created by 杜甲 on 14-6-9.////#include "ListVIEwTest.h"#include "DJTreeNode.h"#include "DJTreeNodeInfo.h"#include "DayReportlistadapter.h"bool ListVIEwTest::init(){ bool bRet = false; do { CC_BREAK_IF(!ui::Layout::init()); winSize = Director::getInstance()->getWinSize(); setBackGroundcolorType(ui::Layout::BackGroundcolorType::SolID); setBackGroundcolor(color3B(18,23,222)); std::vector<DJDataObject*>temp1; std::vector<DJDataObject*>temp2; std::vector<DJDataObject*>temp3; std::vector<DJDataObject*>temp4; DJDataObject* data7 = DJDataObject::create(); data7->retain(); //initWithnameAndChildren 参数1:当前数据内容, 参数2 :子集 data7->initWithnameAndChildren("数据1-1-1",temp4); temp1.push_back(data7); DJDataObject* data3 = DJDataObject::create(); data3->retain(); data3->initWithnameAndChildren("数据1-1",temp1); DJDataObject* data4 = DJDataObject::create(); data4->retain(); data4->initWithnameAndChildren("数据1-2",temp4); for (int i = 0; i < 7; i++) { DJDataObject* data6 = DJDataObject::create(); data6->retain(); data6->initWithnameAndChildren("数据h",temp3); temp2.push_back(data6); } DJDataObject* data1 = DJDataObject::create(); data1->retain(); data1->initWithnameAndChildren("数据r",temp2); DJDataObject* data = DJDataObject::create(); data->retain(); std::vector<DJDataObject*>temp; temp.push_back(data3); temp.push_back(data4); data->initWithnameAndChildren("数据12",temp); data_vec.push_back(data); data_vec.push_back(data1); auto winSize = Director::getInstance()->getWinSize(); auto ListVIEw1 = DJListVIEw::create(); ListVIEw1->setDJListVIEwDataSource(this); ListVIEw1->setDJListVIEwDelegate(this); ListVIEw1->setSize(winSize); ListVIEw1->addExpandedListVIEw(data_vec); addChild(ListVIEw1); bRet = true; } while (0); return bRet;}voID ListVIEwTest::treeVIEwWilldisplayCellForItemTreeNodeInfo(DJListVIEw *treeVIEw,DJTreeNodeInfo *treeNodeInfo){ if (treeNodeInfo->treeDepthLevel == 0) { adapter->setBackGroundcolor(color3B(150,100,200)); }else if (treeNodeInfo->treeDepthLevel == 1) { adapter->setBackGroundcolor(color3B(100,100)); }else if (treeNodeInfo->treeDepthLevel == 2) { adapter->setBackGroundcolor(color3B(100,150,150)); } }ssize_t ListVIEwTest::treeVIEwNumberOfChildrenOfItem(DJListVIEw *treeVIEw,voID *item){ if (item == nullptr) { return data_vec.size(); } DJDataObject* data = static_cast<DJDataObject*>(item); return data->children.size();}ui::Layout* ListVIEwTest::treeVIEwAndCellForItemAndTreeNodeInfo(DJListVIEw *treeVIEw,DJTreeNodeInfo *treeNodeInfo){ DJDataObject* dobject = static_cast<DJDataObject*>(item) ; auto tableLayout1 = DayReportlistadapter::create(); tableLayout1->setSize(Size(winSize.wIDth,100)); tableLayout1->setBackGroundcolorType(ui::Layout::BackGroundcolorType::SolID);// tableLayout1->setBackGround(treeNodeInfo->treeDepthLevel);// tableLayout1->setBackGroundcolor(color3B(189,203,222));// ListVIEw->pushBackCustomItem(tableLayout1); tableLayout1->organname->setString(dobject->name); tableLayout1->prem_day->setString(StringUtils::format("%d",333)); tableLayout1->prem_month->setString("fffff"); return tableLayout1;}voID* ListVIEwTest::treeVIEwAndChildOfItem(DJListVIEw *treeVIEw,voID *item){ DJDataObject* data = static_cast<DJDataObject*>(item); if (item == nullptr) { return data_vec.at( index ); } return data->children.at( index );}
效果:
总结
以上是内存溢出为你收集整理的Cocos2d-x 3.3Bate0 ExpandedListView全部内容,希望文章能够帮你解决Cocos2d-x 3.3Bate0 ExpandedListView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)