2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
#include "ui/UIListView.h"
|
|
|
|
#include "ui/UIHelper.h"
|
2013-10-15 18:00:03 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
namespace ui {
|
2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(ListView)
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView::ListView():
|
2013-11-14 11:37:46 +08:00
|
|
|
_model(nullptr),
|
2014-05-12 09:51:23 +08:00
|
|
|
_gravity(Gravity::CENTER_VERTICAL),
|
2013-11-08 20:29:49 +08:00
|
|
|
_itemsMargin(0.0f),
|
2013-12-23 15:02:52 +08:00
|
|
|
_curSelectedIndex(0),
|
2014-05-12 09:51:23 +08:00
|
|
|
_refreshViewDirty(true),
|
2014-10-30 22:25:46 +08:00
|
|
|
_listViewEventListener(nullptr),
|
|
|
|
_listViewEventSelector(nullptr),
|
2014-05-12 09:51:23 +08:00
|
|
|
_eventCallback(nullptr)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-06-24 15:51:14 +08:00
|
|
|
this->setTouchEnabled(true);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView::~ListView()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-14 11:37:46 +08:00
|
|
|
_listViewEventListener = nullptr;
|
|
|
|
_listViewEventSelector = nullptr;
|
2013-12-23 15:02:52 +08:00
|
|
|
_items.clear();
|
2014-03-31 11:34:26 +08:00
|
|
|
CC_SAFE_RELEASE(_model);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView* ListView::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ListView* widget = new (std::nothrow) ListView();
|
2013-09-13 22:20:20 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool ListView::init()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (ScrollView::init())
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-05-12 18:00:54 +08:00
|
|
|
setLayoutType(Type::VERTICAL);
|
2013-09-13 22:20:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::setItemModel(Widget *model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_model);
|
|
|
|
_model = model;
|
|
|
|
CC_SAFE_RETAIN(_model);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::updateInnerContainerSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
switch (_direction)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::VERTICAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
size_t length = _items.size();
|
2013-12-23 15:02:52 +08:00
|
|
|
float totalHeight = (length - 1) * _itemsMargin;
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& item : _items)
|
2013-12-23 15:02:52 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
totalHeight += item->getContentSize().height;
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
float finalWidth = _contentSize.width;
|
2013-11-06 16:04:06 +08:00
|
|
|
float finalHeight = totalHeight;
|
2013-12-23 15:02:52 +08:00
|
|
|
setInnerContainerSize(Size(finalWidth, finalHeight));
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
size_t length = _items.size();
|
2013-12-23 15:02:52 +08:00
|
|
|
float totalWidth = (length - 1) * _itemsMargin;
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& item : _items)
|
2013-12-23 15:02:52 +08:00
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
totalWidth += item->getContentSize().width;
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
float finalWidth = totalWidth;
|
2014-06-20 10:40:16 +08:00
|
|
|
float finalHeight = _contentSize.height;
|
2013-12-23 15:02:52 +08:00
|
|
|
setInnerContainerSize(Size(finalWidth, finalHeight));
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::remedyLayoutParameter(Widget *item)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!item)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
switch (_direction)
|
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::VERTICAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-05-16 10:33:28 +08:00
|
|
|
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter());
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!llp)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
|
2014-12-16 16:44:04 +08:00
|
|
|
switch (_gravity)
|
|
|
|
{
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::LEFT:
|
2014-05-09 17:54:25 +08:00
|
|
|
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::LEFT);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::RIGHT:
|
2014-05-09 17:54:25 +08:00
|
|
|
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::CENTER_HORIZONTAL:
|
2014-05-09 17:54:25 +08:00
|
|
|
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (getIndex(item) == 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-06-27 15:39:10 +08:00
|
|
|
defaultLp->setMargin(Margin::ZERO);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
else
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
defaultLp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
item->setLayoutParameter(defaultLp);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
else
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (getIndex(item) == 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-06-27 15:39:10 +08:00
|
|
|
llp->setMargin(Margin::ZERO);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
else
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
llp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
switch (_gravity)
|
|
|
|
{
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::LEFT:
|
2014-05-09 17:54:25 +08:00
|
|
|
llp->setGravity(LinearLayoutParameter::LinearGravity::LEFT);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::RIGHT:
|
2014-05-09 17:54:25 +08:00
|
|
|
llp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::CENTER_HORIZONTAL:
|
2014-05-09 17:54:25 +08:00
|
|
|
llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-05-16 10:33:28 +08:00
|
|
|
LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter());
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!llp)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
|
2014-12-16 16:44:04 +08:00
|
|
|
switch (_gravity)
|
|
|
|
{
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::TOP:
|
2014-05-09 17:54:25 +08:00
|
|
|
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::TOP);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::BOTTOM:
|
2014-05-09 17:54:25 +08:00
|
|
|
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::CENTER_VERTICAL:
|
2014-05-09 17:54:25 +08:00
|
|
|
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (getIndex(item) == 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-06-27 15:39:10 +08:00
|
|
|
defaultLp->setMargin(Margin::ZERO);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
else
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
defaultLp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
item->setLayoutParameter(defaultLp);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
else
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (getIndex(item) == 0)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-06-27 15:39:10 +08:00
|
|
|
llp->setMargin(Margin::ZERO);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
else
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
llp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
switch (_gravity)
|
|
|
|
{
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::TOP:
|
2014-05-09 17:54:25 +08:00
|
|
|
llp->setGravity(LinearLayoutParameter::LinearGravity::TOP);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::BOTTOM:
|
2014-05-09 17:54:25 +08:00
|
|
|
llp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
2014-05-12 09:51:23 +08:00
|
|
|
case Gravity::CENTER_VERTICAL:
|
2014-05-09 17:54:25 +08:00
|
|
|
llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
|
2013-11-06 16:04:06 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::pushBackDefaultItem()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!_model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* newItem = _model->clone();
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(newItem);
|
|
|
|
addChild(newItem);
|
2013-12-23 15:02:52 +08:00
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void ListView::insertDefaultItem(ssize_t index)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!_model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* newItem = _model->clone();
|
2014-05-22 17:49:19 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
_items.insert(index, newItem);
|
2014-05-22 17:49:19 +08:00
|
|
|
ScrollView::addChild(newItem);
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(newItem);
|
2014-05-22 17:49:19 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 16:17:16 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::pushBackCustomItem(Widget* item)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(item);
|
|
|
|
addChild(item);
|
2013-12-23 15:02:52 +08:00
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-05-22 17:49:19 +08:00
|
|
|
|
|
|
|
void ListView::addChild(cocos2d::Node *child, int zOrder, int tag)
|
|
|
|
{
|
|
|
|
ScrollView::addChild(child, zOrder, tag);
|
|
|
|
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
_items.pushBack(widget);
|
|
|
|
}
|
2014-06-25 11:27:48 +08:00
|
|
|
}
|
2014-06-25 16:17:16 +08:00
|
|
|
|
|
|
|
void ListView::addChild(cocos2d::Node *child)
|
|
|
|
{
|
|
|
|
ListView::addChild(child, child->getLocalZOrder(), child->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::addChild(cocos2d::Node *child, int zOrder)
|
|
|
|
{
|
|
|
|
ListView::addChild(child, zOrder, child->getName());
|
|
|
|
}
|
2014-06-25 11:27:48 +08:00
|
|
|
|
|
|
|
void ListView::addChild(Node* child, int zOrder, const std::string &name)
|
|
|
|
{
|
|
|
|
ScrollView::addChild(child, zOrder, name);
|
|
|
|
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
_items.pushBack(widget);
|
|
|
|
}
|
2014-05-22 17:49:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::removeChild(cocos2d::Node *child, bool cleaup)
|
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
2014-12-16 16:44:04 +08:00
|
|
|
if (widget)
|
|
|
|
{
|
2014-05-22 17:49:19 +08:00
|
|
|
_items.eraseObject(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrollView::removeChild(child, cleaup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::removeAllChildren()
|
|
|
|
{
|
|
|
|
this->removeAllChildrenWithCleanup(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListView::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
|
|
|
ScrollView::removeAllChildrenWithCleanup(cleanup);
|
|
|
|
_items.clear();
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void ListView::insertCustomItem(Widget* item, ssize_t index)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_items.insert(index, item);
|
2014-05-22 17:49:19 +08:00
|
|
|
ScrollView::addChild(item);
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(item);
|
2013-12-23 15:02:52 +08:00
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
void ListView::removeItem(ssize_t index)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* item = getItem(index);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!item)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-05-22 17:49:19 +08:00
|
|
|
removeChild(item, true);
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::removeLastItem()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
removeItem(_items.size() -1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-27 16:01:03 +08:00
|
|
|
|
|
|
|
void ListView::removeAllItems()
|
|
|
|
{
|
|
|
|
removeAllChildren();
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2014-05-27 10:40:20 +08:00
|
|
|
Widget* ListView::getItem(ssize_t index)const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
if (index < 0 || index >= _items.size())
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-11-14 11:37:46 +08:00
|
|
|
return nullptr;
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
return _items.at(index);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Vector<Widget*>& ListView::getItems()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
return _items;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t ListView::getIndex(Widget *item) const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
return _items.getIndex(item);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 09:51:23 +08:00
|
|
|
void ListView::setGravity(Gravity gravity)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (_gravity == gravity)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_gravity = gravity;
|
2013-12-23 15:02:52 +08:00
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::setItemsMargin(float margin)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
if (_itemsMargin == margin)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_itemsMargin = margin;
|
2013-12-23 15:02:52 +08:00
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2014-03-01 22:34:25 +08:00
|
|
|
|
2014-05-27 10:40:20 +08:00
|
|
|
float ListView::getItemsMargin()const
|
2014-03-01 22:34:25 +08:00
|
|
|
{
|
|
|
|
return _itemsMargin;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2014-05-12 11:08:10 +08:00
|
|
|
void ListView::setDirection(Direction dir)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
switch (dir)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::VERTICAL:
|
2014-05-12 18:00:54 +08:00
|
|
|
setLayoutType(Type::VERTICAL);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2014-05-12 18:00:54 +08:00
|
|
|
setLayoutType(Type::HORIZONTAL);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
2014-05-12 11:08:10 +08:00
|
|
|
case Direction::BOTH:
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
default:
|
2013-11-06 16:04:06 +08:00
|
|
|
return;
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
ScrollView::setDirection(dir);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-12-26 14:57:30 +08:00
|
|
|
|
|
|
|
void ListView::requestRefreshView()
|
|
|
|
{
|
|
|
|
_refreshViewDirty = true;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::refreshView()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t length = _items.size();
|
2013-11-06 16:04:06 +08:00
|
|
|
for (int i=0; i<length; i++)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* item = _items.at(i);
|
2014-01-19 03:35:27 +08:00
|
|
|
item->setLocalZOrder(i);
|
2013-11-06 16:04:06 +08:00
|
|
|
remedyLayoutParameter(item);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-06 16:04:06 +08:00
|
|
|
updateInnerContainerSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
2013-11-08 20:29:49 +08:00
|
|
|
|
2014-12-22 09:55:29 +08:00
|
|
|
void ListView::forceDoLayout()
|
|
|
|
{
|
|
|
|
if (_refreshViewDirty)
|
|
|
|
{
|
|
|
|
refreshView();
|
|
|
|
_refreshViewDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->_innerContainer->forceDoLayout();
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:51:32 +08:00
|
|
|
void ListView::doLayout()
|
2013-12-23 15:02:52 +08:00
|
|
|
{
|
2014-06-04 10:51:32 +08:00
|
|
|
Layout::doLayout();
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
if (_refreshViewDirty)
|
|
|
|
{
|
|
|
|
refreshView();
|
|
|
|
_refreshViewDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ListView::addEventListenerListView(Ref *target, SEL_ListViewEvent selector)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
|
|
|
_listViewEventListener = target;
|
|
|
|
_listViewEventSelector = selector;
|
|
|
|
}
|
2014-06-23 12:09:49 +08:00
|
|
|
|
|
|
|
|
2014-05-14 16:14:28 +08:00
|
|
|
void ListView::addEventListener(const ccListViewCallback& callback)
|
2014-05-12 09:51:23 +08:00
|
|
|
{
|
|
|
|
_eventCallback = callback;
|
|
|
|
}
|
|
|
|
|
2014-05-22 15:23:13 +08:00
|
|
|
void ListView::selectedItemEvent(TouchEventType event)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-05-22 15:23:13 +08:00
|
|
|
switch (event)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2014-05-22 15:23:13 +08:00
|
|
|
case TouchEventType::BEGAN:
|
2014-05-12 09:51:23 +08:00
|
|
|
{
|
2014-03-11 11:53:44 +08:00
|
|
|
if (_listViewEventListener && _listViewEventSelector)
|
|
|
|
{
|
|
|
|
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_START);
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
if (_eventCallback) {
|
|
|
|
_eventCallback(this,EventType::ON_SELECTED_ITEM_START);
|
|
|
|
}
|
2014-12-05 19:31:10 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::ON_SELECTED_ITEM_START));
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-03-11 11:53:44 +08:00
|
|
|
default:
|
2014-05-12 09:51:23 +08:00
|
|
|
{
|
2014-03-11 11:53:44 +08:00
|
|
|
if (_listViewEventListener && _listViewEventSelector)
|
|
|
|
{
|
|
|
|
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_END);
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
if (_eventCallback) {
|
|
|
|
_eventCallback(this, EventType::ON_SELECTED_ITEM_END);
|
|
|
|
}
|
2014-12-05 19:31:10 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::ON_SELECTED_ITEM_END));
|
|
|
|
}
|
2014-05-12 09:51:23 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 16:48:49 +08:00
|
|
|
void ListView::interceptTouchEvent(TouchEventType event, Widget *sender, Touch* touch)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2014-06-06 16:48:49 +08:00
|
|
|
ScrollView::interceptTouchEvent(event, sender, touch);
|
2014-05-22 15:23:13 +08:00
|
|
|
if (event != TouchEventType::MOVED)
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* parent = sender;
|
2013-11-08 20:29:49 +08:00
|
|
|
while (parent)
|
|
|
|
{
|
|
|
|
if (parent && parent->getParent() == _innerContainer)
|
|
|
|
{
|
|
|
|
_curSelectedIndex = getIndex(parent);
|
|
|
|
break;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
parent = dynamic_cast<Widget*>(parent->getParent());
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
2014-06-25 10:50:18 +08:00
|
|
|
if (sender->isHighlighted()) {
|
|
|
|
selectedItemEvent(event);
|
|
|
|
}
|
2013-11-08 20:29:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t ListView::getCurSelectedIndex() const
|
2013-11-08 20:29:49 +08:00
|
|
|
{
|
|
|
|
return _curSelectedIndex;
|
|
|
|
}
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::onSizeChanged()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ScrollView::onSizeChanged();
|
|
|
|
_refreshViewDirty = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string ListView::getDescription() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-27 17:22:51 +08:00
|
|
|
return "ListView";
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* ListView::createCloneInstance()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return ListView::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::copyClonedWidgetChildren(Widget* model)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-27 17:22:51 +08:00
|
|
|
auto& arrayItems = static_cast<ListView*>(model)->getItems();
|
2013-12-26 16:17:52 +08:00
|
|
|
for (auto& item : arrayItems)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-06 16:04:06 +08:00
|
|
|
pushBackCustomItem(item->clone());
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void ListView::copySpecialProperties(Widget *widget)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ListView* listViewEx = dynamic_cast<ListView*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
if (listViewEx)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
ScrollView::copySpecialProperties(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
setItemModel(listViewEx->_model);
|
|
|
|
setItemsMargin(listViewEx->_itemsMargin);
|
|
|
|
setGravity(listViewEx->_gravity);
|
2014-05-23 15:03:46 +08:00
|
|
|
_listViewEventListener = listViewEx->_listViewEventListener;
|
|
|
|
_listViewEventSelector = listViewEx->_listViewEventSelector;
|
|
|
|
_eventCallback = listViewEx->_eventCallback;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-02-24 18:56:45 +08:00
|
|
|
NS_CC_END
|