Cocos2d-x 3.3Bate0 ExpandedListView

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.3Bate0 ExpandedListView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

之前写的ExpandedListView版本由于版本升级这里提供Cocos2d-x 3.3Bate0 版本


代码下载:http://download.csdn.net/detail/qqmcy/8033343




下面介绍下如何使用。

先介绍下

DJDataObject存放数据模型的类

DayReportListAdapter 自定义的adapter或是iOS中的cell

ListViewTest 示例类。

下面上代码

DJDataObject.h

  1. //
  2. // DJDataObject.h
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-7.
  6. //
  7. //
  8.  
  9. #ifndef __testthirdone__DJDataObject__
  10. #define __testthirdone__DJDataObject__
  11.  
  12. #include "cocos2d.h"
  13.  
  14.  
  15. USING_NS_CC;
  16.  
  17. class DJDataObject :public Ref
  18. {
  19. public:
  20. CREATE_FUNC(DJDataObject);
  21. virtual bool init();
  22. std::string name;
  23. std::vector<DJDataObject*> children;
  24. void initWithNameAndChildren(std::string name,std::vector<DJDataObject*> data_vec);
  25. };
  26.  
  27. #endif /* defined(__testthirdone__DJDataObject__) */

DJDataObject.cpp

  1. //
  2. // DJDataObject.cpp
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-7.
  6. //
  7. //
  8.  
  9. #include "DJDataObject.h"
  10.  
  11. bool DJDataObject::init()
  12. {
  13. bool bRet = false;
  14. do {
  15. bRet = true;
  16. } while (0);
  17. return bRet;
  18. }
  19.  
  20. void DJDataObject::initWithNameAndChildren(std::string name,std::vector<DJDataObject*> data_vec)
  21. {
  22. this->name = name;
  23. this->children = data_vec;
  24. }

DayReportListAdapter.h

  1. //
  2. // DayReportListAdapter.h
  3. //
  4. //
  5. // Created by 杜甲 on 14-6-4.
  6. //
  7. //
  8.  
  9. #ifndef __ht_mobile_cpp__DayReportListAdapter__
  10. #define __ht_mobile_cpp__DayReportListAdapter__
  11.  
  12. #include "cocos2d.h"
  13. #include "ui/CocosGUI.h"
  14. #include "DJTreeNodeInfo.h"
  15.  
  16. USING_NS_CC;
  17.  
  18. class DayReportListAdapter :public ui::Layout
  19. {
  20. public:
  21. CREATE_FUNC(DayReportListAdapter);
  22. virtual bool init();
  23. ui::Text* organName;
  24. ui::Text* prem_day;
  25. ui::Text* prem_month;
  26. ui::ImageView* in_Image;
  27. void setBackGround(int treeDepthLevel);
  28. Vec2 preVec2;
  29. float preOffsetX;
  30. typedef std::function<void(Ref*,Vec2 offset_vec2)> ccAdapterCallback;
  31. void addEventListener(const ccAdapterCallback& callback);
  32. ccAdapterCallback _eventCallback;
  33. bool isMoving_HORIZONTAL;
  34. Size winSize;
  35. };
  36.  
  37.  
  38.  
  39.  
  40.  
  41. #endif /* defined(__ht_mobile_cpp__DayReportListAdapter__) */

