/** * @des:添加当前数据索引数据 */ public void AddItem(int dataIndex, float length) { AddWarpContentDataItem(dataIndex, length); if (dataIndex < 0) { return; } //检测是否需添加gameObject bool isNeedAdd = false; for (int i = listItem.Count - 1; i >= 0; i--) { ScrollRectExItem item = listItem [i]; if (item.Index >= (dataCount - 1)) { isNeedAdd = true; break; } } if (listItem.Count == 0) { isNeedAdd = true; } SetDataCount(dataCount + 1); if (isNeedAdd) { for (int i = 0; i < listItem.Count; i++) { ScrollRectExItem 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++) { ScrollRectExItem 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--) { ScrollRectExItem 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--) { ScrollRectExItem 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); } }