1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 | --[[--
Refresh UIListView at the current postion.
NOTE: only needed in async mode
]]
function MyPackage.refreshUIListView(listView)
if not listView.bAsyncLoad then
listView:reload()
return
end
if #listView.items_ <= 0 then
listView:reload()
return
end
local originPos = MyPackage.getOriginPosOfUIListView(listView)
-- index of the previous beginning item
local beginIdx = listView.items_[1].idx_
listView:removeAllItems()
listView.container:setPosition(0, 0)
listView.container:setContentSize(cc.size(0, 0))
MyPackage.drawUIListViewFromIdx(listView, beginIdx, originPos.x, originPos.y)
end
--[[--
NOTE: only needed in async mode
]]
function MyPackage.getOriginPosOfUIListView(listView)
if not listView.bAsyncLoad then
return
end
local getContainerCascadeBoundingBox = function (listView)
local boundingBox
for i, item in ipairs(listView.items_) do
local w,h = item:getItemSize()
local x,y = item:getPosition()
local anchor = item:getAnchorPoint()
x = x - anchor.x * w
y = y - anchor.y * h
if boundingBox then
boundingBox = cc.rectUnion(boundingBox, cc.rect(x, y, w, h))
else
boundingBox = cc.rect(x, y, w, h)
end
end
local point = listView.container:convertToWorldSpace(cc.p(boundingBox.x, boundingBox.y))
boundingBox.x = point.x
boundingBox.y = point.y
return boundingBox
end
local cascadeBound = getContainerCascadeBoundingBox(listView)
-- local cascadeBound = listView.scrollNode:getCascadeBoundingBox()
local localPos = listView:convertToNodeSpace(cc.p(cascadeBound.x, cascadeBound.y))
local originPosX = 0
local originPosY = 0
if cc.ui.UIScrollView.DIRECTION_VERTICAL == listView.direction then
-- ahead part of view
originPosY = localPos.y + cascadeBound.height - listView.viewRect_.y - listView.viewRect_.height
else
-- left part of view
originPosX = - listView.viewRect_.x + localPos.x
end
return cc.p(originPosX, originPosY)
end
--[[--
Draw UIListView from the `beginIdx`th item at position (`originPosX`, `originPosY`).
NOTE: only needed in async mode
]]
function MyPackage.drawUIListViewFromIdx(listView, beginIdx, originPosX, originPosY)
if not listView.bAsyncLoad then
listView:reload()
return
end
listView:removeAllItems()
listView.container:setPosition(0, 0)
listView.container:setContentSize(cc.size(0, 0))
local beginIdx = beginIdx or 1
local originPosX = originPosX or 0
local originPosY = originPosY or 0
local count = listView.delegate_[cc.ui.UIListView.DELEGATE](listView, cc.ui.UIListView.COUNT_TAG)
listView.items_ = {}
local itemW, itemH = 0, 0
local item
local containerW, containerH = 0, 0
for i = beginIdx, count do
item, itemW, itemH = listView:loadOneItem_(cc.p(originPosX, originPosY), i)
if cc.ui.UIScrollView.DIRECTION_VERTICAL == listView.direction then
originPosY = originPosY - itemH
containerH = containerH + itemH
else
originPosX = originPosX + itemW
containerW = containerW + itemW
end
if containerW > listView.viewRect_.width + listView.redundancyViewVal
or containerH > listView.viewRect_.height + listView.redundancyViewVal then
break
end
end
if cc.ui.UIScrollView.DIRECTION_VERTICAL == listView.direction then
listView.container:setPosition(listView.viewRect_.x,
listView.viewRect_.y + listView.viewRect_.height)
else
listView.container:setPosition(listView.viewRect_.x, listView.viewRect_.y)
end
listView:increaseOrReduceItem_()
end
function MyPackage.elasticMoveUIScrollView(scrollView, scrollToBottom, scrollToRight)
local cascadeBound = scrollView:getScrollNodeRect()
local disX, disY = 0, 0
local viewRect = scrollView:getViewRectInWorldSpace()
if cascadeBound.width < viewRect.width then
if scrollToRight then
disX = viewRect.x + viewRect.width - cascadeBound.x - cascadeBound.width
else
disX = viewRect.x - cascadeBound.x
end
else
if cascadeBound.x > viewRect.x then
disX = viewRect.x - cascadeBound.x
elseif cascadeBound.x + cascadeBound.width < viewRect.x + viewRect.width then
disX = viewRect.x + viewRect.width - cascadeBound.x - cascadeBound.width
end
end
if cascadeBound.height < viewRect.height then
if scrollToBottom then
disY = viewRect.y - cascadeBound.y
else
disY = viewRect.y + viewRect.height - cascadeBound.y - cascadeBound.height
end
else
if cascadeBound.y > viewRect.y then
disY = viewRect.y - cascadeBound.y
elseif cascadeBound.y + cascadeBound.height < viewRect.y + viewRect.height then
disY = viewRect.y + viewRect.height - cascadeBound.y - cascadeBound.height
end
end
if 0 == disX and 0 == disY then
return
end
local posX, posY = scrollView.scrollNode:getPosition()
scrollView.position_ = cc.p(posX + disX, posY + disY)
scrollView.scrollNode:setPosition(scrollView.position_)
end
|