/** * @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--) { LoopListItme item = listItem[i]; if (item.Index >= (dataCount - 1)) { isNeedAdd = true; break; } } setDataCount(dataCount + 1); if (isNeedAdd) { for (int i = 0; i < listItem.Count; i++) { LoopListItme 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++) { LoopListItme item = listItem[i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex; } item = null; } } }
/** * @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--) { LoopListItme item = listItem[i]; int oldIndex = item.Index; if (oldIndex == dataIndex) { listItem.Remove(item); if (isNeedDestroyGameObject) { GameObject.Destroy(item.gameObject); } else { item.Index = -1; unUseItem.Enqueue(item); } } if (oldIndex > dataIndex) { item.Index = oldIndex - 1; } } setUpdateRectItem(getCurScrollPerLineIndex()); }
/** * @des:设置更新区域内item * 功能: * 1.隐藏区域之外对象 * 2.更新区域内数据 */ private void setUpdateRectItem(int scrollPerLineIndex) { if (scrollPerLineIndex < 0) { return; } curScrollPerLineIndex = scrollPerLineIndex; int startDataIndex = curScrollPerLineIndex * maxPerLine; int endDataIndex = (curScrollPerLineIndex + viewCount) * maxPerLine; //移除 for (int i = listItem.Count - 1; i >= 0; i--) { LoopListItme 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); } }