/// <summary> /// 刷新当前Item /// </summary> public void UpdateCurrentItem() { for (int i = 0; i < listItem.Count; i++) { UIRectItem item = listItem[i]; item.Index = item.Index; } }
/** * @des:添加当前数据索引数据 */ public void AddItem(int dataIndex) { if (dataIndex < 0 || dataIndex > dataCount) { return; } //检测是否需添加gameObject bool isNeedAdd = false; for (int i = listItem.Count - 1; i >= 0; i--) { UIRectItem item = listItem[i]; if (item.Index >= (dataCount - 1)) { isNeedAdd = true; break; } } SetDataCount(dataCount + 1); if (isNeedAdd) { for (int i = 0; i < listItem.Count; i++) { UIRectItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex + 1; } item = null; } SetUpdateRectItem(GetCurScrollPerLineIndex()); } else { //重新刷新数据 for (int i = 0; i < listItem.Count; i++) { UIRectItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex; } item = null; } } }
public void UpdateItemPosition() { //位置设置 for (int i = listItem.Count - 1; i >= 0; i--) { UIRectItem item = listItem[i]; float currentLine = (item.Index / maxPerLine - curScrollPerLineIndex); if (lineInterval != 0f || lineOffset != 0f) { Vector3 orgPosition = GetLocalPositionByIndex(item.Index); if (lineInterval != 0f) { float currentOffset = 0f; int itemIndex = item.Index % maxPerLine; if (itemIndex != 0) { currentOffset = currentLine * lineIntervalFactor * lineInterval; currentOffset *= itemIndex; orgPosition.x += currentOffset; } } if (lineOffset != 0f) { float currentOffset = 0f; currentOffset = lineOffset / (displayLine - 1) * (float)(displayLine - currentLine); orgPosition.x += currentOffset; } item.SetItemPosition(orgPosition); } else { item.SetItemPosition(GetLocalPositionByIndex(item.Index)); } } }
/** * @des:设置更新区域内item * 功能: * 1.隐藏区域之外对象 * 2.更新区域内数据 */ private void SetUpdateRectItem(float scrollPerLineIndex) { if (scrollPerLineIndex < 0f) { return; } curScrollPerLineIndex = scrollPerLineIndex; int startDataIndex = Mathf.FloorToInt(curScrollPerLineIndex) * maxPerLine; int endDataIndex = startDataIndex + (displayLine + viewCount) * maxPerLine; //移除 for (int i = listItem.Count - 1; i >= 0; i--) { UIRectItem item = listItem[i]; int index = item.Index; if (index < startDataIndex || index >= endDataIndex) { item.Index = -1; listItem.Remove(item); unUseItem.Enqueue(item); } } //显示 for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++) { if (dataIndex >= dataCount) { continue; } if (IsExistDataByDataIndex(dataIndex)) { continue; } CreateItem(dataIndex); } UpdateItemPosition(); }
/** * @des:删除当前数据索引下数据 */ public void DelItem(int dataIndex) { if (dataIndex < 0 || dataIndex >= dataCount) { return; } //删除item逻辑三种情况 //1.只更新数据,不销毁gameObject,也不移除gameobject //2.更新数据,且移除gameObject,不销毁gameObject //3.更新数据,销毁gameObject bool isNeedDestroyGameObject = (listItem.Count >= dataCount); SetDataCount(dataCount - 1); for (int i = listItem.Count - 1; i >= 0; i--) { UIRectItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex == dataIndex) { listItem.Remove(item); if (isNeedDestroyGameObject) { Destroy(item.gameObject); } else { item.Index = -1; unUseItem.Enqueue(item); } } if (oldIndex > dataIndex) { item.Index = oldIndex - 1; } } SetUpdateRectItem(GetCurScrollPerLineIndex()); }