在左Android開始有SDK提供ExpandListView的可扩展列表,而在iOS下有很多第三方做好的Demo,这里我是參照iOS下RATreeView这个第三方库实现的。

本文代码:须要在3.1以上版本号执行。

假设是用3.0版本号须要将Vec2换成Piont

原文地址:http://blog.csdn.net/qqmcy/article/details/29559241

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

以下说下用法:

DJDataObject.h   数据模型类

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

DJDataObject.cpp

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

ListViewTest.h  可扩展列表的使用类,将这个布局addChild到场景中就OK。

  1. //
  2. // ListViewTest.h
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-9.
  6. //
  7. //
  8. #ifndef __testthirdone__ListViewTest__
  9. #define __testthirdone__ListViewTest__
  10. #include "cocos2d.h"
  11. #include "ui/CocosGUI.h"
  12. #include "DJDataObject.h"
  13. USING_NS_CC;
  14. using namespace ui;
  15. class ListViewTest : public ui::Layout
  16. {
  17. public:
  18. CREATE_FUNC(ListViewTest);
  19. virtual bool init();
  20. std::vector<DJDataObject*> data_vec;
  21. };
  22. #endif /* defined(__testthirdone__ListViewTest__) */

ListViewTest.cpp

  1. //
  2. // ListViewTest.cpp
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-9.
  6. //
  7. //
  8. #include "ListViewTest.h"
  9. #include "DJTreeNode.h"
  10. #include "DJTreeNodeInfo.h"
  11. #include "DJListView.h"
  12. bool ListViewTest::init()
  13. {
  14. bool bRet = false;
  15. do {
  16. CC_BREAK_IF(!ui::Layout::init());
  17. setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  18. setBackGroundColor(Color3B(18, 23, 222));
  19. std::vector<DJDataObject*>temp1;
  20. std::vector<DJDataObject*>temp2;
  21. std::vector<DJDataObject*>temp3;
  22. std::vector<DJDataObject*>temp4;
  23. DJDataObject* data7 = DJDataObject::create();
  24. data7->retain();
  25. //initWithNameAndChildren 參数1:当前数据内容。 參数2 :子集
  26. data7->initWithNameAndChildren("数据1-1-1", temp4);
  27. temp1.push_back(data7);
  28. DJDataObject* data3 = DJDataObject::create();
  29. data3->retain();
  30. data3->initWithNameAndChildren("数据1-1", temp1);
  31. DJDataObject* data4 = DJDataObject::create();
  32. data4->retain();
  33. data4->initWithNameAndChildren("数据1-2", temp4);
  34. for (int i = 0; i < 7; i++)
  35. {
  36. DJDataObject* data6 = DJDataObject::create();
  37. data6->retain();
  38. data6->initWithNameAndChildren("数据h", temp3);
  39. temp2.push_back(data6);
  40. }
  41. DJDataObject* data1 = DJDataObject::create();
  42. data1->retain();
  43. data1->initWithNameAndChildren("数据r", temp2);
  44. DJDataObject* data = DJDataObject::create();
  45. data->retain();
  46. std::vector<DJDataObject*>temp;
  47. temp.push_back(data3);
  48. temp.push_back(data4);
  49. data->initWithNameAndChildren("数据1", temp);
  50. data_vec.push_back(data);
  51. data_vec.push_back(data1);
  52. auto winSize = Director::getInstance()->getWinSize();
  53. auto listView1 = DJListView::create();
  54. listView1->setSize(winSize);
  55. listView1->addExpandedListView(data_vec);
  56. addChild(listView1);
  57. bRet = true;
  58. } while (0);
  59. return bRet;
  60. }

这里帖出的是用法,稍后我提供实现的类的下载地址。

实现效果:

之后我会把以下的实现传上来,如今网络有限制不让传。

这里也贴出实现类

