//RefreshItem private void setUpdateRectItem(int scrollPerLineIndex) { if (scrollPerLineIndex < 0) { return; } int startDataIndex = 0; int endDataIndex = 0; curScrollPerLineIndex = scrollPerLineIndex; if (alignment == Alignment.eHorizontal) { startDataIndex = curScrollPerLineIndex * CountOfRow; endDataIndex = (curScrollPerLineIndex + ViewCount) * CountOfRow; } else { startDataIndex = curScrollPerLineIndex * CountOfCol; endDataIndex = (curScrollPerLineIndex + ViewCount) * CountOfCol; } //move item when unused for (int i = listItem.Count - 1; i >= 0; i--) { UILoopItem item = listItem[i]; int index = item.Index; if (index < startDataIndex || index >= endDataIndex) { item.Index = 0; listItem.Remove(item); unUseItem.Enqueue(item); } else { item.UpdateItem(_datas, item.Index); } } for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++) { if (dataIndex >= dataCount) { continue; } if (isExistDataByDataIndex(dataIndex)) { continue; } createItem(dataIndex); } }
//add item public void AddItem(int dataIndex) { if (dataIndex < 0 || dataIndex > dataCount) { return; } bool isNeedAdd = false; for (int i = listItem.Count - 1; i >= 0; i--) { UILoopItem item = listItem[i]; if (item.Index >= (dataCount - 1)) { isNeedAdd = true; break; } } setDataCount(dataCount + 1); if (isNeedAdd) { for (int i = 0; i < listItem.Count; i++) { UILoopItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex + 1; item.UpdateItem(_datas, item.Index); } item = null; } setUpdateRectItem(getCurScrollPerLineIndex()); } else { for (int i = 0; i < listItem.Count; i++) { UILoopItem item = listItem[i]; int oldIndex = item.Index; if (oldIndex >= dataIndex) { item.Index = oldIndex; item.UpdateItem(_datas, item.Index); } item = null; } } }
public void DelItem(int dataIndex) { if (dataIndex < 0 || dataIndex >= dataCount) { return; } //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--) { UILoopItem 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()); }