0%

U3D 基于NGUI写的 自适应大小的ListView

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543


using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;

public enum Layout
{
Vertical,
Horizontal
}

/// <summary>
/// 无限Item列表
/// </summary>
public class UIDiffSizeListView : MonoBehaviour
{

public Layout layout;
public Vector2 spacing;
public GameObject itemPrefab;
private UIItemRender itemRender = null;
private UIPanel mPanel;
public UIScrollView mScrollView;
private bool initialized = false;
private GameObject content;
private Vector2 scrollRectSize;
private GameObject pressObj;
private bool isDraging = false;
private bool isInvalid;
public List<object> dataList = new List<object>();
private Dictionary<int, UIItemRender> dirCurItems = new Dictionary<int, UIItemRender>();
private Dictionary<int, UIItemRender> tmpDic2 = new Dictionary<int, UIItemRender>();
private Dictionary<int, Vector2> itemSizes = new Dictionary<int, Vector2>();
private Queue<UIItemRender> unUseItems = new Queue<UIItemRender>();

void Awake()
{
this.Init();
}

public void AddData(object obj)
{
this.dataList.Add(obj);
this.Invalidate();
}

public void UpdateDataAt(object obj, int inx)
{
if (inx >= 0 && inx <= this.dataList.Count)
{
this.dataList[inx] = obj;
this.itemSizes.Remove(inx);
this.Invalidate();
}
}

public void InsertData(object obj, int inx)
{
this.dataList.Insert(inx, obj);

Dictionary<int, Vector2> tmpDic = new Dictionary<int, Vector2>();
for(int i = 0; i < this.dataList.Count; ++i)
{
if (i < inx)
{
if(this.itemSizes.ContainsKey(i))
{
tmpDic[i] = this.itemSizes[i];
}
}
else if (i > inx)
{
if(this.itemSizes.ContainsKey(i - 1))
{
tmpDic[i] = this.itemSizes[i - 1];
}
}
}
this.itemSizes = null;
this.itemSizes = tmpDic;

this.Invalidate();
}

public void RemoveDataAt(int inx)
{
if (inx >= 0 && inx <= this.dataList.Count)
{
this.dataList.RemoveAt(inx);

Dictionary<int, Vector2> tmpDic = new Dictionary<int, Vector2>();
for(int i = 0; i < this.dataList.Count; ++i)
{
if (i < inx)
{
if(this.itemSizes.ContainsKey(i))
{
tmpDic[i] = this.itemSizes[i];
}
}
else if (i > inx)
{
if(this.itemSizes.ContainsKey(i + 1))
{
tmpDic[i] = this.itemSizes[i + 1];
}
}
}
this.itemSizes = null;
this.itemSizes = tmpDic;
this.Invalidate();
}
}

public void JumpTo(int inx)
{
if (inx >= 0 && inx <= this.dataList.Count)
{
float sizeSum = 0;
UIWidget widget = this.content.GetComponent<UIWidget>();
if (this.layout == Layout.Vertical)
{
for(int i = 0; i < inx; ++i)
{
Vector2 size = this.GetSizeByInx(i);
sizeSum = sizeSum + size.y;
}
this.mScrollView.MoveRelative(new Vector3(0,sizeSum,0));
}
else if (this.layout == Layout.Horizontal)
{
for(int i = 0; i < inx; ++i)
{
Vector2 size = this.GetSizeByInx(i);
sizeSum = sizeSum + size.x;
}
this.mScrollView.MoveRelative(new Vector3(sizeSum,0,0));
}
}
}

public void JumpToBottom()
{
UIWidget widget = this.content.GetComponent<UIWidget>();
if (this.layout == Layout.Vertical)
{
this.mScrollView.ResetPosition();
float showPos = widget.height - scrollRectSize.y;
if (showPos > 0)
this.mScrollView.MoveRelative(new Vector3(0,showPos,0));

}
else if (this.layout == Layout.Horizontal)
{
this.mScrollView.ResetPosition();
float showPos = widget.width - scrollRectSize.x;
if (showPos > 0)
this.mScrollView.MoveRelative(new Vector3(showPos,0,0));
}
}
public void JumpToTop()
{
UIWidget widget = this.content.GetComponent<UIWidget>();
if (this.layout == Layout.Vertical)
{
this.mScrollView.SetDragAmount(0,0,false);
}
else if (this.layout == Layout.Horizontal)
{
this.mScrollView.SetDragAmount(0,0,false);
}
}

public void Init()
{

if (initialized)
return;
initialized = true;

mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
this.mScrollView = mPanel.GetComponent<UIScrollView>();
this.mPanel.onClipMove += OnUIPanelClipMove;

// record some sizes
scrollRectSize = mPanel.GetViewSize();

// add a scrollrect content
GameObject go = new GameObject ();
go.name = "content";
go.transform.SetParent(transform);

UIWidget uiwidget = go.AddComponent<UIWidget>();
uiwidget.pivot = UIWidget.Pivot.TopLeft;
uiwidget.SetDimensions((int)scrollRectSize.x,(int)scrollRectSize.y);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
this.content = go;

//itemPrefab
GameObject item = GameObject.Instantiate(itemPrefab) as GameObject;
item.transform.SetParent(mPanel.transform.parent);
item.transform.localPosition = new Vector3(1500, 1500, 1);
itemRender = item.GetComponent<UIItemRender>();
if (itemRender == null)
itemRender = item.AddComponent<UIItemRender>();

this.mScrollView.onDragStarted += delegate ()
{
this.isDraging = true;
};
this.mScrollView.onDragFinished += delegate ()
{
this.isDraging = false;
this.pressObj = null;
};
}

// 全部清除重新计算每个的大小
public void ClearAll()
{
this.dataList.Clear();
this.itemSizes.Clear();
this.Invalidate();
}

public void ClearAllCurItems()
{
var enumerator = this.dirCurItems.GetEnumerator();
while (enumerator.MoveNext())
{
Destroy(enumerator.Current.Value.gameObject);
}
this.dirCurItems.Clear();
}

void LateUpdate()
{
UpdateNow();
}

public void UpdateNow()
{
if (this.isInvalid)
{
if (!this.gameObject.activeInHierarchy)
return;

this.ClearAllCurItems();
this.UpdateView();
this.UpdateScrollView();
this.isInvalid = false;
}
}

private void Invalidate()
{
this.isInvalid = true;
}

private void OnUIPanelClipMove(UIPanel panel)
{
this.UpdateView();
}

private void UpdateScrollView()
{
this.mScrollView.InvalidateBounds();
this.mScrollView.RestrictWithinBounds(true);
this.mScrollView.UpdateScrollbars(false);
}

private Vector2 GetSizeByInx(int inx)
{
if(!this.itemSizes.ContainsKey(inx))
{

itemRender.SetData(this.dataList[inx]);
UIWidget widget = itemRender.GetComponent<UIWidget>();

Vector2 size = Vector2.zero;
size.x = widget.width;
size.y = widget.height;

this.itemSizes.Add(inx, size);
}

return this.itemSizes[inx];
}

private Vector3 GetLocalPosByInx(int inx)
{
Vector3 pos = Vector3.zero;
for(int i = 0; i < inx; ++i)
{
Vector2 size = this.GetSizeByInx(i);
if (this.layout == Layout.Vertical)
{
pos.y = pos.y - size.y;
}
else if (this.layout == Layout.Horizontal)
{
pos.x = pos.x + size.x;
}
}

return pos;
}

private Vector2Int GetStartInxAndEndInx(float min,float max)
{
int startInx = -1;
int endInx = -1;
float AreaCellSizeCount = 0;
for(int i = 0; i < this.dataList.Count; ++i)
{
Vector2 size = this.GetSizeByInx(i);
if (this.layout == Layout.Vertical)
{
if (max > 0)
{
startInx = 0;
}
UIWidget widget = this.content.GetComponent<UIWidget>();
if (Math.Abs(min) > widget.height)
{
endInx = this.dataList.Count - 1;
}


AreaCellSizeCount = AreaCellSizeCount + size.y;
if (AreaCellSizeCount > Math.Abs(max) && startInx == -1)
{
startInx = i;
}

if (AreaCellSizeCount > Math.Abs(min) && endInx == -1)
{
endInx = i;
}
}
else if (this.layout == Layout.Horizontal)
{

AreaCellSizeCount = AreaCellSizeCount + size.x;

if (AreaCellSizeCount > Math.Abs(max) && startInx == -1)
{
startInx = i;
}

if (AreaCellSizeCount > Math.Abs(min) && endInx == -1)
{
endInx = i;
}
}

if (startInx != -1 && endInx != -1)
break;
}

return new Vector2Int(startInx,endInx);
}

private UIItemRender CreateItemRender()
{
UIItemRender item = null;
if (unUseItems.Count > 0)
{
item = unUseItems.Dequeue();
item.gameObject.SetActive(true);
return item;
}
GameObject obj = NGUITools.AddChild(content, itemPrefab);
item = obj.GetComponent<UIItemRender>();
if (item == null)
item = obj.AddComponent<UIItemRender>();
if (obj.GetComponent<UIDragScrollView>() != null)
UIEventListener.Get(obj).onPress += OnPressItem;

obj.SetActive(true);

return item;
}

private void OnPressItem(GameObject obj, bool isPress)
{
if (isPress)
{
pressObj = obj;
}
else
{
if (obj == pressObj)
pressObj = null;
}
}

private bool RecoverItemRender(UIItemRender item)
{
bool result = false;
if (item.gameObject == pressObj && isDraging)
{

pressObj = null;
result = true;
}

unUseItems.Enqueue(item);
item.SetSelected(false);
item.gameObject.SetActive(false);
return result;
}

private void UpdateSize()
{
Vector2 tempSize = Vector2.zero;
for(int i = 0; i < this.dataList.Count; ++i)
{
Vector2 size = this.GetSizeByInx(i);
if (this.layout == Layout.Vertical)
{
tempSize.x = size.x;
tempSize.y = tempSize.y + size.y;
}
else if (this.layout == Layout.Horizontal)
{
tempSize.x = tempSize.x + size.x;
tempSize.y = size.y;
}
}

UIWidget widget = this.content.GetComponent<UIWidget>();
widget.SetDimensions(Mathf.FloorToInt(tempSize.x), Mathf.FloorToInt(tempSize.y));
}

private void UpdateView()
{
this.Init();
this.UpdateSize();

Vector3[] corners = mPanel.worldCorners;
for (int i = 0; i < 4; ++i)
{
Vector3 v = corners[i];
v = this.content.transform.InverseTransformPoint(v);
corners[i] = v;
}

float min = 0;
float max = 0;
int tmpSizeH = 100;
int tmpSizeW = 100;
int maxInx = 0;
int startInx = 0;
int endInx = 0;

if (this.layout == Layout.Vertical)
{
min = corners[0].y;
max = corners[2].y;
max += tmpSizeH;
min -= tmpSizeH;
min = Mathf.Min(0, min);
max = Mathf.Min(0, max);
maxInx = Mathf.Max(0, this.dataList.Count - 1);
tmpSizeW = 0;

Vector2Int tempInx = this.GetStartInxAndEndInx(min,max);
startInx = tempInx.x;
endInx = tempInx.y;
startInx = Mathf.Min(startInx, maxInx);
endInx = Mathf.Min(endInx, maxInx);
}
else if (this.layout == Layout.Horizontal)
{
min = corners[0].x;
max = corners[2].x;
max += tmpSizeW;
min -= tmpSizeW;
min = Mathf.Max(0, min);
max = Mathf.Max(0, max);
maxInx = Mathf.Max(0, this.dataList.Count - 1);
tmpSizeH = 0;

Vector2Int tempInx = this.GetStartInxAndEndInx(min,max);
startInx = tempInx.x;
endInx = tempInx.y;
startInx = Mathf.Min(startInx, maxInx);
endInx = Mathf.Min(endInx, maxInx);
}

int count = this.dataList.Count;
Dictionary<int, UIItemRender>.Enumerator enumerator = this.dirCurItems.GetEnumerator();
bool recoverFocus = false;
while (enumerator.MoveNext())
{

int key = enumerator.Current.Key;
if (count > 0 && key >= startInx && key <= endInx)
{
this.tmpDic2[key] = enumerator.Current.Value;
}
else
{
bool tmp = this.RecoverItemRender(enumerator.Current.Value);
if (tmp)
recoverFocus = true;
}
}

this.dirCurItems.Clear();
Dictionary<int, UIItemRender> tmpDic = null;
tmpDic = dirCurItems;
dirCurItems = tmpDic2;
tmpDic2 = tmpDic;

if (count > 0 && startInx >= 0)
{

for (int i = startInx; i <= endInx; i++)
{
UIItemRender item = null;
Vector3 position = this.GetLocalPosByInx(i);
if (!this.dirCurItems.TryGetValue(i, out item))
{
item = this.CreateItemRender();
item.__index = i;
this.dirCurItems[i] = item;
item.SetData(this.dataList[i]);
}
item.gameObject.name = i.ToString();
item.transform.localPosition = position;
}
}
if (recoverFocus)
this.mScrollView.Press(false);
}
}