DJTreeNodeInfo.h 节点信息类

  1. //
  2. // DJTreeNodeInfo.h
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-6.
  6. //
  7. //
  8. #ifndef __testthirdone__DJTreeNodeInfo__
  9. #define __testthirdone__DJTreeNodeInfo__
  10. #include "cocos2d.h"
  11. USING_NS_CC;
  12. class DJTreeNode;
  13. class DJTreeNodeInfo :public Ref
  14. {
  15. public:
  16. CREATE_FUNC(DJTreeNodeInfo);
  17. virtual bool init();
  18. bool expanded;
  19. int treeDepthLevel;
  20. int siblingsNumber;
  21. int positionInsiblings;
  22. DJTreeNodeInfo* parent;
  23. DJTreeNode* parentTreeNode;
  24. std::vector<DJTreeNodeInfo*> children;
  25. void* item;
  26. std::vector<DJTreeNode*> childrenTreeNodes;
  27. void initWithParent(DJTreeNode* parent1 , std::vector<DJTreeNode*> children1);
  28. DJTreeNodeInfo* getParent();
  29. std::vector<DJTreeNodeInfo*> getChildren();
  30. };
  31. #endif /* defined(__testthirdone__DJTreeNodeInfo__) */

DJTreeNodeInfo.cpp

  1. //
  2. // DJTreeNodeInfo.cpp
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-6.
  6. //
  7. //
  8. #include "DJTreeNodeInfo.h"
  9. #include "DJTreeNode.h"
  10. bool DJTreeNodeInfo::init()
  11. {
  12. bool bRet = false;
  13. do {
  14. bRet = true;
  15. } while (0);
  16. return bRet;
  17. }
  18. void DJTreeNodeInfo::initWithParent(DJTreeNode *parent1, std::vector<DJTreeNode *> children1)
  19. {
  20. this->parentTreeNode = parent1;
  21. this->childrenTreeNodes = children1;
  22. }
  23. DJTreeNodeInfo* DJTreeNodeInfo::getParent()
  24. {
  25. if (this->parent == nullptr)
  26. {
  27. this->parent = this->parentTreeNode->treeNodeInfo1();
  28. }
  29. return nullptr;
  30. }
  31. std::vector<DJTreeNodeInfo*> DJTreeNodeInfo::getChildren()
  32. {
  33. if (this->children.size() == 0 )
  34. {
  35. std::vector<DJTreeNodeInfo*> treeNodesInfos;
  36. for (int i = 0; i < this->childrenTreeNodes.size(); i++) {
  37. DJTreeNode* treeNode = this->childrenTreeNodes.at(i);
  38. treeNodesInfos.push_back(treeNode->treeNodeInfo1());
  39. this->children = treeNodesInfos;
  40. }
  41. }
  42. return this->children;
  43. }

DJTreeNode.h  节点类

  1. //
  2. // DJTreeNode.h
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-6.
  6. //
  7. //
  8. #ifndef __testthirdone__DJTreeNode__
  9. #define __testthirdone__DJTreeNode__
  10. #include "cocos2d.h"
  11. USING_NS_CC;
  12. class DJTreeNodeInfo;
  13. class DJTreeNode :public Ref{
  14. public:
  15. CREATE_FUNC(DJTreeNode);
  16. virtual bool init();
  17. bool expanded;
  18. bool visible;
  19. DJTreeNode* parent;
  20. std::vector<DJTreeNode*> children;
  21. DJTreeNodeInfo* treeNodeInfo;
  22. DJTreeNodeInfo* treeNodeInfo1();
  23. void* item;
  24. int treeDepthLevel;
  25. void initWithItem(void* item , DJTreeNode* parent , bool expanded);
  26. void addChildNode(DJTreeNode* child);
  27. std::vector<DJTreeNode*> visibleDescendants();
  28. int numberOfVisibleDescendants();
  29. void collapse();
  30. void expand();
  31. int startIndex();
  32. int endIndex();
  33. bool isVisible();
  34. int treeDepthLevel1();
  35. };
  36. #endif /* defined(__testthirdone__DJTreeNode__) */

