diff --git a/Assets/Scripts/UI/GList.cs b/Assets/Scripts/UI/GList.cs index f19ed898..6d4ec71e 100644 --- a/Assets/Scripts/UI/GList.cs +++ b/Assets/Scripts/UI/GList.cs @@ -71,6 +71,7 @@ public class GList : GComponent bool _virtual; bool _loop; int _numItems; + List _dataList; // list自身绑定的数据 int _realNumItems; int _firstIndex; //the top left index int _curLineItemCount; //item count in one line @@ -104,7 +105,8 @@ public GList() rootContainer.gameObject.name = "GList"; _pool = new GObjectPool(container.cachedTransform); - + _dataList = new List(); + _itemClickDelegate = __clickItem; } @@ -118,7 +120,8 @@ public override void Dispose() scrollItemToViewOnClick = false; itemRenderer = null; itemProvider = null; - + _dataList.Clear(); + _dataList = null; base.Dispose(); } @@ -1500,6 +1503,22 @@ void SetVirtual(bool loop) } } + /// + /// 设置list数据源 + /// 设置后会绑定数据在每个子对象,可以根据 dataSource 来直接获取数据 + /// + public List dataList + { + get{ + return _dataList; + } + set + { + _dataList = value; + numItems = value.Count; + } + } + /// /// Set the list item count. /// If the list is not virtual, specified number of items will be created. @@ -1571,7 +1590,14 @@ public int numItems if (itemRenderer != null) { for (int i = 0; i < value; i++) - itemRenderer(i, GetChildAt(i)); + { + var child = GetChildAt(i); + if (i < _dataList.Count) + {// 赋值 + child.dataSource = _dataList[i]; + } + itemRenderer(i, child); + } } } } @@ -2032,6 +2058,11 @@ bool HandleScroll1(bool forceUpdate) if (_autoResizeItem && (_layout == ListLayoutType.SingleColumn || _columnCount > 0)) ii.obj.SetSize(partSize, ii.obj.height, true); + if (curIndex % _numItems < _dataList.Count) + {// 赋值 + ii.obj.dataSource = _dataList[curIndex % _numItems]; + } + itemRenderer(curIndex % _numItems, ii.obj); if (curIndex % _curLineItemCount == 0) { @@ -2201,6 +2232,11 @@ bool HandleScroll2(bool forceUpdate) if (_autoResizeItem && (_layout == ListLayoutType.SingleRow || _lineCount > 0)) ii.obj.SetSize(ii.obj.width, partSize, true); + if (curIndex % _numItems < _dataList.Count) + {// 赋值 + ii.obj.dataSource = _dataList[curIndex % _numItems]; + } + itemRenderer(curIndex % _numItems, ii.obj); if (curIndex % _curLineItemCount == 0) { @@ -2382,6 +2418,11 @@ void HandleScroll3(bool forceUpdate) else if (_curLineItemCount2 == _lineCount) ii.obj.SetSize(ii.obj.width, partHeight, true); } + + if (i % _numItems < _dataList.Count) + {// 赋值 + ii.obj.dataSource = _dataList[i % _numItems]; + } itemRenderer(i % _numItems, ii.obj); ii.size.x = Mathf.CeilToInt(ii.obj.size.x); diff --git a/Assets/Scripts/UI/GObject.cs b/Assets/Scripts/UI/GObject.cs index 55431701..b700a5ad 100644 --- a/Assets/Scripts/UI/GObject.cs +++ b/Assets/Scripts/UI/GObject.cs @@ -22,6 +22,12 @@ public class GObject : EventDispatcher /// public object data; + /// + /// User defined data. + /// GList 内部会赋值 + /// + public object dataSource; + /// /// The source width of the object. ///