issue #5057, refactor Listview

This commit is contained in:
andyque 2014-05-12 09:51:23 +08:00
parent e1fe26543d
commit 43eb75fb1a
8 changed files with 93 additions and 49 deletions

View File

@ -2020,7 +2020,7 @@ void WidgetPropertiesReader0300::setPropsForListViewFromJsonDictionary(Widget* w
int direction = DICTOOL->getFloatValue_json(options, "direction"); int direction = DICTOOL->getFloatValue_json(options, "direction");
listView->setDirection((SCROLLVIEW_DIR)direction); listView->setDirection((SCROLLVIEW_DIR)direction);
ListViewGravity gravity = (ListViewGravity)DICTOOL->getIntValue_json(options, "gravity"); ListView::Gravity gravity = (ListView::Gravity)DICTOOL->getIntValue_json(options, "gravity");
listView->setGravity(gravity); listView->setGravity(gravity);
float itemMargin = DICTOOL->getFloatValue_json(options, "itemMargin"); float itemMargin = DICTOOL->getFloatValue_json(options, "itemMargin");

View File

@ -41,7 +41,7 @@ namespace cocostudio
int direction = DICTOOL->getFloatValue_json(options, "direction"); int direction = DICTOOL->getFloatValue_json(options, "direction");
listView->setDirection((SCROLLVIEW_DIR)direction); listView->setDirection((SCROLLVIEW_DIR)direction);
ListViewGravity gravity = (ListViewGravity)DICTOOL->getIntValue_json(options, "gravity"); ListView::Gravity gravity = (ListView::Gravity)DICTOOL->getIntValue_json(options, "gravity");
listView->setGravity(gravity); listView->setGravity(gravity);
float itemMargin = DICTOOL->getFloatValue_json(options, "itemMargin"); float itemMargin = DICTOOL->getFloatValue_json(options, "itemMargin");

View File

@ -115,6 +115,12 @@ CC_DEPRECATED_ATTRIBUTE const RelativeLayoutParameter::RelativeAlign RELATIVE_LO
CC_DEPRECATED_ATTRIBUTE const RelativeLayoutParameter::RelativeAlign RELATIVE_LOCATION_BELOW_RIGHTALIGN CC_DEPRECATED_ATTRIBUTE const RelativeLayoutParameter::RelativeAlign RELATIVE_LOCATION_BELOW_RIGHTALIGN
= RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN; = RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_LEFT = ListView::Gravity::LEFT;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_RIGHT = ListView::Gravity::RIGHT;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_CENTER_HORIZONTAL = ListView::Gravity::CENTER_HORIZONTAL;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_TOP = ListView::Gravity::TOP;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_BOTTOM = ListView::Gravity::BOTTOM;
CC_DEPRECATED_ATTRIBUTE const ListView::Gravity LISTVIEW_GRAVITY_CENTER_VERTICAL = ListView::Gravity::CENTER_VERTICAL;
CC_DEPRECATED_ATTRIBUTE typedef Widget::TextureResType TextureResType; CC_DEPRECATED_ATTRIBUTE typedef Widget::TextureResType TextureResType;
CC_DEPRECATED_ATTRIBUTE typedef Widget::PositionType PositionType; CC_DEPRECATED_ATTRIBUTE typedef Widget::PositionType PositionType;
@ -127,6 +133,7 @@ CC_DEPRECATED_ATTRIBUTE typedef Layout::BackGroundColorType LayoutBackGroundColo
CC_DEPRECATED_ATTRIBUTE typedef LayoutParameter::Type LayoutParameterType; CC_DEPRECATED_ATTRIBUTE typedef LayoutParameter::Type LayoutParameterType;
CC_DEPRECATED_ATTRIBUTE typedef LinearLayoutParameter::LinearGravity LinearGravity; CC_DEPRECATED_ATTRIBUTE typedef LinearLayoutParameter::LinearGravity LinearGravity;
CC_DEPRECATED_ATTRIBUTE typedef RelativeLayoutParameter::RelativeAlign RelativeAlign; CC_DEPRECATED_ATTRIBUTE typedef RelativeLayoutParameter::RelativeAlign RelativeAlign;
CC_DEPRECATED_ATTRIBUTE typedef ListView::Gravity ListViewGravity;
} }

