private void CreateItemByIndex(int dataIndex) { UIInfiniteTableItem item; if (mUnusedItemQueue.Count > 0) { item = mUnusedItemQueue.Dequeue(); } else { item = new UIInfiniteTableItem(); GameObject itemPrefab = ResourcesManager.Instance.ResourcesLoad <GameObject>("Prefabs/UI/" + mItemPrefabName); if (itemPrefab != null) { GameObject obj = AddChild(transform, itemPrefab); item.mObj = obj; } item.mChildIndex = childCount; childCount++; //返回 item 挂的脚本回调 if (onGetItemComponent != null) { onGetItemComponent(item.mObj); } } item.mDataIndex = dataIndex; item.OnShow(GetItemPosByIndex(dataIndex)); mItemList.Add(item); //刷洗 item 数据 if (onReposition != null) { onReposition(item.mObj, dataIndex, item.mChildIndex); } }
/// <summary> /// 根据数据index 获取对应的item ChildIndex /// </summary> public int GetItemChildIndexByIndex(int index) { for (int i = 0; i < mItemList.Count; i++) { UIInfiniteTableItem item = mItemList[i]; if (item.mDataIndex == index) { return(item.mChildIndex); } } return(0); }
//刷新 private void Refresh() { int index = GetIndexByPanelPos(); //满足刷新条件 if (mCurLineIndex != index && index > -1) { mCurLineIndex = index; //移除不再显示范围内的 item for (int i = mItemList.Count; i > 0; i--) { UIInfiniteTableItem item = mItemList[i - 1]; if (item.mDataIndex < index * mColumns || (item.mDataIndex >= (index + mMaxLineCount) * mColumns) || item.mDataIndex > mTableDataCount - 1) { item.OnHide(); //移除 mItemList.Remove(item); //添加到队列 mUnusedItemQueue.Enqueue(item); } } //显示满足条件的 item for (int i = mCurLineIndex * mColumns; i < (mCurLineIndex + mMaxLineCount) * mColumns; i++) { if (i < 0) { continue; } //大于数据量 -- 不足以显示 if (i > mTableDataCount - 1) { continue; } bool isOk = false; foreach (UIInfiniteTableItem item in mItemList) { if (item.mDataIndex == i) { isOk = true; } } if (isOk) { continue; } CreateItemByIndex(i); } } }