Cocos2d-x 3.3Bate0 ExpandedListView
Previously written ExpandedListView version due to version upgrade here provides Cocos2d-x 3.3Bate0 version
The following describes how to use it.
First introduce
Classes in which DJDataObject stores data models
DayReportListAdapter custom adapter or cell in iOS
ListViewTest example class.
The code below
DJDataObject. h
//// DJDataObject. h // testthirdone /// Created by du jia on 14-6-7. /// # ifndef _ testthirdone _ DJDataObject __# define _ testthirdone _ DJDataObject __# include cocos2d. hUSING_NS_CC; class DJDataObject: public Ref {public: CREATE_FUNC (DJDataObject); virtual bool init (); std: string name; std: vector
Children; void initWithNameAndChildren (std: string name, std: vector
Data_vec) ;};# endif/* defined (_ testthirdone _ DJDataObject __)*/
DJDataObject. cpp
//// DJDataObject. cpp // testthirdone /// Created by du jia on 14-6-7. //// # include DJDataObject. hbool DJDataObject: init () {bool bRet = false; do {bRet = true;} while (0); return bRet;} void DJDataObject: initWithNameAndChildren (std :: string name, std: vector
Data_vec) {this-> name = name; this-> children = data_vec ;}
DayReportListAdapter. h
/// DayReportListAdapter. h ///// Created by du jia on 14-6-4. /// # ifndef _ ht_mobile_cpp _ DayReportListAdapter __# define _ ht_mobile_cpp _ DayReportListAdapter __# include cocos2d. h # include ui/CocosGUI. h # include DJTreeNodeInfo. hUSING_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
CcAdapterCallback; void addEventListener (const ccAdapterCallback & callback); ccAdapterCallback _ eventCallback; bool callback; Size winSize ;}; # endif/* defined (_ future _ DayReportListAdapter __)*/
DayReportListAdapter. cpp
/// DayReportListAdapter. cpp //// Created by du jia on 14-6-4. //// # include DayReportListAdapter. h # include VisibleRect. hbool 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, 0 )); rp_organName-> setAlign (cocos2d: ui: RelativeLayoutParameter: RelativeAlign: inline); organName-> setLayoutParameter (rp_organName); prem_month = ui: Text: create (); prem_month-> setFontSize (38 * VisibleRect: getImageScale (); prem_month-> values (cocos2d: TextHAlignment: LEFT); prem_month-> setColor (Color3B: BLACK ); addChild (prem_month); auto rp_prem_month = ui: RelativeLayoutParameter: create (); rp_prem_month-> setAlign (cocos2d: ui: prem_month: RelativeAlign: PARENT_TOP_RIGHT ); rp_prem_month-> setRelativeName (rp_prem_month); rp_prem_month-> setMargin (ui: Margin (0, topLength, 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: relativeal :: 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: hour); // rp_prem_day-> hour (rp_prem_month); rp_prem_day-> setMargin (ui :: margin (30, topLength, 0, 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: tags: create (); rp_bottom_color-> setAlign (cocos2d: ui: tags: RelativeAlign: tags); rp_bottom_color-> setMargin (ui:: Margin (,); 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 du jia on 14-6-9. //// ** Demo example */# ifndef _ testthirdone _ ListViewTest __# define _ testthirdone _ ListViewTest __# include cocos2d. h # include ui/CocosGUI. h # include DJDataObject. h # include DJListView. hUSING_NS_CC; using namespace ui; class ListViewTest: public ui: Layout, public DJListViewDataSource, public DJListViewDelegate {public: CREATE_FUNC (ListViewTest); virtual bool init (); private: std: vector
Data_vec; virtual ssize_t treeViewNumberOfChildrenOfItem (DJListView * treeView, void * item); virtual ui: Layout * Layout (DJListView * treeView, void * item, DJTreeNodeInfo * treeNodeInfo ); virtual void * treeViewAndChildOfItem (DJListView * treeView, int index, void * item); Size winSize; virtual void Preview (DJListView * treeView, ui: Layout * adapter, void * item, DJTreeNodeInfo * treeNodeInfo);}; # endif/* defined (_ testthirdone _ ListViewTest __)*/
ListViewTest. cpp
//// ListViewTest. cpp // testthirdone /// Created by du jia on 14-6-9. //// # include ListViewTest. h # include DJTreeNode. h # include DJTreeNodeInfo. h # include DayReportListAdapter. hbool 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
Temp1; std: vector
Temp2; std: vector
Temp3; std: vector
Temp4; DJDataObject * data7 = DJDataObject: create (); data7-> retain (); // initWithNameAndChildren parameter 1: current data content, parameter 2: subset data7-> initWithNameAndChildren (data 1-1-1, temp4); temp1.push _ back (data7); DJDataObject * data3 = DJDataObject: create (); data3-> retain (); data3-> initWithNameAndChildren (data 1-1, temp1); DJDataObject * data4 = DJDataObject: create (); data4-> retain (); data4-> initWithNameAndChildren (data 1-2, temp4); for (int I = 0; I <7; I ++) {DJDataObject * data6 = DJDataObject: create (); data6-> retain (); data6-> initWithNameAndChildren (Data h, temp3); temp2.push _ back (data6);} DJDataObject * data1 = DJDataObject: create (); data1-> retain (); data1-> initWithNameAndChildren (data r, temp2); DJDataObject * data = DJDataObject: create (); data-> retain (); std: vector
Temp; temp. push_back (data3); temp. push_back (data4); data-> initWithNameAndChildren (data 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: callback (DJListView * treeView, ui :: layout * adapter, void * item, DJTreeNodeInfo * treeNodeInfo) {if (treeNodeInfo-> treeDepthLevel = 0) {adapter-> setBackGroundColor (Color3B (150,100,200 ));} else if (treeNodeInfo-> treeDepthLevel = 1) {adapter-> setBackGroundColor (Color3B (100,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
(Item); return data-> children. size ();} ui: Layout * ListViewTest: treeViewAndCellForItemAndTreeNodeInfo (DJListView * treeView, void * item, DJTreeNodeInfo * treeNodeInfo) {DJDataObject * dobject = static_cast
(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, int index, void * item) {DJDataObject * data = static_cast
(Item); if (item = nullptr) {return data_vec.at (index);} return data-> children. at (index );}
Effect: