Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

//
//  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所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。