//public float GetItemPos(int itemIndex) //{ // Update(true); // int groupIndex = itemIndex / mItemMaxCountPerGroup; // int indexInGroup = itemIndex % mItemMaxCountPerGroup; // return mItemSizeGroupList[groupIndex].GetItemStartPos(indexInGroup); //} public void GetItemIndexAndPosAtGivenPos(float pos, ref int index, ref float itemPos) { Update(true); index = 0; itemPos = 0f; int count = mItemSizeGroupList.Count; if (count == 0) { return; } ItemSizeGroup hitGroup = null; int low = 0; int high = count - 1; while (low <= high) { int mid = (low + high) / 2; ItemSizeGroup tGroup = mItemSizeGroupList[mid]; if (tGroup.mGroupStartPos <= pos && tGroup.mGroupEndPos >= pos) { hitGroup = tGroup; break; } else if (pos > tGroup.mGroupEndPos) { low = mid + 1; } else { high = mid - 1; } } int hitIndex = -1; if (hitGroup != null) { hitIndex = hitGroup.GetItemIndexByPos(pos - hitGroup.mGroupStartPos); } else { return; } if (hitIndex < 0) { return; } index = hitIndex + hitGroup.mGroupIndex * mItemMaxCountPerGroup; itemPos = hitGroup.GetItemStartPos(hitIndex); }
public void SetItemSize(int itemIndex, float size) { int groupIndex = itemIndex / mItemMaxCountPerGroup; int indexInGroup = itemIndex % mItemMaxCountPerGroup; ItemSizeGroup tGroup = mItemSizeGroupList[groupIndex]; float changeSize = tGroup.SetItemSize(indexInGroup, size); if (changeSize != 0f) { if (groupIndex < mDirtyBeginIndex) { mDirtyBeginIndex = groupIndex; } } mTotalSize += changeSize; }
public void SetItemMaxCount(int maxCount) { mDirtyBeginIndex = 0; mTotalSize = 0; int st = maxCount % mItemMaxCountPerGroup; int lastGroupItemCount = st; int needMaxGroupCount = maxCount / mItemMaxCountPerGroup; if (st > 0) { needMaxGroupCount++; } else { lastGroupItemCount = mItemMaxCountPerGroup; } int count = mItemSizeGroupList.Count; if (count > needMaxGroupCount) { int d = count - needMaxGroupCount; mItemSizeGroupList.RemoveRange(needMaxGroupCount, d); } else if (count < needMaxGroupCount) { int d = needMaxGroupCount - count; for (int i = 0; i < d; ++i) { ItemSizeGroup tGroup = new ItemSizeGroup(count + i, mItemDefaultSize); mItemSizeGroupList.Add(tGroup); } } count = mItemSizeGroupList.Count; if (count == 0) { return; } for (int i = 0; i < count - 1; ++i) { mItemSizeGroupList[i].SetItemCount(mItemMaxCountPerGroup); } mItemSizeGroupList[count - 1].SetItemCount(lastGroupItemCount); for (int i = 0; i < count; ++i) { mTotalSize = mTotalSize + mItemSizeGroupList[i].mGroupSize; } }
/// <summary> /// Update /// </summary> /// <param name="updateAll"></param> public void Update(bool updateAll) { int count = mItemSizeGroupList.Count; if (count == 0) { return; } if (mDirtyBeginIndex >= count) { return; } int loopCount = 0; for (int i = mDirtyBeginIndex; i < count; ++i) { loopCount++; ItemSizeGroup tGroup = mItemSizeGroupList[i]; mDirtyBeginIndex++; tGroup.UpdateAllItemStartPos(); if (i == 0) { tGroup.mGroupStartPos = 0; tGroup.mGroupEndPos = tGroup.mGroupSize; } else { tGroup.mGroupStartPos = mItemSizeGroupList[i - 1].mGroupEndPos; tGroup.mGroupEndPos = tGroup.mGroupStartPos + tGroup.mGroupSize; } if (!updateAll && loopCount > 1) { return; } } }