ListView的使用

来源:互联网 发布:现在开淘宝网店赚钱吗 编辑:程序博客网 时间:2024/06/06 05:25

// 创建一个ListView

    auto listView = ListView::create();

    // 设置ListView的方向,本例为VERTICAL(垂直)

    listView->setDirection(ScrollView::Direction::VERTICAL);

    // 设置ListView的背景图片

    listView->setBackGroundImage("green_edit.png");

    // 设置背景图片作为九宫格填充

    listView->setBackGroundImageScale9Enabled(true);

    // 设置ListViewContentSize

    listView->setContentSize(Size(480, 260));

    // 设置ListView的位置

    listView->setPosition(Vec2((visibleSize.width - listView->getContentSize().width) / 2.0f,

                               (visibleSize.height - listView->getContentSize().height) / 2.0f));

    // 添加事件监听器(ListView

    listView->addEventListener([=](Ref *pSender, ListView::EventType type){

    switch (type)

    {

    case ListView::EventType::ON_SELECTED_ITEM_START:

{  

ListView* listView = static_cast<ListView*>(pSender);

    log("select child start index = %ld", listView->getCurSelectedIndex());

       break;

    }

    case ListView::EventType::ON_SELECTED_ITEM_END:

    {

    ListView* listView = static_cast<ListView*>(pSender);

    log("select child end index = %ld",                 listView->getCurSelectedIndex());

    break;

  }

 default:

 break;

}

});

    // 添加事件监听器(ScrollView)

listView->addEventListener([=](Ref* pSender, ScrollView::EventType type){

switch (type) {

case ScrollView::EventType::SCROLL_TO_BOTTOM:

   log("SCROLL_TO_BOTTOM");

   break;

   case ScrollView::EventType::SCROLL_TO_TOP:

   log("SCROLL_TO_TOP");

   break;

   default:

   break;

}

});

    // ListView添加为当前层的子节点

    this->addChild(listView);

    // 添加自定义item10Button

    for (int i = 0; i < 10; ++i)

{

        // 创建一个Layout,用来添加Button

        Layout *custom_item = Layout::create();

        // 设置LayoutContentSizeButtonContentSize一致

        custom_item->setContentSize(custom_button->getContentSize());

        // Layout添加为ListView的子节点

        listView->addChild(custom_item);

 

        // 创建一个Button

        Button* custom_button = Button::create("button.png", "buttonHighlighted.png");

        // 设置ButtonName

        custom_button->setName("Title Button");

        // 设置Button是否九宫格填充

        custom_button->setScale9Enabled(true);

        // 设置ButtonContentSize

        custom_button->setContentSize(Size(200, 60));

        // 设置ButtonTitleText为对应_array的文本内容

        custom_button->setTitleText(StringUtils::format("listview_item_%d", i));

        // 设置Button的文本字体大小

        custom_button->setTitleFontSize(24);

        // 设置Layout的坐标位置

        custom_button->setPosition(Vec2(custom_item->getContentSize().width / 2.0f, custom_item->getContentSize().height / 2.0f));

        // Button添加为Layout的字节

        custom_item->addChild(custom_button);

}

原创粉丝点击