DJTreeNode.cpp

  1. //
  2. // DJTreeNode.cpp
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-6.
  6. //
  7. //
  8. #include "DJTreeNode.h"
  9. #include "DJTreeNodeInfo.h"
  10. typedef enum DJTreeDepthLevel {
  11. DJTreeDepthLevelNotInitialized
  12. } DJTreeDepthLevel;
  13. bool DJTreeNode::init()
  14. {
  15. bool bRet = false;
  16. do {
  17. bRet = true;
  18. } while (0);
  19. return bRet;
  20. }
  21. void DJTreeNode::initWithItem(void *item, DJTreeNode *parent, bool expanded)
  22. {
  23. this->treeDepthLevel = DJTreeDepthLevelNotInitialized;
  24. this->item = item;
  25. this->parent = parent;
  26. this->expanded = expanded;
  27. }
  28. bool DJTreeNode::isVisible()
  29. {
  30. return this->parent->expanded || this->parent == nullptr;
  31. }
  32. void DJTreeNode::addChildNode(DJTreeNode *child)
  33. {
  34. // if (this->children.size() > 0) {
  35. // this->children.clear();
  36. // }
  37. std::vector<DJTreeNode*> children1(this->children);
  38. children1.push_back(child);
  39. this->children = children1;
  40. }
  41. int DJTreeNode::numberOfVisibleDescendants()
  42. {
  43. return (int)visibleDescendants().size();
  44. }
  45. std::vector<DJTreeNode*> DJTreeNode::visibleDescendants()
  46. {
  47. std::vector<DJTreeNode*> visibleDescendants;
  48. try {
  49. if (expanded)
  50. {
  51. if (this->children.size() > 0)
  52. {
  53. // log("children.size() = %lu",children.size());
  54. for (int i = 0; i < this->children.size(); i++)
  55. {
  56. // log("i = %d",i);
  57. DJTreeNode* treeNode = children.at(i);
  58. visibleDescendants.push_back(treeNode);
  59. if (treeNode->expanded)
  60. {
  61. std::vector<DJTreeNode*> tree_vec = treeNode->visibleDescendants();
  62. for (int i = 0; i < tree_vec.size(); i++)
  63. {
  64. visibleDescendants.push_back(tree_vec.at(i));
  65. }
  66. }
  67. }
  68. }
  69. }
  70. } catch (std::out_of_range & exc) {
  71. log("exc.what() = %s",exc.what());
  72. }
  73. // log("visibleDescendants = %zd",visibleDescendants.size());
  74. return visibleDescendants;
  75. }
  76. void DJTreeNode::expand()
  77. {
  78. this->expanded = true;
  79. if (this->parent != nullptr) {
  80. this->parent->expand();
  81. }
  82. }
  83. void DJTreeNode::collapse()
  84. {
  85. this->expanded = false;
  86. for (int i = 0; i < children.size(); i++)
  87. {
  88. DJTreeNode* treeNode = children.at(i);
  89. treeNode->collapse();
  90. }
  91. }
  92. int DJTreeNode::startIndex()
  93. {
  94. int startIndex;
  95. if (this->parent->parent == nullptr) {
  96. startIndex = 0;
  97. }else{
  98. startIndex = this->parent->startIndex() + 1;
  99. }
  100. for (int i = 0 ; i < this->parent->children.size() ; i++)
  101. {
  102. DJTreeNode* treeNode = this->parent->children.at(i);
  103. if (treeNode != this) {
  104. startIndex += 1;
  105. if (treeNode->expanded) {
  106. startIndex += treeNode->numberOfVisibleDescendants();
  107. }
  108. }
  109. else
  110. {
  111. break;
  112. }
  113. }
  114. return startIndex;
  115. }
  116. int DJTreeNode::endIndex()
  117. {
  118. int startIndex = this->startIndex();
  119. return startIndex + numberOfVisibleDescendants();
  120. }
  121. DJTreeNodeInfo* DJTreeNode::treeNodeInfo1()
  122. {
  123. if (this->treeNodeInfo == nullptr)
  124. {
  125. DJTreeNodeInfo* treeNodeInfo = DJTreeNodeInfo::create();
  126. treeNodeInfo->initWithParent(this->parent, this->children);
  127. treeNodeInfo->treeDepthLevel = treeDepthLevel1();
  128. treeNodeInfo->siblingsNumber = (int)this->parent->children.size();
  129. for (int i = 0; i < this->parent->children.size(); i++)
  130. {
  131. if (this->parent->children.at(i) == this)
  132. {
  133. treeNodeInfo->positionInsiblings = i;
  134. }
  135. }
  136. this->treeNodeInfo = treeNodeInfo;
  137. }
  138. this->treeNodeInfo->item = this->item;
  139. this->treeNodeInfo->expanded = this->expanded;
  140. return this->treeNodeInfo;
  141. }
  142. int DJTreeNode::treeDepthLevel1()
  143. {
  144. if (this->treeDepthLevel == DJTreeDepthLevelNotInitialized)
  145. {
  146. int treeDepthLevel = 0;
  147. DJTreeNode* current = this->parent->parent;
  148. while (current != nullptr) {
  149. treeDepthLevel++;
  150. current = current->parent;
  151. }
  152. this->treeDepthLevel = treeDepthLevel;
  153. }
  154. return this->treeDepthLevel;
  155. }

