void CreateCells(object[] _objs) { unitName = go.name; for (int i = 0; i < rowNum * colNum; i++) { GameObject unit = GameObject.Instantiate(go); unit.transform.SetParent(pool.transform, false); (unit.transform as RectTransform).localScale = new Vector3(cellScale, cellScale, cellScale); (unit.transform as RectTransform).pivot = new Vector2(0, 1); (unit.transform as RectTransform).anchorMin = new Vector2(0, 1); (unit.transform as RectTransform).anchorMax = new Vector2(0, 1); SuperListCell cell = unit.GetComponent <SuperListCell>(); cell.Init(this, _objs); if (isAlphaIn) { cell.canvasGroup = unit.GetComponent <CanvasGroup>(); if (cell.canvasGroup == null) { cell.canvasGroup = unit.AddComponent <CanvasGroup>(); } } hidePool.Add(cell); } }
private void HideCell(SuperListCell _cell) { //_cell.gameObject.SetActive(false); _cell.canvasGroup.alpha = 0;//for no gc set alpha to zero _cell.canvasGroup.blocksRaycasts = false; }
private void ShowCell(SuperListCell _cell) { //_cell.gameObject.SetActive(true); _cell.canvasGroup.alpha = 1; _cell.canvasGroup.blocksRaycasts = true; }
private void OneAlphaInOver() { alphaInList.RemoveAt(0); if (alphaInList.Count > 0) { SuperListCell cell = alphaInList[0]; cell.AlphaIn(OneAlphaInOver); } }
private bool hasSetContainerSize = false;//由于在setData之前就调用OnScroll方法会导致报错 所以确保在OnScroll之前就已经setDta了 public void SetData <T>(List <T> _data) { scrollRect.StopMovement(); if (isAlphaIn) { if (showPool.Count > 0) { for (int i = 0; i < showPool.Count; i++) { SuperListCell unit = showPool[i]; HideCell(unit); //unit.gameObject.SetActive(false); //unit.transform.SetParent(pool.transform, false); hidePool.Add(unit); } showPool.Clear(); } } if (data.Count > 0) { data.Clear(); } for (int i = 0; i < _data.Count; i++) { T unit = _data[i]; data.Add(unit); } SetContainerSize(); showIndex = 0; ResetPos(0, true, false); //由于Rect2DMask有bug 所以添加这2行2B代码 container.SetActive(false); container.SetActive(true); }
public void UpdateItemAt(int _index, object _data) { data[_index] = _data; for (int i = 0; i < showPool.Count; i++) { SuperListCell cell = showPool[i]; if (cell.index == _index) { cell.SetData(_data); break; } } }
public void SetSelectedIndex(int _index) { if (selectedIndex == _index) { return; } if (needSelectedIndex) { for (int i = 0; i < showPool.Count; i++) { SuperListCell cell = showPool[i]; if (cell.index == _index) { cell.SetSelected(true); } else if (cell.index == selectedIndex) { cell.SetSelected(false); } } selectedIndex = _index; } if (selectedIndex != -1 || !needSelectedIndex) { if (CellClickHandle != null) { CellClickHandle(data[_index]); } if (CellClickIndexHandle != null) { CellClickIndexHandle(_index); } } }
private void SetCellIndex(SuperListCell _cell, int _index) { int row; int col; float xFix = 0; float yFix = 0; if (isVertical) { row = _index / colNum; col = _index % colNum; if (insertIndexArr != null) { for (int i = 0; i < insertIndexArr.Length; i++) { if (row >= insertIndexArr[i]) { yFix += insertRectArr[i].rect.height + verticalGap; } else { break; } } } } else { col = _index / rowNum; row = _index % rowNum; if (insertIndexArr != null) { for (int i = 0; i < insertIndexArr.Length; i++) { if (col >= insertIndexArr[i]) { xFix += insertRectArr[i].rect.width + horizontalGap; } else { break; } } } } float xPos = col * (cellWidth + horizontalGap) + xFix; float yPos = -row * (cellHeight + verticalGap) - yFix; Vector2 pos = new Vector2(xPos, yPos); (_cell.transform as RectTransform).anchoredPosition = pos; _cell.index = _index; //_cell.gameObject.name = unitName + _index; _cell.SetSelected(_index == selectedIndex);//如果补全空的单元格 这里可能会有问题 }
private void ResetPos(int _nowIndex, bool _dataHasChange, bool _insertHasChange) { // if (data.Count == 0) { // // return; // } if (isVertical) { if (_nowIndex - showIndex == colNum) { for (int i = 0; i < _nowIndex - showIndex; i++) { int newIndex = showIndex + rowNum * colNum + i; SuperListCell unit = showPool[0]; showPool.RemoveAt(0); if (newIndex < data.Count) { showPool.Add(unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } else { hidePool.Add(unit); HideCell(unit); //unit.gameObject.SetActive(false); //unit.transform.SetParent(pool.transform, false); } } } else if (_nowIndex - showIndex == -colNum) { for (int i = 0; i < showIndex - _nowIndex; i++) { int newIndex = showIndex - colNum + i; SuperListCell unit; if (showPool.Count == rowNum * colNum) { unit = showPool[showPool.Count - 1]; showPool.RemoveAt(showPool.Count - 1); } else { unit = hidePool[0]; hidePool.RemoveAt(0); ShowCell(unit); //unit.gameObject.SetActive(true); //unit.transform.SetParent(container.transform, false); } showPool.Insert(0, unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } } else { for (int i = 0; i < rowNum * colNum; i++) { if (_nowIndex + i < data.Count) { tmpDic.Add(_nowIndex + i, i); } else { break; } } for (int i = 0; i < showPool.Count; i++) { SuperListCell unit = showPool[i]; int tmpIndex = unit.index; int ii; bool b = tmpDic.TryGetValue(tmpIndex, out ii); if (b) { newShowPool[ii] = unit; if (_dataHasChange) { SetCellData(unit, tmpIndex);//如果列表数据已经发生了变化则需要在这里进行一次单元格赋值 } if (_insertHasChange) { SetCellIndex(unit, tmpIndex); } } else { replacePool.Add(unit); } } showPool.Clear(); IEnumerator <KeyValuePair <int, int> > enumerator = tmpDic.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <int, int> pair = enumerator.Current; if (newShowPool[pair.Value] == null) { SuperListCell unit; if (replacePool.Count > 0) { unit = replacePool[0]; replacePool.RemoveAt(0); } else { unit = hidePool[0]; hidePool.RemoveAt(0); ShowCell(unit); //unit.gameObject.SetActive(true); //unit.transform.SetParent(container.transform, false); } newShowPool[pair.Value] = unit; SetCellData(unit, pair.Key); SetCellIndex(unit, pair.Key); } showPool.Add(newShowPool[pair.Value]); } for (int i = 0; i < replacePool.Count; i++) { SuperListCell unit = replacePool[i]; HideCell(unit); //unit.gameObject.SetActive(false); //unit.transform.SetParent(pool.transform, false); hidePool.Add(unit); } for (int i = 0; i < tmpDic.Count; i++) { newShowPool[i] = null; } tmpDic.Clear(); replacePool.Clear(); } } else { if (_nowIndex - showIndex == rowNum) { for (int i = 0; i < _nowIndex - showIndex; i++) { int newIndex = showIndex + rowNum * colNum + i; SuperListCell unit = showPool[0]; showPool.RemoveAt(0); if (newIndex < data.Count) { showPool.Add(unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } else { hidePool.Add(unit); HideCell(unit); //unit.gameObject.SetActive(false); //unit.transform.SetParent(pool.transform, false); } } } else if (_nowIndex - showIndex == -rowNum) { for (int i = 0; i < showIndex - _nowIndex; i++) { int newIndex = showIndex - rowNum + i; SuperListCell unit; if (showPool.Count == rowNum * colNum) { unit = showPool[showPool.Count - 1]; showPool.RemoveAt(showPool.Count - 1); } else { unit = hidePool[0]; hidePool.RemoveAt(0); ShowCell(unit); //unit.gameObject.SetActive(true); //unit.transform.SetParent(container.transform, false); } showPool.Insert(0, unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } } else { for (int i = 0; i < rowNum * colNum; i++) { if (_nowIndex + i < data.Count) { tmpDic.Add(_nowIndex + i, i); } else { break; } } for (int i = 0; i < showPool.Count; i++) { SuperListCell unit = showPool[i]; int tmpIndex = unit.index; int ii; bool b = tmpDic.TryGetValue(tmpIndex, out ii); if (b) { newShowPool[ii] = unit; if (_dataHasChange) { SetCellData(unit, tmpIndex); } if (_insertHasChange) { SetCellIndex(unit, tmpIndex); } } else { replacePool.Add(unit); } } showPool.Clear(); IEnumerator <KeyValuePair <int, int> > enumerator = tmpDic.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <int, int> pair = enumerator.Current; if (newShowPool[pair.Value] == null) { SuperListCell unit; if (replacePool.Count > 0) { unit = replacePool[0]; replacePool.RemoveAt(0); } else { unit = hidePool[0]; hidePool.RemoveAt(0); ShowCell(unit); //unit.gameObject.SetActive(true); //unit.transform.SetParent(container.transform, false); } newShowPool[pair.Value] = unit; SetCellData(unit, pair.Key); SetCellIndex(unit, pair.Key); } showPool.Add(newShowPool[pair.Value]); } for (int i = 0; i < replacePool.Count; i++) { SuperListCell unit = replacePool[i]; HideCell(unit); //unit.gameObject.SetActive(false); //unit.transform.SetParent(pool.transform, false); hidePool.Add(unit); } for (int i = 0; i < tmpDic.Count; i++) { newShowPool[i] = null; } tmpDic.Clear(); replacePool.Clear(); } } showIndex = _nowIndex; if (showEmptyCell) { for (int i = showPool.Count; i < minShowNum; i++) { SuperListCell unit = hidePool[0]; hidePool.RemoveAt(0); ShowCell(unit); //unit.gameObject.SetActive(true); //unit.transform.SetParent(container.transform, false); showPool.Add(unit); SetCellData(unit, -1); SetCellIndex(unit, showIndex + i); } } }
public void CellClick(SuperListCell _cell) { SetSelectedIndex(_cell.index); }
private void SetCellData(SuperListCell _cell, int _index) { if (_index == -1) { _cell.SetData(null); } else if (!isAlphaIn) { _cell.SetData(data[_index]); } else { bool result = _cell.SetData(data[_index]); if (result) { _cell.canvasGroup.alpha = 0; int index = alphaInList.IndexOf(_cell); bool needStart = false; if (index != -1) { if (index == 0) { _cell.StopAlphaIn(); needStart = true; } alphaInList.RemoveAt(index); } alphaInList.Add(_cell); if (alphaInList.Count == 1 || needStart) { alphaInList[0].AlphaIn(OneAlphaInOver); } } else { _cell.canvasGroup.alpha = 1; int index = alphaInList.IndexOf(_cell); bool needStart = false; if (index != -1) { if (index == 0) { _cell.StopAlphaIn(); needStart = true; } alphaInList.RemoveAt(index); } if (needStart && alphaInList.Count > 0) { alphaInList[0].AlphaIn(OneAlphaInOver); } } } }
private void ResetPos(int _nowIndex, bool _dataHasChange, bool _insertHasChange) { // if (data.Count == 0) { // // return; // } if (isVertical) { if (_nowIndex - showIndex == colNum) { for (int i = 0; i < _nowIndex - showIndex; i++) { int newIndex = showIndex + rowNum * colNum + i; SuperListCell unit = showPool [0]; showPool.RemoveAt(0); if (newIndex < data.Count) { showPool.Add(unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } else { hidePool.Add(unit); unit.transform.SetParent(pool.transform, false); } } } else if (_nowIndex - showIndex == -colNum) { for (int i = 0; i < showIndex - _nowIndex; i++) { int newIndex = showIndex - colNum + i; SuperListCell unit; if (showPool.Count == rowNum * colNum) { unit = showPool [showPool.Count - 1]; showPool.RemoveAt(showPool.Count - 1); } else { unit = hidePool [0]; hidePool.RemoveAt(0); unit.transform.SetParent(container.transform, false); } showPool.Insert(0, unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } } else { List <int> tmpList = new List <int> (); for (int i = 0; i < rowNum * colNum; i++) { if (_nowIndex + i < data.Count) { tmpList.Add(_nowIndex + i); } else { break; } } SuperListCell[] newShowPool = new SuperListCell[tmpList.Count]; List <SuperListCell> replacePool = new List <SuperListCell> (); for (int i = 0; i < showPool.Count; i++) { SuperListCell unit = showPool [i]; int tmpIndex = unit.index; if (tmpList.Contains(tmpIndex)) { newShowPool [tmpList.IndexOf(tmpIndex)] = unit; if (_dataHasChange) { SetCellData(unit, tmpIndex); //如果列表数据已经发生了变化则需要在这里进行一次单元格赋值 } if (_insertHasChange) { SetCellIndex(unit, tmpIndex); } } else { replacePool.Add(unit); } } showPool.Clear(); for (int i = 0; i < newShowPool.Length; i++) { if (newShowPool [i] == null) { SuperListCell unit; if (replacePool.Count > 0) { unit = replacePool [0]; replacePool.RemoveAt(0); } else { unit = hidePool [0]; hidePool.RemoveAt(0); unit.transform.SetParent(container.transform, false); } newShowPool [i] = unit; SetCellData(unit, tmpList [i]); SetCellIndex(unit, tmpList[i]); } showPool.Add(newShowPool [i]); } for (int i = 0; i < replacePool.Count; i++) { SuperListCell unit = replacePool[i]; unit.transform.SetParent(pool.transform, false); hidePool.Add(unit); } } } else { if (_nowIndex - showIndex == rowNum) { for (int i = 0; i < _nowIndex - showIndex; i++) { int newIndex = showIndex + rowNum * colNum + i; SuperListCell unit = showPool [0]; showPool.RemoveAt(0); if (newIndex < data.Count) { showPool.Add(unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } else { hidePool.Add(unit); unit.transform.SetParent(pool.transform, false); } } } else if (_nowIndex - showIndex == -rowNum) { for (int i = 0; i < showIndex - _nowIndex; i++) { int newIndex = showIndex - rowNum + i; SuperListCell unit; if (showPool.Count == rowNum * colNum) { unit = showPool [showPool.Count - 1]; showPool.RemoveAt(showPool.Count - 1); } else { unit = hidePool [0]; hidePool.RemoveAt(0); unit.transform.SetParent(container.transform, false); } showPool.Insert(0, unit); SetCellData(unit, newIndex); SetCellIndex(unit, newIndex); } } else { List <int> tmpList = new List <int> (); for (int i = 0; i < rowNum * colNum; i++) { if (_nowIndex + i < data.Count) { tmpList.Add(_nowIndex + i); } else { break; } } SuperListCell[] newShowPool = new SuperListCell[tmpList.Count]; List <SuperListCell> replacePool = new List <SuperListCell> (); for (int i = 0; i < showPool.Count; i++) { SuperListCell unit = showPool [i]; int tmpIndex = unit.index; if (tmpList.Contains(tmpIndex)) { newShowPool [tmpList.IndexOf(tmpIndex)] = unit; if (_dataHasChange) { SetCellData(unit, tmpIndex); } if (_insertHasChange) { SetCellIndex(unit, tmpIndex); } } else { replacePool.Add(unit); } } showPool.Clear(); for (int i = 0; i < newShowPool.Length; i++) { if (newShowPool [i] == null) { SuperListCell unit; if (replacePool.Count > 0) { unit = replacePool [0]; replacePool.RemoveAt(0); } else { unit = hidePool [0]; hidePool.RemoveAt(0); unit.transform.SetParent(container.transform, false); } newShowPool [i] = unit; SetCellData(unit, tmpList [i]); SetCellIndex(unit, tmpList[i]); } showPool.Add(newShowPool [i]); } for (int i = 0; i < replacePool.Count; i++) { SuperListCell unit = replacePool[i]; unit.transform.SetParent(pool.transform, false); hidePool.Add(unit); } } } showIndex = _nowIndex; if (showEmptyCell) { for (int i = showPool.Count; i < minShowNum; i++) { SuperListCell unit = hidePool [0]; hidePool.RemoveAt(0); unit.transform.SetParent(container.transform, false); showPool.Add(unit); SetCellData(unit, -1); SetCellIndex(unit, showIndex + i); } } }