Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glist 自定义数据绑定 #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions Assets/Scripts/UI/GList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class GList : GComponent
bool _virtual;
bool _loop;
int _numItems;
List<object> _dataList; // list自身绑定的数据
int _realNumItems;
int _firstIndex; //the top left index
int _curLineItemCount; //item count in one line
Expand Down Expand Up @@ -104,7 +105,8 @@ public GList()
rootContainer.gameObject.name = "GList";

_pool = new GObjectPool(container.cachedTransform);

_dataList = new List<object>();

_itemClickDelegate = __clickItem;
}

Expand All @@ -118,7 +120,8 @@ public override void Dispose()
scrollItemToViewOnClick = false;
itemRenderer = null;
itemProvider = null;

_dataList.Clear();
_dataList = null;
base.Dispose();
}

Expand Down Expand Up @@ -1500,6 +1503,22 @@ void SetVirtual(bool loop)
}
}

/// <summary>
/// 设置list数据源
/// 设置后会绑定数据在每个子对象,可以根据 dataSource 来直接获取数据
/// </summary>
public List<object> dataList
{
get{
return _dataList;
}
set
{
_dataList = value;
numItems = value.Count;
}
}

/// <summary>
/// Set the list item count.
/// If the list is not virtual, specified number of items will be created.
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions Assets/Scripts/UI/GObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class GObject : EventDispatcher
/// </summary>
public object data;

/// <summary>
/// User defined data.
/// GList 内部会赋值
/// </summary>
public object dataSource;

/// <summary>
/// The source width of the object.
/// </summary>
Expand Down