View File

@ -34,12 +34,13 @@ IMPLEMENT_CLASS_GUI_INFO(ListView)
ListView::ListView(): ListView::ListView():
_model(nullptr), _model(nullptr),
_gravity(LISTVIEW_GRAVITY_CENTER_HORIZONTAL), _gravity(Gravity::CENTER_VERTICAL),
_itemsMargin(0.0f), _itemsMargin(0.0f),
_listViewEventListener(nullptr), _listViewEventListener(nullptr),
_listViewEventSelector(nullptr), _listViewEventSelector(nullptr),
_curSelectedIndex(0), _curSelectedIndex(0),
_refreshViewDirty(true) _refreshViewDirty(true),
_eventCallback(nullptr)
{ {
} }
@ -134,13 +135,13 @@ void ListView::remedyLayoutParameter(Widget *item)
{ {
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
switch (_gravity) { switch (_gravity) {
case LISTVIEW_GRAVITY_LEFT: case Gravity::LEFT:
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::LEFT); defaultLp->setGravity(LinearLayoutParameter::LinearGravity::LEFT);
break; break;
case LISTVIEW_GRAVITY_RIGHT: case Gravity::RIGHT:
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT); defaultLp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT);
break; break;
case LISTVIEW_GRAVITY_CENTER_HORIZONTAL: case Gravity::CENTER_HORIZONTAL:
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
break; break;
default: default:
@ -167,13 +168,13 @@ void ListView::remedyLayoutParameter(Widget *item)
llp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f)); llp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f));
} }
switch (_gravity) { switch (_gravity) {
case LISTVIEW_GRAVITY_LEFT: case Gravity::LEFT:
llp->setGravity(LinearLayoutParameter::LinearGravity::LEFT); llp->setGravity(LinearLayoutParameter::LinearGravity::LEFT);
break; break;
case LISTVIEW_GRAVITY_RIGHT: case Gravity::RIGHT:
llp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT); llp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT);
break; break;
case LISTVIEW_GRAVITY_CENTER_HORIZONTAL: case Gravity::CENTER_HORIZONTAL:
llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL);
break; break;
default: default:
@ -189,13 +190,13 @@ void ListView::remedyLayoutParameter(Widget *item)
{ {
LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); LinearLayoutParameter* defaultLp = LinearLayoutParameter::create();
switch (_gravity) { switch (_gravity) {
case LISTVIEW_GRAVITY_TOP: case Gravity::TOP:
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::TOP); defaultLp->setGravity(LinearLayoutParameter::LinearGravity::TOP);
break; break;
case LISTVIEW_GRAVITY_BOTTOM: case Gravity::BOTTOM:
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM); defaultLp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM);
break; break;
case LISTVIEW_GRAVITY_CENTER_VERTICAL: case Gravity::CENTER_VERTICAL:
defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
break; break;
default: default:
@ -222,13 +223,13 @@ void ListView::remedyLayoutParameter(Widget *item)
llp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f)); llp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f));
} }
switch (_gravity) { switch (_gravity) {
case LISTVIEW_GRAVITY_TOP: case Gravity::TOP:
llp->setGravity(LinearLayoutParameter::LinearGravity::TOP); llp->setGravity(LinearLayoutParameter::LinearGravity::TOP);
break; break;
case LISTVIEW_GRAVITY_BOTTOM: case Gravity::BOTTOM:
llp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM); llp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM);
break; break;
case LISTVIEW_GRAVITY_CENTER_VERTICAL: case Gravity::CENTER_VERTICAL:
llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL);
break; break;
default: default:
@ -331,7 +332,7 @@ ssize_t ListView::getIndex(Widget *item) const
return _items.getIndex(item); return _items.getIndex(item);
} }
void ListView::setGravity(ListViewGravity gravity) void ListView::setGravity(Gravity gravity)
{ {
if (_gravity == gravity) if (_gravity == gravity)
{ {
@ -408,22 +409,37 @@ void ListView::addEventListenerListView(Ref *target, SEL_ListViewEvent selector)
_listViewEventSelector = selector; _listViewEventSelector = selector;
} }
void ListView::addEventListener(ccListViewCallback callback)
{
_eventCallback = callback;
}
void ListView::selectedItemEvent(int state) void ListView::selectedItemEvent(int state)
{ {
switch (state) switch (state)
{ {
case 0: case 0:
{
if (_listViewEventListener && _listViewEventSelector) if (_listViewEventListener && _listViewEventSelector)
{ {
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_START); (_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_START);
} }
break; if (_eventCallback) {
_eventCallback(this,EventType::ON_SELECTED_ITEM_START);
}
}
break;
default: default:
{
if (_listViewEventListener && _listViewEventSelector) if (_listViewEventListener && _listViewEventSelector)
{ {
(_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_END); (_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_END);
} }
break; if (_eventCallback) {
_eventCallback(this, EventType::ON_SELECTED_ITEM_END);
}
}
break;
} }
} }

View File

@ -31,25 +31,14 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
namespace ui{ namespace ui{
typedef enum
{
LISTVIEW_GRAVITY_LEFT,
LISTVIEW_GRAVITY_RIGHT,
LISTVIEW_GRAVITY_CENTER_HORIZONTAL,
LISTVIEW_GRAVITY_TOP, CC_DEPRECATED_ATTRIBUTE typedef enum
LISTVIEW_GRAVITY_BOTTOM,
LISTVIEW_GRAVITY_CENTER_VERTICAL,
}ListViewGravity;
typedef enum
{ {
LISTVIEW_ONSELECTEDITEM_START, LISTVIEW_ONSELECTEDITEM_START,
LISTVIEW_ONSELECTEDITEM_END LISTVIEW_ONSELECTEDITEM_END
}ListViewEventType; }ListViewEventType;
typedef void (Ref::*SEL_ListViewEvent)(Ref*,ListViewEventType); CC_DEPRECATED_ATTRIBUTE typedef void (Ref::*SEL_ListViewEvent)(Ref*,ListViewEventType);
#define listvieweventselector(_SELECTOR) (SEL_ListViewEvent)(&_SELECTOR) #define listvieweventselector(_SELECTOR) (SEL_ListViewEvent)(&_SELECTOR)
class ListView : public ScrollView class ListView : public ScrollView
@ -58,6 +47,23 @@ class ListView : public ScrollView
DECLARE_CLASS_GUI_INFO DECLARE_CLASS_GUI_INFO
public: public:
enum class Gravity
{
LEFT,
RIGHT,
CENTER_HORIZONTAL,
TOP,
BOTTOM,
CENTER_VERTICAL
};
enum class EventType
{
ON_SELECTED_ITEM_START,
ON_SELECTED_ITEM_END
};
typedef std::function<void(Ref*, EventType)> ccListViewCallback;
/** /**
* Default constructor * Default constructor
@ -144,7 +150,7 @@ public:
* Changes the gravity of listview. * Changes the gravity of listview.
* @see ListViewGravity * @see ListViewGravity
*/ */
void setGravity(ListViewGravity gravity); void setGravity(Gravity gravity);
/** /**
* Changes the margin between each item. * Changes the margin between each item.
@ -159,7 +165,8 @@ public:
ssize_t getCurSelectedIndex() const; ssize_t getCurSelectedIndex() const;
void addEventListenerListView(Ref* target, SEL_ListViewEvent selector); CC_DEPRECATED_ATTRIBUTE void addEventListenerListView(Ref* target, SEL_ListViewEvent selector);
void addEventListener(ccListViewCallback callback);
/** /**
* Changes scroll direction of scrollview. * Changes scroll direction of scrollview.
@ -203,10 +210,24 @@ protected:
Widget* _model; Widget* _model;
Vector<Widget*> _items; Vector<Widget*> _items;
ListViewGravity _gravity; Gravity _gravity;
float _itemsMargin; float _itemsMargin;
Ref* _listViewEventListener; Ref* _listViewEventListener;
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif _MSC_VER >= 1400 //vs 2005 or higher
#pragma warning (push)
#pragma warning (disable: 4996)
#endif
SEL_ListViewEvent _listViewEventSelector; SEL_ListViewEvent _listViewEventSelector;
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
#elif _MSC_VER >= 1400 //vs 2005 or higher
#pragma warning (pop)
#endif
ccListViewCallback _eventCallback;
ssize_t _curSelectedIndex; ssize_t _curSelectedIndex;
bool _refreshViewDirty; bool _refreshViewDirty;
}; };

View File

@ -66,7 +66,7 @@ bool UIListViewTest_Vertical::init()
(backgroundSize.width - listView->getSize().width) / 2.0f, (backgroundSize.width - listView->getSize().width) / 2.0f,
(widgetSize.height - backgroundSize.height) / 2.0f + (widgetSize.height - backgroundSize.height) / 2.0f +
(backgroundSize.height - listView->getSize().height) / 2.0f)); (backgroundSize.height - listView->getSize().height) / 2.0f));
listView->addEventListenerListView(this, listvieweventselector(UIListViewTest_Vertical::selectedItemEvent)); listView->addEventListener(CC_CALLBACK_2(UIListViewTest_Vertical::selectedItemEvent, this));
_uiLayer->addChild(listView); _uiLayer->addChild(listView);
@ -147,7 +147,7 @@ bool UIListViewTest_Vertical::init()
listView->removeItem(items_count - 1); listView->removeItem(items_count - 1);
// set all items layout gravity // set all items layout gravity
listView->setGravity(LISTVIEW_GRAVITY_CENTER_VERTICAL); listView->setGravity(ListView::Gravity::CENTER_VERTICAL);
// set items margin // set items margin
listView->setItemsMargin(2.0f); listView->setItemsMargin(2.0f);
@ -158,18 +158,18 @@ bool UIListViewTest_Vertical::init()
return false; return false;
} }
void UIListViewTest_Vertical::selectedItemEvent(Ref *pSender, ListViewEventType type) void UIListViewTest_Vertical::selectedItemEvent(Ref *pSender, ListView::EventType type)
{ {
switch (type) switch (type)
{ {
case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_START: case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_START:
{ {
ListView* listView = static_cast<ListView*>(pSender); ListView* listView = static_cast<ListView*>(pSender);
CC_UNUSED_PARAM(listView); CC_UNUSED_PARAM(listView);
CCLOG("select child start index = %ld", listView->getCurSelectedIndex()); CCLOG("select child start index = %ld", listView->getCurSelectedIndex());
break; break;
} }
case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_END: case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_END:
{ {
ListView* listView = static_cast<ListView*>(pSender); ListView* listView = static_cast<ListView*>(pSender);
CC_UNUSED_PARAM(listView); CC_UNUSED_PARAM(listView);
@ -243,7 +243,7 @@ bool UIListViewTest_Horizontal::init()
(backgroundSize.width - listView->getSize().width) / 2.0f, (backgroundSize.width - listView->getSize().width) / 2.0f,
(widgetSize.height - backgroundSize.height) / 2.0f + (widgetSize.height - backgroundSize.height) / 2.0f +
(backgroundSize.height - listView->getSize().height) / 2.0f)); (backgroundSize.height - listView->getSize().height) / 2.0f));
listView->addEventListenerListView(this, listvieweventselector(UIListViewTest_Horizontal::selectedItemEvent)); listView->addEventListener(CC_CALLBACK_2(UIListViewTest_Horizontal::selectedItemEvent, this));
_uiLayer->addChild(listView); _uiLayer->addChild(listView);
@ -323,7 +323,7 @@ bool UIListViewTest_Horizontal::init()
listView->removeItem(items_count - 1); listView->removeItem(items_count - 1);
// set all items layout gravity // set all items layout gravity
listView->setGravity(LISTVIEW_GRAVITY_CENTER_VERTICAL); listView->setGravity(ListView::Gravity::CENTER_VERTICAL);
// set items margin // set items margin
listView->setItemsMargin(2); listView->setItemsMargin(2);
@ -334,18 +334,18 @@ bool UIListViewTest_Horizontal::init()
return false; return false;
} }
void UIListViewTest_Horizontal::selectedItemEvent(Ref *pSender, ListViewEventType type) void UIListViewTest_Horizontal::selectedItemEvent(Ref *pSender, ListView::EventType type)
{ {
switch (type) switch (type)
{ {
case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_START: case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_START:
{ {
ListView* listView = static_cast<ListView*>(pSender); ListView* listView = static_cast<ListView*>(pSender);
CC_UNUSED_PARAM(listView); CC_UNUSED_PARAM(listView);
CCLOG("select child start index = %ld", listView->getCurSelectedIndex()); CCLOG("select child start index = %ld", listView->getCurSelectedIndex());
break; break;
} }
case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_END: case cocos2d::ui::ListView::EventType::ON_SELECTED_ITEM_END:
{ {
ListView* listView = static_cast<ListView*>(pSender); ListView* listView = static_cast<ListView*>(pSender);
CC_UNUSED_PARAM(listView); CC_UNUSED_PARAM(listView);

View File

@ -33,7 +33,7 @@ public:
UIListViewTest_Vertical(); UIListViewTest_Vertical();
~UIListViewTest_Vertical(); ~UIListViewTest_Vertical();
bool init(); bool init();
void selectedItemEvent(Ref* pSender, ListViewEventType type); void selectedItemEvent(Ref* pSender, ListView::EventType type);
protected: protected:
UI_SCENE_CREATE_FUNC(UIListViewTest_Vertical) UI_SCENE_CREATE_FUNC(UIListViewTest_Vertical)
@ -48,7 +48,7 @@ public:
UIListViewTest_Horizontal(); UIListViewTest_Horizontal();
~UIListViewTest_Horizontal(); ~UIListViewTest_Horizontal();
bool init(); bool init();
void selectedItemEvent(Ref* pSender, ListViewEventType type); void selectedItemEvent(Ref* pSender, ListView::EventType type);
protected: protected:
UI_SCENE_CREATE_FUNC(UIListViewTest_Horizontal) UI_SCENE_CREATE_FUNC(UIListViewTest_Horizontal)

View File

@ -33,7 +33,7 @@ public:
UIListViewTest_Vertical_Editor(); UIListViewTest_Vertical_Editor();
~UIListViewTest_Vertical_Editor(); ~UIListViewTest_Vertical_Editor();
bool init(); bool init();
void selectedItemEvent(Ref* pSender, ListViewEventType type); void selectedItemEvent(Ref* pSender, ListView::EventType type);
protected: protected:
UI_SCENE_EDITOR_CREATE_FUNC(UIListViewTest_Vertical_Editor) UI_SCENE_EDITOR_CREATE_FUNC(UIListViewTest_Vertical_Editor)
@ -49,7 +49,7 @@ public:
UIListViewTest_Horizontal_Editor(); UIListViewTest_Horizontal_Editor();
~UIListViewTest_Horizontal_Editor(); ~UIListViewTest_Horizontal_Editor();
bool init(); bool init();
void selectedItemEvent(Ref* pSender, ListViewEventType type); void selectedItemEvent(Ref* pSender, ListView::EventType type);
protected: protected:
UI_SCENE_EDITOR_CREATE_FUNC(UIListViewTest_Horizontal_Editor) UI_SCENE_EDITOR_CREATE_FUNC(UIListViewTest_Horizontal_Editor)