Exemplo n.º 1
0
        private void addEndItem(int index)
        {
            ScrollItem item = getItem();

            mCurItems.Add(item);
            setItemDataByIndex(item, index);
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 4
0
        private void setItemDataByIndex(ScrollItem item, int index)
        {
            UIItemData data = mData[index];

            item.Index = index;
            item.SetData(data);
            item.transform.localPosition = getItemPosByIndex(index);
        }
Exemplo n.º 5
0
 private void recoverAll()
 {
     for (int i = mCurItems.Count - 1; i > -1; --i)
     {
         ScrollItem item = mCurItems[i];
         recoverItem(item);
         mCurItems.RemoveAt(i);
     }
 }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
 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);
         }
     }
 }
Exemplo n.º 8
0
 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();
 }
Exemplo n.º 9
0
 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);
         }
     }
 }
Exemplo n.º 10
0
 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);
     }
 }
Exemplo n.º 11
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);
 }
Exemplo n.º 12
0
 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);
     }
 }
Exemplo n.º 13
0
 //进入时刷新部分参数
 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>();
     }
 }
Exemplo n.º 14
0
 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);
 }
Exemplo n.º 15
0
 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;
             }
         }
     }
 }
Exemplo n.º 16
0
        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;
        }
Exemplo n.º 17
0
 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);
         }
     }
 }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
0
        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);
            }
        }
Exemplo n.º 20
0
 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);
 }
Exemplo n.º 21
0
        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);
                }
            }
        }
Exemplo n.º 22
0
        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;
            }
        }
Exemplo n.º 23
0
 bool onPoolRecoverDelegate(ScrollItem obj)
 {
     obj.gameObject.SetActive(false);
     return(true);
 }
Exemplo n.º 24
0
        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;
            }
        }
Exemplo n.º 25
0
 private void recoverItem(ScrollItem item)
 {
     mItemPool.Recover(item);
 }
Exemplo n.º 26
0
 bool OnItemDisposeDelegate(ref ScrollItem obj)
 {
     Destroy(obj.gameObject);
     obj = null;
     return(true);
 }
Exemplo n.º 27
0
 bool OnItemRecoverDelegate(ScrollItem obj)
 {
     obj.gameObject.SetActive(false);
     obj.Index = mData.Count;
     return(true);
 }