DJTreeNodeCollectionController.h  节点控制类

  1. //
  2. // DJTreeNodeCollectionController.h
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-6.
  6. //
  7. //
  8. #ifndef __testthirdone__DJTreeNodeCollectionController__
  9. #define __testthirdone__DJTreeNodeCollectionController__
  10. #include "cocos2d.h"
  11. USING_NS_CC;
  12. class DJTreeNode;
  13. class DJTreeNodeInfo;
  14. class DJTreeNodeCollectionController : public Ref
  15. {
  16. public:
  17. CREATE_FUNC(DJTreeNodeCollectionController);
  18. virtual bool init();
  19. DJTreeNode* root;
  20. void addTreeNode(DJTreeNode* treeNode);
  21. DJTreeNode* treeNodeForIndex(int index);
  22. DJTreeNode* treeNodeForIndex(int index , DJTreeNode* currentTreeNode);
  23. int indexForItem(void* item);
  24. int indexForItem(void* item , DJTreeNode* currentTreeNode);
  25. };
  26. #endif /* defined(__testthirdone__DJTreeNodeCollectionController__) */

DJTreeNodeCollectionController.cpp

  1. //
  2. // DJTreeNodeCollectionController.cpp
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-6.
  6. //
  7. //
  8. #include "DJTreeNodeCollectionController.h"
  9. #include "DJTreeNode.h"
  10. bool DJTreeNodeCollectionController::init()
  11. {
  12. bool bRet = false;
  13. do {
  14. root = DJTreeNode::create();
  15. root->retain();
  16. root->initWithItem(nullptr, nullptr, true);
  17. bRet = true;
  18. } while (0);
  19. return bRet;
  20. }
  21. void DJTreeNodeCollectionController::addTreeNode(DJTreeNode *treeNode)
  22. {
  23. if (treeNode->parent == nullptr)
  24. {
  25. this->root->addChildNode(treeNode);
  26. treeNode->parent = this->root;
  27. }else
  28. {
  29. treeNode->parent->addChildNode(treeNode);
  30. if (treeNode->expanded)
  31. {
  32. treeNode->expand();
  33. }
  34. }
  35. }
  36. DJTreeNode* DJTreeNodeCollectionController::treeNodeForIndex(int index)
  37. {
  38. if (index < 0)
  39. {
  40. return nullptr;
  41. }
  42. return treeNodeForIndex(index, this->root);
  43. }
  44. DJTreeNode* DJTreeNodeCollectionController::treeNodeForIndex(int index, DJTreeNode *currentTreeNode)
  45. {
  46. for (int i = 0; i < currentTreeNode->children.size(); i++)
  47. {
  48. DJTreeNode* treeNode = currentTreeNode->children.at(i);
  49. if (treeNode->startIndex() == index)
  50. {
  51. return treeNode;
  52. }else if (index <= treeNode->endIndex())
  53. {
  54. return treeNodeForIndex(index, treeNode);
  55. }
  56. }
  57. return nullptr;
  58. }
  59. int DJTreeNodeCollectionController::indexForItem(void *item)
  60. {
  61. return indexForItem(item, this->root);
  62. }
  63. int DJTreeNodeCollectionController::indexForItem(void *item, DJTreeNode *currentTreeNode)
  64. {
  65. std::vector<DJTreeNode*> array = this->root->visibleDescendants();
  66. for (int i = 0; i < array.size(); i++)
  67. {
  68. DJTreeNode* treeNode = array.at(i);
  69. if (treeNode->item == item)
  70. {
  71. return i;
  72. }
  73. }
  74. return -1;
  75. }

