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,0,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::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,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::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,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::RelativeLayoutParameter::create();
        rp_bottom_color->setAlign(cocos2d::ui::RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL);
        rp_bottom_color->setMargin(ui::Margin(0,0,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 , void *item , 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, 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<DJDataObject*>(item);
    return data->children.size();
}

ui::Layout* ListViewTest::treeViewAndCellForItemAndTreeNodeInfo(DJListView *treeView , void *item , 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 , int index , void *item)
{
    DJDataObject* data = static_cast<DJDataObject*>(item);
    if (item == nullptr) {
        return data_vec.at( index );
    }
    return data->children.at( index );
}

效果:

时间: 2024-10-01 02:49:02

Cocos2d-x 3.3Bate0 ExpandedListView的相关文章

在Win7上搭建Cocos-2D开发环境

注:公司最近开了一个新的项目,我从页游再次转入手游,不过这次用的是COCOS-2D技术,不是U3D. 主要语言Lua , 利用Lua来调用Cocos引擎的C++接口.当然还有其他的方案,这里不讲. 好了,话不多说,准备搭建. 1:安装相关的工具 ①:SublimeText软件   :  用来编辑Lua ②:Python2.7.8软件    :  创建Cocos项目时,需要用到其DOS命令 ③:CocosStutio软件    :  UI编辑器 ④:VS2012软件           :  编译

cocos2d::Vector

v3.0 beta加入 定义在"COCOS2DX_ROOT/cocos/base"的"CCVector.h"头文件中. template<class T>class CC_DLL Vector; cocos2d::Vector<T>是一个封装好的能动态增长顺序访问的容器. cocos2d::Vector<T>中的元素是按序存取的,它的低层实现数据结构是标准模版库中的标准顺序容器std::vector. 在Cocos2d-x v3.

Cocos2D中Action的进阶使用技巧(一)

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 大家对Cocos2d中动作的使用大概都非常清楚了,事实上本身action的概念也不复杂. 可是在某些情况下,一般的动作不能满足我们需求的时候,我们就必须使用更高级的Action方法来解决这个问题. 比方,串行化(不是序列化哦,这是两个全然不同的概念)不同Action的执行-有些童鞋可能会说非常easy,直接用CCActionSequence不就结了,可是等等我

Cocos2d入门--3-- 向量的应用

 Cocos2d入门--3-- 向量的应用 小球向一个方向持续运动的Demo HelloWorldScene.h ...... //设置一个protected的属性 protected: cocos2d::Vec2 _vec; ...... HelloWorldScene.cpp //这个是通过随机数设置向量的方向 _vec.set(random(-0.1f, 1.0f), random(-1.0f, 1.0f)); //通过normalize这个标准化函数,能够使得向量的大小为1 _vec.n

Cocos2d入门--1-- 初涉相关属性或代码

 Cocos2d入门--1-- 初涉相关属性或代码 Cocos2d vision:  cocos2d-x-3.8.1 万丈高楼,起于累土.对于一个游戏框架的学习,其实在于框架功能的使用积累,学会了如何在cocos2d游戏引擎的基础上使用它提供的各种功能,并灵活运用, 以及学会查阅Cocos2d官方提供的API文档.相信自己也能开发出自己喜爱或者让别人羡慕的游戏. 目录: 1>认识origin和visibleSize以及cocos2d的基础绘画类DrawNode的简单实用 2>认识 CCLOG(

UITableViewController 滚动引起的cocos2d动画暂停问题的解决

UITableViewController 滚动引起的cocos2d动画暂停问题的解决 之前在使用UITableViewController进行滚动时,cocos2d的动画会暂停,直至滚动完毕才会继续动画. 后来发现可以这样解决,想要了解更多,请查阅NSRunLoop相关知识. // .h NSTimer* m_timer; // .m- (void)updateCCDirector { [[CCDirector sharedDirector] drawScene]; if (!self.tab

cocos2d 游戏开发实战

文章转自:http://uliweb.clkg.org/tutorial/read/40 6   cocos2d 游戏开发实战 6.1   创建cocos2d项目 6.2   cocos2d v3 "hello world" 6.2.1   显示一个飞船 6.3   精灵 6.4   開始 space viking 之旅 6.4.1   添加 sneakyinput 6.5   精灵的移动效果,旋转效果 6.6   定时器效果 6.7   启动 cocos2d,默认竖屏 6.8   检

cocos2d::Map

v3.0 beta加入 定义在"COCOS2DX_ROOT/cocos/base"的"CCMap.h"头文件中. template <class K, class V> class CC_DLL Map; cocos2d::Map<K,V>是使用std::unordered_map作为底层结构的关联式容器. 而std::unordered_map是一个存储键值对的关联式容器,它可以通过它们的键快速检索对应的值. 使用unordered_map

cocos2d::Value

cocos2d::Value 于v3.0beta加入 定义在"COCOS2DX_ROOT/cocos/base"的头文件"CCValue.h"中 class Value; cocos2d::Value是许多基本类型(int,float,double,bool,unsigned char,char*和std::string)还有std::vector<Value>, std::unordered_map<std::string,Value>和s