DayReportListAdapter.cpp

  1. //
  2. // DayReportListAdapter.cpp
  3. //
  4. //
  5. // Created by 杜甲 on 14-6-4.
  6. //
  7. //
  8.  
  9. #include "DayReportListAdapter.h"
  10.  
  11. #include "VisibleRect.h"
  12.  
  13.  
  14. bool DayReportListAdapter::init()
  15. {
  16. bool bRet = false;
  17. do {
  18. CC_BREAK_IF(!ui::Layout::init());
  19. setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);
  20. setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  21. winSize = Director::getInstance()->getWinSize();
  22. setTouchEnabled(true);
  23. float topLength = 30;
  24. in_Image = ui::ImageView::create("CloseNormal.png");
  25. in_Image->setScale(VisibleRect::getImageScale());
  26. in_Image->setTouchEnabled(true);
  27. addChild(in_Image);
  28. auto rp_image = ui::RelativeLayoutParameter::create();
  29. rp_image->setRelativeName("rp_image");
  30. float offset = -in_Image->getContentSize().width * 2/3 *(1- VisibleRect::getImageScale());
  31. rp_image->setMargin(ui::Margin(-in_Image->getContentSize().width * 2/3 *(1- VisibleRect::getImageScale()),0));
  32. rp_image->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL);
  33. in_Image->setLayoutParameter(rp_image);
  34. organName = ui::Text::create();
  35. organName->setFontSize(38 * VisibleRect::getImageScale());
  36. organName->setColor(Color3B::BLACK);
  37. addChild(organName);
  38. auto rp_organName = ui::RelativeLayoutParameter::create();
  39. rp_organName->setRelativeName("rp_organName");
  40. rp_organName->setRelativeToWidgetName("rp_image");
  41. rp_organName->setMargin(ui::Margin(1 * VisibleRect::getImageScale() + offset,topLength,0));
  42. rp_organName->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER);
  43. organName->setLayoutParameter(rp_organName);
  44. prem_month = ui::Text::create();
  45. prem_month->setFontSize(38 * VisibleRect::getImageScale());
  46. prem_month->setTextHorizontalAlignment(cocos2d::TextHAlignment::LEFT);
  47. prem_month->setColor(Color3B::BLACK);
  48. addChild(prem_month);
  49. auto rp_prem_month = ui::RelativeLayoutParameter::create();
  50. rp_prem_month->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT);
  51. rp_prem_month->setRelativeName("rp_prem_month");
  52. rp_prem_month->setMargin(ui::Margin(0,50*VisibleRect::getImageScale(),0));
  53. prem_month->setLayoutParameter(rp_prem_month);
  54. auto center = ui::Layout::create();
  55. center->setSize(Size(1,1));
  56. addChild(center);
  57. auto rp_center = ui::RelativeLayoutParameter::create();
  58. rp_center->setRelativeName("rp_center");
  59. rp_center->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT);
  60. center->setLayoutParameter(rp_center);
  61. prem_day = ui::Text::create();
  62. prem_day->setTextHorizontalAlignment(cocos2d::TextHAlignment::LEFT);
  63. prem_day->setFontSize(38 * VisibleRect::getImageScale());
  64. prem_day->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
  65. prem_day->setSize(Size(100,50));
  66. prem_day->setColor(Color3B::BLACK);
  67. addChild(prem_day);
  68. auto rp_prem_day = ui::RelativeLayoutParameter::create();
  69. rp_prem_day->setRelativeToWidgetName("rp_center");
  70. rp_prem_day->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL);
  71. // rp_prem_day->setRelativeToWidgetName("rp_prem_month");
  72. rp_prem_day->setMargin(ui::Margin(30,0));
  73. prem_day->setLayoutParameter(rp_prem_day);
  74. auto bottom_color = ui::Layout::create();
  75. bottom_color->setSize(Size(winSize.width,1));
  76. bottom_color->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  77. bottom_color->setBackGroundColor(Color3B::BLACK);
  78. addChild(bottom_color);
  79. auto rp_bottom_color = ui::RelativeLayoutParameter::create();
  80. rp_bottom_color->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL);
  81. rp_bottom_color->setMargin(ui::Margin(0,1));
  82. bottom_color->setLayoutParameter(rp_bottom_color);
  83. bRet = true;
  84. } while (0);
  85. return bRet;
  86.  
  87. }
  88.  
  89.  
  90. void DayReportListAdapter::setBackGround(int treeDepthLevel)
  91. {
  92. log("treeDepthLevel = %d",treeDepthLevel);
  93. switch (treeDepthLevel) {
  94. case 0:
  95. setBackGroundColor(Color3B(209,238,252));
  96. break;
  97. case 1:
  98. setBackGroundColor(Color3B(224,248,216));
  99.  
  100. break;
  101. case 2:
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107.  
  108. void DayReportListAdapter::addEventListener(const ccAdapterCallback &callback)
  109. {
  110. _eventCallback = callback;
  111. }


ListViewTest.h

  1. //
  2. // ListViewTest.h
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-9.
  6. //
  7. //
  8.  
  9. /*
  10. * 示例Demo
  11. */
  12.  
  13. #ifndef __testthirdone__ListViewTest__
  14. #define __testthirdone__ListViewTest__
  15.  
  16. #include "cocos2d.h"
  17. #include "ui/CocosGUI.h"
  18. #include "DJDataObject.h"
  19. #include "DJListView.h"
  20.  
  21.  
  22. USING_NS_CC;
  23. using namespace ui;
  24.  
  25. class ListViewTest : public ui::Layout,public DJListViewDataSource,public DJListViewDelegate
  26. {
  27. public:
  28. CREATE_FUNC(ListViewTest);
  29. virtual bool init();
  30. private:
  31. std::vector<DJDataObject*> data_vec;
  32. virtual ssize_t treeViewNumberOfChildrenOfItem(DJListView *treeView,void *item) ;
  33. virtual ui::Layout *treeViewAndCellForItemAndTreeNodeInfo(DJListView *treeView,void *item,DJTreeNodeInfo *treeNodeInfo);
  34. virtual void* treeViewAndChildOfItem(DJListView *treeView,int index,void *item);
  35. Size winSize;
  36. virtual void treeViewWillDisplayCellForItemTreeNodeInfo(DJListView *treeView,ui::Layout *adapter,DJTreeNodeInfo *treeNodeInfo) ;
  37. };
  38.  
  39.  
  40. #endif /* defined(__testthirdone__ListViewTest__) */

ListViewTest.cpp

  1. //
  2. // ListViewTest.cpp
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-9.
  6. //
  7. //
  8.  
  9. #include "ListViewTest.h"
  10. #include "DJTreeNode.h"
  11. #include "DJTreeNodeInfo.h"
  12. #include "DayReportListAdapter.h"
  13.  
  14.  
  15.  
  16. bool ListViewTest::init()
  17. {
  18. bool bRet = false;
  19. do {
  20. CC_BREAK_IF(!ui::Layout::init());
  21. winSize = Director::getInstance()->getWinSize();
  22. setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  23. setBackGroundColor(Color3B(18,23,222));
  24. std::vector<DJDataObject*>temp1;
  25. std::vector<DJDataObject*>temp2;
  26. std::vector<DJDataObject*>temp3;
  27. std::vector<DJDataObject*>temp4;
  28. DJDataObject* data7 = DJDataObject::create();
  29. data7->retain();
  30. //initWithNameAndChildren 参数1:当前数据内容, 参数2 :子集
  31. data7->initWithNameAndChildren("数据1-1-1",temp4);
  32. temp1.push_back(data7);
  33. DJDataObject* data3 = DJDataObject::create();
  34. data3->retain();
  35. data3->initWithNameAndChildren("数据1-1",temp1);
  36. DJDataObject* data4 = DJDataObject::create();
  37. data4->retain();
  38. data4->initWithNameAndChildren("数据1-2",temp4);
  39. for (int i = 0; i < 7; i++)
  40. {
  41. DJDataObject* data6 = DJDataObject::create();
  42. data6->retain();
  43. data6->initWithNameAndChildren("数据h",temp3);
  44. temp2.push_back(data6);
  45. }
  46. DJDataObject* data1 = DJDataObject::create();
  47. data1->retain();
  48. data1->initWithNameAndChildren("数据r",temp2);
  49. DJDataObject* data = DJDataObject::create();
  50. data->retain();
  51. std::vector<DJDataObject*>temp;
  52. temp.push_back(data3);
  53. temp.push_back(data4);
  54. data->initWithNameAndChildren("数据12",temp);
  55. data_vec.push_back(data);
  56. data_vec.push_back(data1);
  57.  
  58. auto winSize = Director::getInstance()->getWinSize();
  59. auto listView1 = DJListView::create();
  60. listView1->setDJListViewDataSource(this);
  61. listView1->setDJListViewDelegate(this);
  62. listView1->setSize(winSize);
  63. listView1->addExpandedListView(data_vec);
  64. addChild(listView1);
  65. bRet = true;
  66. } while (0);
  67. return bRet;
  68. }
  69.  
  70. void ListViewTest::treeViewWillDisplayCellForItemTreeNodeInfo(DJListView *treeView,DJTreeNodeInfo *treeNodeInfo)
  71. {
  72. if (treeNodeInfo->treeDepthLevel == 0) {
  73. adapter->setBackGroundColor(Color3B(150,100,200));
  74. }else if (treeNodeInfo->treeDepthLevel == 1)
  75. {
  76. adapter->setBackGroundColor(Color3B(100,100));
  77. }else if (treeNodeInfo->treeDepthLevel == 2)
  78. {
  79. adapter->setBackGroundColor(Color3B(100,150,150));
  80. }
  81. }
  82.  
  83. ssize_t ListViewTest::treeViewNumberOfChildrenOfItem(DJListView *treeView,void *item)
  84. {
  85. if (item == nullptr) {
  86. return data_vec.size();
  87. }
  88. DJDataObject* data = static_cast<DJDataObject*>(item);
  89. return data->children.size();
  90. }
  91.  
  92. ui::Layout* ListViewTest::treeViewAndCellForItemAndTreeNodeInfo(DJListView *treeView,DJTreeNodeInfo *treeNodeInfo)
  93. {
  94. DJDataObject* dobject = static_cast<DJDataObject*>(item) ;
  95. auto tableLayout1 = DayReportListAdapter::create();
  96. tableLayout1->setSize(Size(winSize.width,100));
  97. tableLayout1->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  98. // tableLayout1->setBackGround(treeNodeInfo->treeDepthLevel);
  99. // tableLayout1->setBackGroundColor(Color3B(189,203,222));
  100. // listView->pushBackCustomItem(tableLayout1);
  101. tableLayout1->organName->setString(dobject->name);
  102. tableLayout1->prem_day->setString(StringUtils::format("%d",333));
  103. tableLayout1->prem_month->setString("fffff");
  104. return tableLayout1;
  105. }
  106. void* ListViewTest::treeViewAndChildOfItem(DJListView *treeView,void *item)
  107. {
  108. DJDataObject* data = static_cast<DJDataObject*>(item);
  109. if (item == nullptr) {
  110. return data_vec.at( index );
  111. }
  112. return data->children.at( index );
  113. }
  114.  
  115.  
  116.  
  117.  


效果

猜你在找的Cocos2d-x相关文章