Refactor list view's layout refreshing logic.

This commit is contained in:
Neo Kim 2015-09-14 12:20:17 +09:00
parent f58cbad6c2
commit 11d80f47f8
2 changed files with 36 additions and 54 deletions

View File

@ -40,7 +40,6 @@ _magneticType(MagneticType::NONE),
_magneticAllowedOutOfBoundary(true),
_itemsMargin(0.0f),
_curSelectedIndex(-1),
_refreshViewDirty(true),
_listViewEventListener(nullptr),
_listViewEventSelector(nullptr),
_eventCallback(nullptr)
@ -235,7 +234,7 @@ void ListView::pushBackDefaultItem()
Widget* newItem = _model->clone();
remedyLayoutParameter(newItem);
addChild(newItem);
_refreshViewDirty = true;
requestDoLayout();
}
void ListView::insertDefaultItem(ssize_t index)
@ -252,7 +251,7 @@ void ListView::pushBackCustomItem(Widget* item)
{
remedyLayoutParameter(item);
addChild(item);
_refreshViewDirty = true;
requestDoLayout();
}
void ListView::addChild(cocos2d::Node *child, int zOrder, int tag)
@ -341,7 +340,7 @@ void ListView::insertCustomItem(Widget* item, ssize_t index)
ScrollView::addChild(item);
remedyLayoutParameter(item);
_refreshViewDirty = true;
requestDoLayout();
}
void ListView::removeItem(ssize_t index)
@ -352,7 +351,7 @@ void ListView::removeItem(ssize_t index)
return;
}
removeChild(item, true);
_refreshViewDirty = true;
requestDoLayout();
}
void ListView::removeLastItem()
@ -395,7 +394,7 @@ void ListView::setGravity(Gravity gravity)
return;
}
_gravity = gravity;
_refreshViewDirty = true;
requestDoLayout();
}
void ListView::setMagneticType(MagneticType magneticType)
@ -427,7 +426,7 @@ void ListView::setItemsMargin(float margin)
return;
}
_itemsMargin = margin;
_refreshViewDirty = true;
requestDoLayout();
}
float ListView::getItemsMargin()const
@ -455,15 +454,20 @@ void ListView::setDirection(Direction dir)
ScrollView::setDirection(dir);
}
void ListView::requestRefreshView()
{
_refreshViewDirty = true;
}
void ListView::refreshView()
{
forceDoLayout();
}
void ListView::doLayout()
{
if(!_doLayoutDirty)
{
return;
}
ssize_t length = _items.size();
for (int i=0; i<length; i++)
for (int i = 0; i < length; ++i)
{
Widget* item = _items.at(i);
item->setLocalZOrder(i);
@ -471,27 +475,7 @@ void ListView::refreshView()
}
_innerContainer->forceDoLayout();
updateInnerContainerSize();
_refreshViewDirty = false;
}
void ListView::refreshViewIfNecessary()
{
if (_refreshViewDirty)
{
refreshView();
}
}
void ListView::forceDoLayout()
{
refreshViewIfNecessary();
this->_innerContainer->forceDoLayout();
}
void ListView::doLayout()
{
Layout::doLayout();
refreshViewIfNecessary();
_doLayoutDirty = false;
}
void ListView::addEventListenerListView(Ref *target, SEL_ListViewEvent selector)
@ -684,67 +668,67 @@ Widget* ListView::getBottommostItemInCurrentView() const
void ListView::jumpToBottom()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToBottom();
}
void ListView::jumpToTop()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToTop();
}
void ListView::jumpToLeft()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToLeft();
}
void ListView::jumpToRight()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToRight();
}
void ListView::jumpToTopLeft()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToTopLeft();
}
void ListView::jumpToTopRight()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToTopRight();
}
void ListView::jumpToBottomLeft()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToBottomLeft();
}
void ListView::jumpToBottomRight()
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToBottomRight();
}
void ListView::jumpToPercentVertical(float percent)
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToPercentVertical(percent);
}
void ListView::jumpToPercentHorizontal(float percent)
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToPercentHorizontal(percent);
}
void ListView::jumpToPercentBothDirection(const Vec2& percent)
{
refreshViewIfNecessary();
doLayout();
ScrollView::jumpToPercentBothDirection(percent);
}
@ -765,7 +749,7 @@ void ListView::jumpToItem(int itemIndex, const Vec2& positionRatioInView, const
{
return;
}
refreshViewIfNecessary();
doLayout();
Vec2 destination = calculateItemDestination(getContentSize(), item, positionRatioInView, itemAnchorPoint);
destination = flattenVectorByDirection(destination);
@ -799,7 +783,7 @@ ssize_t ListView::getCurSelectedIndex() const
void ListView::onSizeChanged()
{
ScrollView::onSizeChanged();
_refreshViewDirty = true;
requestDoLayout();
}
std::string ListView::getDescription() const

View File

@ -246,7 +246,6 @@ public:
float getItemsMargin()const;
//override methods
virtual void forceDoLayout()override;
virtual void doLayout() override;
virtual void addChild(Node* child)override;
virtual void addChild(Node * child, int localZOrder)override;
@ -371,16 +370,16 @@ public:
/**
* @brief Refresh view and layout of ListView manually.
* This method will mark ListView content as dirty and the content view will be refershed in the next frame.
* This method will mark ListView content as dirty and the content view will be refreshed in the next frame.
* @deprecated Use method requestDoLayout() instead
*/
void requestRefreshView();
CC_DEPRECATED_ATTRIBUTE void requestRefreshView();
/**
* @brief Refresh content view of ListView.
* @deprecated Use method forceDoLayout() instead
*/
void refreshView();
void refreshViewIfNecessary();
CC_DEPRECATED_ATTRIBUTE void refreshView();
CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
@ -419,7 +418,6 @@ protected:
float _itemsMargin;
ssize_t _curSelectedIndex;
bool _refreshViewDirty;
Ref* _listViewEventListener;
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))