DayReportListAdapter.h  

  1. //
  2. // DayReportListAdapter.h
  3. //
  4. //
  5. // Created by 杜甲 on 14-6-4.
  6. //
  7. //
  8. #ifndef __ht_mobile_cpp__DayReportListAdapter__
  9. #define __ht_mobile_cpp__DayReportListAdapter__
  10. #include "cocos2d.h"
  11. #include "../../cocos2d/cocos/ui/CocosGUI.h"
  12. USING_NS_CC;
  13. class DayReportListAdapter :public ui::Layout
  14. {
  15. public:
  16. CREATE_FUNC(DayReportListAdapter);
  17. virtual bool init();
  18. ui::Text* organName;
  19. ui::Text* prem_day;
  20. ui::Text* prem_month;
  21. };
  22. #endif /* defined(__ht_mobile_cpp__DayReportListAdapter__) */

DayReportListAdapter.cpp

  1. //
  2. // DayReportListAdapter.cpp
  3. //
  4. //
  5. // Created by 杜甲 on 14-6-4.
  6. //
  7. //
  8. #include "DayReportListAdapter.h"
  9. bool DayReportListAdapter::init()
  10. {
  11. bool bRet = false;
  12. do {
  13. CC_BREAK_IF(!ui::Layout::init());
  14. setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);
  15. float topLength = 30;
  16. organName = ui::Text::create();
  17. organName->setFontSize(30);
  18. organName->setColor(Color3B::BLACK);
  19. addChild(organName);
  20. auto rp_organName = ui::RelativeLayoutParameter::create();
  21. rp_organName->setMargin(ui::Margin(30,topLength,0,0));
  22. rp_organName->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT);
  23. organName->setLayoutParameter(rp_organName);
  24. prem_month = ui::Text::create();
  25. prem_month->setFontSize(30);
  26. prem_month->setColor(Color3B::BLACK);
  27. addChild(prem_month);
  28. auto rp_prem_month = ui::RelativeLayoutParameter::create();
  29. rp_prem_month->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT);
  30. rp_prem_month->setRelativeName("rp_prem_month");
  31. rp_prem_month->setMargin(ui::Margin(0,topLength,50,0));
  32. prem_month->setLayoutParameter(rp_prem_month);
  33. prem_day = ui::Text::create();
  34. prem_day->setFontSize(30);
  35. prem_day->setColor(Color3B::BLACK);
  36. addChild(prem_day);
  37. auto rp_prem_day = ui::RelativeLayoutParameter::create();
  38. rp_prem_day->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL);
  39. // rp_prem_day->setRelativeToWidgetName("rp_prem_month");
  40. rp_prem_day->setMargin(ui::Margin(30,topLength,0,0));
  41. prem_day->setLayoutParameter(rp_prem_day);
  42. bRet = true;
  43. } while (0);
  44. return bRet;
  45. }

DJListView.h   

  1. //
  2. // DJListView.h
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-8.
  6. //
  7. //
  8. #ifndef __testthirdone__DJListView__
  9. #define __testthirdone__DJListView__
  10. #include "cocos2d.h"
  11. #include "ui/CocosGUI.h"
  12. #include "DJDataObject.h"
  13. #include "DJExpandListView.h"
  14. USING_NS_CC;
  15. using namespace ui;
  16. class DJListView :public ui::Layout
  17. {
  18. public:
  19. CREATE_FUNC(DJListView);
  20. virtual bool init();
  21. void selectedItemEvent(Ref *pSender, cocos2d::ui::ListView::EventType type);
  22. Size winSize;
  23. std::vector<DJDataObject*> data_vec;
  24. void addExpandedListView( std::vector<DJDataObject*> data_vec);
  25. private:
  26. ui::ListView* listView;
  27. ssize_t numberOfCellsInListView(void *item);
  28. void* treeViewItem( int index, void *item);
  29. DJTreeNodeCollectionController* treeNodeCollectionController;
  30. std::vector<void*> childrenForItem(void *item);
  31. void setupTreeStructure();
  32. void setupTreeStructureWithParentNode(DJTreeNode* parentTreeNode ,int treeDepthLevel);
  33. void collapseCellForTreeNode(cocos2d::Ref *pSender,DJTreeNode* treeNode);
  34. DJTreeNode* treeNodeForIndex(int index);
  35. void expandCellForTreeNode(cocos2d::Ref *pSender , DJTreeNode* treeNode);
  36. };
  37. #endif /* defined(__testthirdone__DJListView__) */