第二版

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;
using LuaInterface;

// public enum Layout
// {
// Vertical,
// Horizontal
// }

/// <summary>
/// 无限Item列表
/// </summary>
public class UIDictListView : MonoBehaviour
{

public Layout layout;
public Vector2 spacing;
public GameObject itemPrefab;
private Vector3 panelDefaultPos;
private Vector2 panelDefaultClipOffset;
private UIPanel mPanel;
public UIScrollView mScrollView;
private UIItemRender itemRender = null;
private bool initialized = false;
private GameObject content;
private Vector2 scrollRectSize;
public bool bBackLoad = false;
public bool isCheackBackLoad = false;
public List<object> dataList = new List<object>();
private List<UIItemRender> lstItems = new List<UIItemRender>();
private GameObject pressObj;
private bool isDraging = false;
float svLastPos = 0; //记录scrollviet上一次的位置,用于判断scrollview的移动方向

void Awake()
{
this.Init();
}

public void Init()
{

if (initialized)
return;
initialized = true;

mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
this.mScrollView = mPanel.GetComponent<UIScrollView>();
this.mPanel.onClipMove += OnUIPanelClipMove;
this.panelDefaultPos = mPanel.transform.localPosition;
this.panelDefaultClipOffset = mPanel.clipOffset;

// record some sizes
scrollRectSize = mPanel.GetViewSize();

// add a scrollrect content
GameObject go = new GameObject ();
go.name = "content";
go.transform.SetParent(transform);

UIWidget uiwidget = go.AddComponent<UIWidget>();
uiwidget.SetDimensions((int)scrollRectSize.x,(int)scrollRectSize.y);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
this.content = go;

//itemPrefab
GameObject item = GameObject.Instantiate(itemPrefab) as GameObject;
item.transform.SetParent(mPanel.transform.parent);
item.transform.localPosition = new Vector3(1500, 1500, 1);
itemRender = item.GetComponent<UIItemRender>();
if (itemRender == null)
itemRender = item.AddComponent<UIItemRender>();

this.mScrollView.onDragStarted += delegate ()
{
this.isDraging = true;
};
this.mScrollView.onDragFinished += delegate ()
{
this.isDraging = false;
this.pressObj = null;
};
}

public void AddData(object obj)
{
this.dataList.Add(obj);
}

public void InsertData(object data,int inx)
{
this.dataList.Insert(inx, data);
}

public void ClearAll()
{
this.dataList.Clear();
}

public void UpdateNow()
{
this.Init();
this.AutoContentPivot();
this.UpdateView();
}

private void ClearAllItems()
{
var enumerator = this.lstItems.GetEnumerator();
while (enumerator.MoveNext())
{
Destroy(enumerator.Current.gameObject);
}
this.lstItems.Clear();
}

public bool CheckBackLoad(LuaTable table)
{
List<LuaTable> tableList = LuaTools.GetLuaTableToList<LuaTable>(table);
float containerH = 0;
for(int i = 0; i < tableList.Count; ++i)
{
itemRender.SetData(tableList[i]);
UIWidget itemWidget = itemRender.GetComponent<UIWidget>();
containerH = containerH + itemWidget.height;
// 初始布局,最多保证可隐藏的区域大于显示区域就可以了
if (containerH > (scrollRectSize.y))
{
return true;
}
}

return false;
}

public void AutoContentPivot()
{
this.ClearAllItems();

// 设置锚点
UIWidget contentWidget = this.content.GetComponent<UIWidget>();
contentWidget.SetDimensions((int)scrollRectSize.x,(int)scrollRectSize.y);
if(bBackLoad)
{
mPanel.transform.localPosition = this.panelDefaultPos - new Vector3(0,scrollRectSize.y - 1, 0);
mPanel.clipOffset = this.panelDefaultClipOffset + new Vector2(0,scrollRectSize.y - 1);

this.mScrollView.contentPivot = UIWidget.Pivot.BottomLeft;
contentWidget.pivot = UIWidget.Pivot.BottomLeft;
}
else
{
mPanel.transform.localPosition = this.panelDefaultPos;
mPanel.clipOffset = this.panelDefaultClipOffset;

this.mScrollView.contentPivot = UIWidget.Pivot.TopLeft;
contentWidget.pivot = UIWidget.Pivot.TopLeft;
}
this.content.transform.localPosition = Vector3.zero;
this.content.transform.localScale = Vector3.one;
}

private void OnUIPanelClipMove(UIPanel panel)
{
Vector3 delta = panelDefaultPos - panel.transform.localPosition;
float distance = delta.y;

float moveDis = this.mScrollView.transform.localPosition.y - svLastPos;
if (Mathf.Abs(moveDis) > 0.05 && lstItems.Count > 0)
{
bool isup = moveDis > 0;
if (isup)
{
if(bBackLoad)
{
float height = lstItems[lstItems.Count - 1].GetComponent<UIWidget>().height;
while (lstItems[lstItems.Count - 1].transform.localPosition.y - height > distance &&
lstItems[0].__index > 0)
{
UIItemRender itemRender = lstItems[lstItems.Count - 1];
int idx = lstItems[0].__index - 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();

lstItems.Insert(0, itemRender);
lstItems.RemoveAt(lstItems.Count - 1);

UIItemRender tempRender = lstItems[1];
float _height = tempRender.GetComponent<UIWidget>().height; //锚点左上角所以加itemRender height
itemRender.transform.localPosition = tempRender.transform.localPosition - new Vector3(0, _height, 0);
}
}
else
{
float height = lstItems[0].GetComponent<UIWidget>().height;
while (lstItems[0].transform.localPosition.y - height > distance &&
lstItems[lstItems.Count - 1].__index < dataList.Count - 1)
{
UIItemRender itemRender = lstItems[0];
int idx = lstItems[lstItems.Count - 1].__index + 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();

lstItems.Add(itemRender);
lstItems.RemoveAt(0);


UIItemRender tempRender = lstItems[lstItems.Count - 2];
float _height = tempRender.GetComponent<UIWidget>().height; //锚点左上角所以加tempRender height
itemRender.transform.localPosition = tempRender.transform.localPosition - new Vector3(0, _height, 0);
}
}
}
else
{
if(bBackLoad)
{
float height = lstItems[0].GetComponent<UIWidget>().height;
distance = distance - scrollRectSize.y;
while (distance > lstItems[0].transform.localPosition.y &&
lstItems[lstItems.Count - 1].__index < dataList.Count - 1)
{
UIItemRender itemRender = lstItems[0];
int idx = lstItems[lstItems.Count - 1].__index + 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();

lstItems.Add(itemRender);
lstItems.RemoveAt(0);


UIItemRender tempRender = lstItems[lstItems.Count - 2];
float _height = itemRender.GetComponent<UIWidget>().height; //锚点左上角所以加tempRender height
itemRender.transform.localPosition = tempRender.transform.localPosition + new Vector3(0, _height, 0);
}
}
else
{
float height = lstItems[lstItems.Count - 1].GetComponent<UIWidget>().height;
float temp = Mathf.Abs(distance) + scrollRectSize.y;
while (lstItems[lstItems.Count - 1].transform.localPosition.y < - temp &&
lstItems[0].__index > 0)
{

UIItemRender itemRender = lstItems[lstItems.Count - 1];
int idx = lstItems[0].__index - 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();

lstItems.Insert(0, itemRender);
lstItems.RemoveAt(lstItems.Count - 1);

UIItemRender tempRender = lstItems[1];
float _height = itemRender.GetComponent<UIWidget>().height; //锚点左上角所以加itemRender height
itemRender.transform.localPosition = tempRender.transform.localPosition + new Vector3(0, _height, 0);
}
}

}
svLastPos = this.mScrollView.transform.localPosition.y;
}
}

private UIItemRender loadOneItem()
{
GameObject item = GameObject.Instantiate(itemPrefab) as GameObject;
UIItemRender itemRender = item.GetComponent<UIItemRender>();
item.transform.SetParent(this.content.transform);
item.transform.localScale = Vector3.one;

lstItems.Add(itemRender);
return itemRender;
}

private void UpdateView()
{
svLastPos = 0;
this.ClearAllItems();
float containerH = 0;

Vector3 itemPosition = Vector3.zero;
for(int i = 0; i < dataList.Count; ++i)
{
UIItemRender itemRender = this.loadOneItem();
itemRender.SetData(dataList[i]);
itemRender.__index = i;
itemRender.gameObject.name = i.ToString();

UIWidget widget = itemRender.GetComponent<UIWidget>();
if(bBackLoad)
{
containerH = containerH + widget.height; // 锚点在左上所以位置不是从0开始
itemPosition.y = itemPosition.y + widget.height;
itemRender.transform.localPosition = new Vector3(0,itemPosition.y, 0);
}
else
{
itemRender.transform.localPosition = new Vector3(0,itemPosition.y, 0);
containerH = containerH + widget.height;
itemPosition.y = itemPosition.y - widget.height;
}

// 初始布局,最多保证可隐藏的区域大于显示区域就可以了
if (containerH > (scrollRectSize.y * 1.5f))
{
break;
}
}
}
}