private void addEndItem(int index) { ScrollItem item = getItem(); mCurItems.Add(item); setItemDataByIndex(item, index); }
private void addStartItem(int index) { ScrollItem item = getItem(); mCurItems.Insert(0, item); setItemDataByIndex(item, index); }
/// <summary> /// 重新设置ScrollView的ItemSize /// </summary> private void resetItemSize() { GameObject obj = Instantiate(mTarget.ItemPrefab); RectTransform rectTransform = obj.GetComponent <RectTransform>(); ScrollItem i = obj.GetComponent <ScrollItem>(); if (null == i) { LogFile.Error("ScrollView 的 Item 必须添加ScrollView组件!"); return; } if (null != rectTransform) { mTarget.ItemSize = rectTransform.rect.size; if (!rectTransform.anchorMin.x.Equals(rectTransform.anchorMax.x)) { mTarget.ItemSize.x = -1; } if (!rectTransform.anchorMin.y.Equals(rectTransform.anchorMax.y)) { mTarget.ItemSize.y = -1; } } DestroyImmediate(obj); serializedObject.ApplyModifiedProperties(); }
private void setItemDataByIndex(ScrollItem item, int index) { UIItemData data = mData[index]; item.Index = index; item.SetData(data); item.transform.localPosition = getItemPosByIndex(index); }
private void recoverAll() { for (int i = mCurItems.Count - 1; i > -1; --i) { ScrollItem item = mCurItems[i]; recoverItem(item); mCurItems.RemoveAt(i); } }
private ScrollItem getItemByIndex(int index) { ScrollItem item = null; //test //TODO:根据content position 计算index对应的Item,没有在显示区域的返回bull if (index >= mShowStart && index <= mShowEnd) { item = mCurItems[index - mShowStart]; } return(item); }
private void addEndLine(int line) { for (int i = 0; i < mNumPerLine; i++) { int index = line * mNumPerLine + i; if (index < mItemDatas.Count) { ScrollItem item = getItem(); mCurItems.Add(item); setItemDataByIndex(item, index); } } }
private void changeSelectAll(bool selected) { for (int i = 0; i < mItemDatas.Count; i++) { mItemSelectStatus[i] = selected; } for (int i = 0; i < mCurItems.Count; i++) { ScrollItem item = mCurItems[i]; item.IsSelected = selected; } onSelectStatusChanges(); }
private void _addStartLine(int line) { for (int i = 0; i < mNumPerLine; i++) { int index = line * mNumPerLine + i; if (index < mItemDatas.Count) { ScrollItem item = _getItem(); mCurItems.Insert(0, item); _setItemDataByIndex(item, index); } } }
private void recoverStartItem(int index) { if (index < 0 || index > mData.Count - 1) { return; } if (index < mData.Count) { ScrollItem item = mCurItems[0]; recoverItem(item); mCurItems.RemoveAt(0); } }
bool onPoolGetDelegate(ref ScrollItem obj) { if (null == obj) { GameObject gobj = Instantiate(ItemPrefab, this.content, false); gobj.name = "item" + mItemPool.TotalObjCount; //LogFile.Warn(gobj.name); obj = gobj.GetComponent <ScrollItem>(); if (null == obj) { LogFile.Error("ItemPrefab:{0} prefab没有添加ScrollItem组件", ItemPrefab.name); return(false); } obj.OnItemClicked = onItemClicked; obj.OnBtnClickedIndex = onItemBtnClickI; obj.OnBtnClickedStr = onItemBtnClickS; if (ItemSize.x.Equals(-1f)) { //修改锚点 Vector2 vector = obj.rectTransform.anchorMax; vector.x = 0.5f; obj.rectTransform.anchorMax = vector; vector = obj.rectTransform.anchorMin; vector.x = 0.5f; obj.rectTransform.anchorMin = vector; obj.rectTransform.pivot = vector; //修改 item 宽高 vector.x = mRealItemSize.x; vector.y = obj.rectTransform.rect.height; obj.rectTransform.sizeDelta = vector; } if (ItemSize.y.Equals(-1f)) { //修改锚点 Vector2 vector = obj.rectTransform.anchorMax; vector.y = 0.5f; obj.rectTransform.anchorMax = vector; vector = obj.rectTransform.anchorMin; vector.y = 0.5f; obj.rectTransform.anchorMin = vector; obj.rectTransform.pivot = vector; //修改 item 宽高 vector.x = obj.rectTransform.rect.width; vector.y = mRealItemSize.y; obj.rectTransform.sizeDelta = vector; } } obj.gameObject.SetActive(true); return(true); }
private void recoverEndItem(int index) { if (index < 0 || index > mData.Count - 1) { return; } if (index < mData.Count) { int idx = mCurItems.Count - 1; ScrollItem item = mCurItems[idx]; recoverItem(item); mCurItems.RemoveAt(idx); } }
//进入时刷新部分参数 void OnEnable() { mTarget = target as ScrollItem; if (null == mTarget) { return; } //mPrefab = mTarget.ItemPrefab; //Debug.Log(mPrefab); if (null == mTarget.UIHandler) { mTarget.UIHandler = mTarget.GetComponent <UIHandler>(); } }
private void recoverAll() { if (null == mCurItems) { return; } //LogFile.Log("recoverAll 1 mCurItems.Count:{0}, objPool.count:{1}", mCurItems.Count, mItemPool.Count); for (int i = mCurItems.Count - 1; i > -1; --i) { ScrollItem item = mCurItems[i]; recoverItem(item); mCurItems.RemoveAt(i); } //LogFile.Warn("recoverAll 2 mCurItems.Count:{0}, objPool.count:{1}", mCurItems.Count, mItemPool.Count); }
private void setItemSelected(int index, bool selected) { if (index >= 0 && index < mItemDatas.Count) { mItemSelectStatus[index] = selected; for (int i = 0; i < mCurItems.Count; i++) { ScrollItem item = mCurItems[i]; if (index == item.Index) { item.IsSelected = selected; return; } } } }
private void setItemDataByIndex(ScrollItem item, int index) { UIItemData data = mItemDatas[index]; item.Index = index; item.SetData(data); item.IsSelected = mItemSelectStatus[index]; RectTransform rect = item.rectTransform; Vector3 offset = Vector3.zero; if (!rect.pivot.Equals(Vector2.one * 0.5f)) { offset.x = rect.rect.width * (rect.pivot.x - 0.5f); offset.y = rect.rect.height * (rect.pivot.y - 0.5f); } item.transform.localPosition = getItemPosByIndex(index) + offset; }
private void recoverStartLine(int line) { if (line < 0 || line > mTotalLines - 1) { return; } for (int i = 0; i < mNumPerLine; i++) { int index = line * mNumPerLine + i; if (index < mItemDatas.Count) { ScrollItem item = mCurItems[0]; recoverItem(item); mCurItems.RemoveAt(0); } } }
/// <summary> /// 重新设置ScrollView的ItemSize /// </summary> private void resetItemSize() { GameObject obj = Instantiate(mTarget.ItemPrefab); RectTransform rectTransform = obj.GetComponent <RectTransform>(); ScrollItem i = obj.GetComponent <ScrollItem>(); if (null == i) { LogFile.Error("ScrollView 的 Item 必须添加ScrollView组件!"); return; } if (null != rectTransform) { mTarget.ItemSize = rectTransform.rect.size; } DestroyImmediate(obj); }
private void sortItems(bool force = false) { int curIndex = getCurIndex(); if (mCurIndex != curIndex || force) { int count = mCurItems.Count; //Debug.LogWarning("curIndex:" + curIndex); mCurIndex = curIndex; if (null != Toggles) { Toggles.SetCurIndex(mCurIndex); } List <ItemSortData> siblings = new List <ItemSortData>(); for (int i = 0; i < count; i++) { siblings.Add(new ItemSortData(i, mCurItems[i].Index)); //Debug.Log(siblings[i]); } siblings.Sort((ItemSortData left, ItemSortData right) => { int dl = Math.Abs(left.Index - mCurIndex); int dr = Math.Abs(right.Index - mCurIndex); if (dl > dr) { return(-1); } if (dl < dr) { return(1); } return(0); }); //string log = ""; for (int i = 0; i < siblings.Count; i++) { ScrollItem item = mCurItems[siblings[i].ListIndex]; item.transform.SetSiblingIndex(i); item.transform.localScale = Vector3.one * (mCurIndex == item.Index ? CenterScale : OtherScale); //log = log + "idx:" + item.Index + ",sib:" + i + ";"; } //Debug.Log(log); } }
bool OnGetItemDelegate(ref ScrollItem obj) { if (null == obj) { GameObject gobj = Instantiate(ItemPrefab, Content, false); gobj.name = "item" + mItemPool.TotalObjCount; //LogFile.Warn(gobj.name); obj = gobj.GetComponent <ScrollItem>(); if (null == obj) { LogFile.Error("ItemPrefab:{0} prefab没有添加ScrollItem组件", ItemPrefab.name); return(false); } obj.OnItemClicked = onItemClicked; } obj.transform.SetSiblingIndex(0); obj.gameObject.SetActive(true); return(true); }
private void recoverEndLine(int line) { if (line < 0 || line > mTotalLines - 1) { return; } int count = mCurItems.Count; for (int i = 0; i < mNumPerLine; i++) { int index = line * mNumPerLine + i; if (index < mItemDatas.Count) { int idx = count - 1 - i; ScrollItem item = mCurItems[idx]; recoverItem(item); mCurItems.RemoveAt(idx); } } }
private IEnumerator _updateAllItem(int startLine, int endLine, bool forceUpdate = false) { //修复异步加载图片可能失败的问题 yield return(null); if (mShowStart != startLine || mShowEnd != endLine || forceUpdate) { //只有首尾的行变动,只处理相应的行,只在一帧处理完 if (!forceUpdate && Math.Abs(mShowStart - startLine) <= 1 && Math.Abs(endLine - mShowEnd) <= 1) { //减少Item //如果开始的行数比当前的大1,证明现在在开头少显示了一行 if (startLine - mShowStart == 1) { _recoverStartLine(mShowStart); } //如果结束的行数比当前的小1,证明现在在末尾少显示了一行 if (mShowEnd - endLine == 1) { _recoverEndLine(mShowEnd); } ///添加新的Item //如果开始的行数比当前的小1,证明现在在开头多显示了一行 if (mShowStart - startLine == 1) { _addStartLine(startLine); } //如果结束的行数比当前的大1,证明现在在末尾多显示了一行 if (endLine - mShowEnd == 1) { _addEndLine(endLine); } mShowStart = startLine; mShowEnd = endLine; } else { //有多行变动,在协程中处理所有刷新,有新变动停止协程重新处理 //LogFile.Log("updateAllItem 滑动多行了,直接全部刷新 start:{0}, end{1}", startLine, endLine); //滑动多行了,直接全部刷新 int startIndex = Mathf.Clamp(startLine * mNumPerLine, 0, mItemDatas.Count - 1); int endIndex = Mathf.Clamp((endLine + 1) * mNumPerLine - 1, 0, mItemDatas.Count - 1); int count = Mathf.Clamp(endIndex - startIndex + 1, 0, mItemDatas.Count); //回收所有Item,在之后的协程中刷新 _recoverAll(); for (int i = 0; i < count; i++) { ScrollItem item = _getItem(); mCurItems.Add(item); _setItemDataByIndex(item, startIndex + i); if ((count + 1) % ItemNumPerStep == 0) { yield return(null); } } if (mTargetIndex != -1) { _tweenToIndex(mTargetIndex); mTargetIndex = -1; mTargetPos = -1f; } else if (!mTargetPos.Equals(-1f)) { _tweenToPos(mTargetPos); mTargetPos = -1f; } } mShowStart = startLine; mShowEnd = endLine; } if (null != mUpCoroutine) { StopCoroutine(mUpCoroutine); mUpCoroutine = null; } }
bool onPoolRecoverDelegate(ScrollItem obj) { obj.gameObject.SetActive(false); return(true); }
private IEnumerator updateAllItem(int startIndex, int endIndex, bool forceUpdate = false) { if (mShowStart != startIndex || mShowEnd != endIndex || forceUpdate) { //只有首尾的行变动,只处理相应的行,只在一帧处理完 if (Math.Abs(mShowStart - startIndex) <= 1 && Math.Abs(endIndex - mShowEnd) <= 1 && startIndex != endIndex && !forceUpdate) { //减少Item //如果开始的行数比当前的大1,证明现在在开头少显示了一行 if (startIndex - mShowStart == 1) { recoverStartItem(mShowStart); } //如果结束的行数比当前的小1,证明现在在末尾少显示了一行 if (mShowEnd - endIndex == 1) { recoverEndItem(mShowEnd); } ///添加新的Item //如果开始的行数比当前的小1,证明现在在开头多显示了一行 if (mShowStart - startIndex == 1) { addStartItem(startIndex); } //如果结束的行数比当前的大1,证明现在在末尾多显示了一行 if (endIndex - mShowEnd == 1) { addEndItem(endIndex); } mShowStart = startIndex; mShowEnd = endIndex; } else { //有多行变动,在协程中处理所有刷新,有新变动停止协程重新处理 //LogFile.Log("updateAllItem 滑动多行了,直接全部刷新 start:{0}, end{1}", startLine, endLine); //滑动多行了,直接全部刷新 mShowStart = startIndex; mShowEnd = endIndex; int startIdx = Mathf.Clamp(startIndex, 0, mData.Count - 1); int endIdx = Mathf.Clamp(endIndex, 0, mData.Count - 1); int count = endIdx - startIdx + 1; //回收所有Item,在之后的协程中刷新 recoverAll(); for (int i = 0; i < count; i++) { ScrollItem item = getItem(); mCurItems.Add(item); setItemDataByIndex(item, startIdx + i); if ((count + 1) % ItemNumPerStep == 0) { yield return(null); } } } if (mTargetIndex != -1) { tweenToIndex(mTargetIndex); mTargetIndex = -1; } } if (null != mUpCoroutine) { StopCoroutine(mUpCoroutine); mUpCoroutine = null; } }
private void recoverItem(ScrollItem item) { mItemPool.Recover(item); }
bool OnItemDisposeDelegate(ref ScrollItem obj) { Destroy(obj.gameObject); obj = null; return(true); }
bool OnItemRecoverDelegate(ScrollItem obj) { obj.gameObject.SetActive(false); obj.Index = mData.Count; return(true); }