DJListView.cpp

  1. //
  2. // DJListView.cpp
  3. // testthirdone
  4. //
  5. // Created by 杜甲 on 14-6-8.
  6. //
  7. //
  8. #include "DJListView.h"
  9. #include "DayReportListAdapter.h"
  10. #include "DJTreeNode.h"
  11. #include "DJTreeNodeInfo.h"
  12. #include "DJTreeNodeCollectionController.h"
  13. bool DJListView::init()
  14. {
  15. bool bRet = false;
  16. do {
  17. CC_BREAK_IF(!ui::Layout::init());
  18. setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);
  19. winSize = Director::getInstance()->getWinSize();
  20. bRet = true;
  21. } while (0);
  22. return bRet;
  23. }
  24. void DJListView::addExpandedListView( std::vector<DJDataObject*> data_vec1)
  25. {
  26. data_vec = data_vec1;
  27. listView = ui::ListView::create();
  28. listView->setDirection(cocos2d::ui::ScrollView::Direction::VERTICAL);
  29. listView->setTouchEnabled(true);
  30. listView->setBounceEnabled(true);
  31. listView->setSize(Size(winSize.width,winSize.height - 100));
  32. listView->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  33. listView->setBackGroundColor(Color3B::WHITE);
  34. listView->addEventListener(CC_CALLBACK_2(DJListView::selectedItemEvent, this));
  35. auto rp_listView = ui::RelativeLayoutParameter::create();
  36. rp_listView->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL);
  37. listView->setLayoutParameter(rp_listView);
  38. setupTreeStructure();
  39. // set model
  40. for (int i = 0; i < treeNodeCollectionController->root->numberOfVisibleDescendants(); i++)
  41. {
  42. DJDataObject* dobject = static_cast<DJDataObject*>(treeNodeForIndex(i)->item) ;
  43. auto tableLayout1 = DayReportListAdapter::create();
  44. tableLayout1->setSize(Size(winSize.width, 1));
  45. tableLayout1->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  46. tableLayout1->setBackGroundColor(Color3B(189, 203, 222));
  47. listView->pushBackCustomItem(tableLayout1);
  48. tableLayout1->organName->setString(dobject->name);
  49. tableLayout1->prem_day->setString(StringUtils::format("%d",i));
  50. tableLayout1->prem_month->setString("fffff");
  51. }
  52. auto tableLayout2 = DayReportListAdapter::create();
  53. tableLayout2->setSize(Size(winSize.width, 1));
  54. tableLayout2->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  55. tableLayout2->setBackGroundColor(Color3B(189, 203, 222));
  56. listView->pushBackCustomItem(tableLayout2);
  57. auto layout21 = ui::Layout::create();
  58. layout21->setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);
  59. layout21->setSize(winSize);
  60. layout21->addChild(listView);
  61. layout21->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  62. layout21->setBackGroundColor(Color3B(89, 203, 222));
  63. auto tableLayout1 = ui::Layout::create();
  64. tableLayout1->setSize(winSize);
  65. tableLayout1->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  66. tableLayout1->setBackGroundColor(Color3B(189, 203, 222));
  67. //listView->pushBackCustomItem(tableLayout1);
  68. listView->setGravity(cocos2d::ui::ListView::Gravity::CENTER_VERTICAL);
  69. listView->setItemsMargin(100.0f);
  70. auto page = ui::PageView::create();
  71. page->setLayoutType(cocos2d::ui::Layout::Type::RELATIVE);
  72. page->setSize(winSize);
  73. page->addPage(layout21);
  74. page->addPage(tableLayout1);
  75. addChild(page);
  76. auto rp_page = ui::RelativeLayoutParameter::create();
  77. rp_page->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL);
  78. page->setLayoutParameter(rp_page);
  79. }
  80. void DJListView::selectedItemEvent(cocos2d::Ref *pSender, cocos2d::ui::ListView::EventType type)
  81. {
  82. switch (type) {
  83. case ui::ListView::EventType::ON_SELECTED_ITEM_END:
  84. {
  85. ui::ListView* listView = static_cast<ui::ListView*>(pSender);
  86. DJTreeNode* treeNode = treeNodeForIndex((int)listView->getCurSelectedIndex());
  87. if (treeNode->children.size() == 0)
  88. {
  89. return;
  90. }
  91. if (treeNode->expanded) {
  92. collapseCellForTreeNode(pSender, treeNode);
  93. }else{
  94. log("treeNode->expanded = %d",treeNode->expanded);
  95. expandCellForTreeNode(pSender, treeNode);
  96. }
  97. listView->refreshView();
  98. }
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. ssize_t DJListView::numberOfCellsInListView(void *item)
  105. {
  106. if (item == nullptr) {
  107. log("%zd",data_vec.size());
  108. return data_vec.size();
  109. }
  110. DJDataObject* data = static_cast<DJDataObject*>(item);
  111. return data->children.size();
  112. }
  113. void* DJListView::treeViewItem( int index, void *item)
  114. {
  115. DJDataObject* data = static_cast<DJDataObject*>(item);
  116. if (item == nullptr) {
  117. return data_vec.at( index );
  118. }
  119. return data->children.at( index );
  120. }
  121. std::vector<void*> DJListView::childrenForItem(void *item)
  122. {
  123. std::vector<void*> children ;
  124. ssize_t numberOfChildren = numberOfCellsInListView(item);
  125. for (int i = 0; i< numberOfChildren ; i++)
  126. {
  127. children.push_back(treeViewItem(i, item));
  128. }
  129. return children;
  130. }
  131. DJTreeNode* DJListView::treeNodeForIndex(int index)
  132. {
  133. return treeNodeCollectionController->treeNodeForIndex(index);
  134. }
  135. void DJListView::setupTreeStructure()
  136. {
  137. treeNodeCollectionController = DJTreeNodeCollectionController::create();
  138. treeNodeCollectionController->retain();
  139. setupTreeStructureWithParentNode(nullptr, 0);
  140. }
  141. void DJListView::setupTreeStructureWithParentNode(DJTreeNode* parentTreeNode ,int treeDepthLevel)
  142. {
  143. std::vector<void*> children;
  144. if (parentTreeNode == nullptr)
  145. {
  146. children = childrenForItem(nullptr);
  147. }else
  148. {
  149. children = childrenForItem(parentTreeNode->item);
  150. }
  151. log("setupTreeStructureWithParentNode 循环前");
  152. try {
  153. for (int i = 0; i < children.size(); i++)
  154. {
  155. log(" i = %d",i);
  156. void* item = children.at(i);
  157. DJTreeNode* treeNode = DJTreeNode::create();
  158. treeNode->retain();
  159. treeNode->initWithItem(item, parentTreeNode, false);
  160. setupTreeStructureWithParentNode(treeNode, treeDepthLevel + 1);
  161. treeNodeCollectionController->addTreeNode(treeNode);
  162. }
  163. } catch (std::out_of_range & exc)
  164. {
  165. log("exc.what() = %s",exc.what());
  166. }
  167. }
  168. void DJListView::collapseCellForTreeNode(cocos2d::Ref *pSender,DJTreeNode* treeNode)
  169. {
  170. log("treeNode->startIndex() = %d , treeNode->endIndex() = %d",treeNode->startIndex(),treeNode->endIndex());
  171. for (int index = treeNode->startIndex() + 1; index <= treeNode->endIndex(); index++)
  172. {
  173. ui::ListView* listView1 = static_cast<ui::ListView*>(pSender);
  174. listView1->removeItem(treeNode->startIndex() + 1);
  175. }
  176. treeNode->collapse();
  177. }
  178. void DJListView::expandCellForTreeNode(cocos2d::Ref *pSender , DJTreeNode* treeNode)
  179. {
  180. log("treeNode->startIndex() = %d , treeNode->endIndex() = %d",treeNode->startIndex(),treeNode->endIndex());
  181. treeNode->expand();
  182. // ui::ListView* listView1 = static_cast<ui::ListView*>(pSender);
  183. // listView1->insertDefaultItem(0);
  184. for (int index = treeNode->startIndex() + 1; index <= treeNode->endIndex();index++ )
  185. {
  186. DJDataObject* dobject = static_cast<DJDataObject*>(treeNodeForIndex(index)->item) ;
  187. auto tableLayout1 = DayReportListAdapter::create();
  188. tableLayout1->setSize(Size(winSize.width, 1));
  189. tableLayout1->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID);
  190. tableLayout1->setBackGroundColor(Color3B(189, 203, 222));
  191. listView->insertCustomItem(tableLayout1, index);
  192. //pushBackCustomItem(tableLayout1);
  193. tableLayout1->organName->setString(dobject->name);
  194. tableLayout1->prem_day->setString(StringUtils::format("%d",index));
  195. tableLayout1->prem_month->setString("fffff");
  196. }
  197. }

cocos2d-x3.1 下实现相似Android下ExpandListView的效果的更多相关文章

  1. Android下Cocos2d创建HelloWorld工程

    最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...

  2. 【FAQ】Ubuntu环境下ant编译android代码问题

    在Ubuntu14.04环境下,编译android程序时候,运行ant debug的时候出现如下异常:

  3. Android下/data/data/<package_name>/files读写权限

    今天将更新模块拿到android上面测试的时候,发现在创建writablepath.."upd/"目录的时候出现Permission Denied提示BTW:我使用的是lfs来创建 ...

  4. Linux学习心得之 Linux下命令行Android开发环境的搭建

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...

  5. Android下读取logcat的信息

    有时我们需要在程序执行进程中遇到一些异常,需要收集一logcat的信息,android下就可以使用以下方法获取: private static String getLogcatInfo(){ Stri ...

  6. Android下OpenCV的环境搭建

    目录(?)[-] 前言 系统环境 相关工具 Android ADT环境搭建 Android SDK环境变量的配置 Android NDK的安装与配置 OpenCV for Android 环境搭建 基 ...

  7. Android下添加新的自定义键值和按键处理流程

            Android下添加新的自定义键值和按键处理流程     说出来不怕大家笑话,我写这篇博客的原因在于前几天去一个小公司面试Android系统工程师,然后在面试的时候对方的技术总监问了我 ...

  8. Android下的数据储存方式(三)

      Android下最好的数据储存方式:关系型数据库sqlite.   数据库的创建:使用SqliteOpenHelper类 结合SqliteOpenHelper类和SQLiteDatabase类的帮 ...

  9. [转]Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能

    版权声明:本文出自郭霖的博客,转载必须注明出处. 转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9255575 最近项目中需要用到L ...

随机推荐

  1. 破解MyEclipse

  2. 后台返回的值ajax接收不到

    原因有很多种可能,我遇到的是后台写的Controller忘记了加@ResponseBody,导致springMVC把返回的字符串当成view了

  3. ESC/POS 控制指令

    ESC/POS  控制指令 HT 横向跳格 [名称] Horizontal tab [格式] ASCII HT Hex 09 Decimal 9 [描述] 将当前位置移动到下一个跳格位置. [注释] ...

  4. CentOS6 克 隆

    原始机子关机 自己设置名字 保存地址 开机 配置hosts   后面的为你要设置的名字不配置可能xshell链接上不了网 更改名字: 配置网卡 删除物理地址 mac 和  uuid 删除网卡 重启

  5. SecureCRT 连接 CentOS虚拟机

    SecureCRT 连接 CentOS虚拟机 1.安装SecureCRT SecureCRT是一款支持SSH等协议的终端仿真软件,可以在windows下登录Linux服务器,这样大大方便了开发工作.安 ...

  6. core核心模块

    5. core核心模块 核心模块会通过compiler模块提供的调用compiler的功能, 将用户的输入转为VM直接的输入 编译模块用来编译, 而核心模块用来执行 在core.h文件中 // 不需要 ...

  7. nyoj 220——推桌子——————【贪心】

    推桌子 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The famous ACM (Advanced Computer Maker) Company has re ...

  8. 白话SpringCloud | 第五章:服务容错保护(Hystrix)

    前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用.在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如 ...

  9. D3(v5) in TypeScript 坐标轴之 饼状图生成

    饼状图生成时依旧遇到了类型问题,记录如下: import * as d3 from 'd3'; import * as React from 'react'; class TestGraph exte ...

  10. 深入理解JavaScript系列(18):面向对象编程之ECMAScript实现(推荐)

    介绍 本章是关于ECMAScript面向对象实现的第2篇,第1篇我们讨论的是概论和CEMAScript的比较,如果你还没有读第1篇,在进行本章之前,我强烈建议你先读一下第1篇,因为本篇实在太长了(35 ...