public virtual void Add(object data) { var model = new ScrollListItemModel(data, itemDefaultfSize); _itemModels.Add(model); MarkDirty(true); }
public virtual void Insert(int index, object data) { var model = new ScrollListItemModel(data, itemDefaultfSize); _itemModels.Insert(index, model); if (index <= lastStartIndex) { MarkDirty(true, EKeepPaddingType.END); } else { MarkDirty(true); } }
/// <summary> /// 创建一个列表项Item /// </summary> /// <param name="data"></param> /// <param name="dataIdx"></param> /// <param name="lastShowingItems"></param> /// <returns></returns> protected ScrollListItem CreateItem(ScrollListItemModel model, int dataIdx, Dictionary <ScrollListItemModel, ScrollListItem> lastShowingItems) { ScrollListItem item; if (lastShowingItems.ContainsKey(model) && lastShowingItems[model].index == dataIdx) //本来就在显示列表中,直接返回 { item = lastShowingItems[model]; lastShowingItems.Remove(model); } else { if (_recycledItems.Count > 0) { ScrollListItem tempItem = null; //tempItem = _recycledItems[0]; int idx = _recycledItems.Count; while (--idx > -1) { tempItem = _recycledItems[idx]; if (tempItem.index == dataIdx && tempItem.data.Equals(model.data)) //以前回收的一个对象,刚好对应的数据一致,直接返回 { item = tempItem; _recycledItems.RemoveAt(idx); item.gameObject.SetActive(true); RenderItem(item, model.data, false); return(item); } } //没有绝对匹配的,则抽取最后找到的item,之后刷新数据并返回 item = tempItem; _recycledItems.Remove(tempItem); //当一个列表项,将被复用时触发 onItemBeforeReuse?.Invoke(item); } else { var go = GameObject.Instantiate(itemPrefab, content); if (false == go.activeInHierarchy) { go.SetActive(true); } item = go.AddComponent <ScrollListItem>(); item.rectTransform.anchorMin = Vector2.up; item.rectTransform.anchorMax = Vector2.up; item.rectTransform.pivot = Vector2.up; //Debug.Log($"新生成的 Item GameObject"); } var name = string.Format("item_{0}", dataIdx); item.gameObject.name = name; item.index = dataIdx; item.data = model.data; item.width = _itemModels[dataIdx].width; item.height = _itemModels[dataIdx].height; if (false == item.gameObject.activeInHierarchy) { item.gameObject.SetActive(true); } RenderItem(item, model.data, true); } if (item.index != dataIdx) { //插入列表项可能导致数据索引改变 item.index = dataIdx; } return(item); }