public void SetPositionViewHolder(IViewHolderInfo vh)
            {
                Vector2 size = GetRowSize();


                if (IsVerticalOrientation())
                {
                    if (recyclerView.IsReverse)
                    {
                        vh.RectTransform.localPosition = new Vector3(0, (vh.CurrentIndex * size.y), 0);
                    }
                    else
                    {
                        vh.RectTransform.localPosition = new Vector3(0, (-vh.CurrentIndex * size.y), 0);
                    }
                }
                else
                {
                    if (recyclerView.IsReverse)
                    {
                        vh.RectTransform.localPosition = new Vector3((-vh.CurrentIndex * size.x), 0, 0);
                    }
                    else
                    {
                        vh.RectTransform.localPosition = new Vector3((vh.CurrentIndex * size.x), 0, 0);
                    }
                }
            }
 private void ThrowToCache(IViewHolderInfo viewHolder)
 {
     viewHolder.Status = ViewHolder.Status.CACHE;
     Cache.Add(viewHolder);
     if (Cache.Count > CacheSize)
     {
         ThrowToPool(Cache[0]);
         Cache.RemoveAt(0);
     }
 }
        private void ThrowToPool(IViewHolderInfo vh)
        {
            vh.Status = ViewHolder.Status.RECYCLED;
            vh.ItemView.SetActive(false);
            IViewHolderInfo recycled = pool.Throw(vh);

            if (recycled != null)
            {
                recycled.Destroy();
            }
        }
        private IViewHolderInfo TryGetViewHolderForPosition(int position)
        {
            if (position >= 0 && position < GetItemCount())
            {
                for (int i = 0; i < AttachedScrap.Count; i++)
                {
                    if (AttachedScrap[i].CurrentIndex == position)
                    {
                        IViewHolderInfo v = AttachedScrap[i];
                        AttachedScrap.RemoveAt(i);
                        return(v);
                    }
                }

                for (int i = 0; i < Cache.Count; i++)
                {
                    if (Cache[i].CurrentIndex == position)
                    {
                        IViewHolderInfo v = Cache[i];
                        Cache.RemoveAt(i);
                        return(v);
                    }
                }

                IViewHolderInfo vhrecycled;
                vhrecycled = pool.GetFromPool();
                if (vhrecycled != null)
                {
                    vhrecycled.Status       = ViewHolder.Status.SCRAP;
                    vhrecycled.LastIndex    = vhrecycled.CurrentIndex;
                    vhrecycled.CurrentIndex = position;
                    layoutManager.AttachToGrid(vhrecycled, true);
                    OnBindViewHolder((T)Convert.ChangeType(vhrecycled, typeof(T)), vhrecycled.CurrentIndex);
                    return(vhrecycled);
                }


                IViewHolderInfo vh = (ViewHolder)Activator.CreateInstance(typeof(T), new object[] { OnCreateViewHolder() });
                vh.CurrentIndex = position;
                vh.LastIndex    = position;
                vh.Status       = ViewHolder.Status.SCRAP;
                layoutManager.AttachToGrid(vh, true);
                OnBindViewHolder((T)Convert.ChangeType(vh, typeof(T)), vh.CurrentIndex);
                return(vh);
            }
            else
            {
                return(null);
            }
        }
 public void AttachToGrid(IViewHolderInfo vh, bool up)
 {
     vh.ItemView.transform.SetParent(Grid.transform);
     if (up)
     {
         vh.ItemView.transform.SetAsLastSibling();
     }
     else
     {
         vh.ItemView.transform.SetAsFirstSibling();
     }
     vh.ItemView.name = vh.CurrentIndex.ToString();
     vh.ItemView.SetActive(true);
     SetPivot(vh.RectTransform);
     SetPositionViewHolder(vh);
 }
 public IViewHolderInfo Throw(IViewHolderInfo vh)
 {
     if (Scrap.Count < poolSize)
     {
         vh.Status = ViewHolder.Status.RECYCLED;
         Scrap.Enqueue(vh);
     }
     else
     {
         vh.Status = ViewHolder.Status.RECYCLED;
         IViewHolderInfo recycled = Scrap.Dequeue();
         Scrap.Enqueue(vh);
         return(recycled);
     }
     return(null);
 }
        protected void OnDataChange(int pos = 0)
        {
            layoutManager.IsCreating = true;

            if (pos < 0 || pos > GetItemCount())
            {
                return;
            }

            Clear();

            pool = new Pool(PoolSize, CacheSize);

            if (GetItemCount() > 0)
            {
                IViewHolderInfo vh = (T)Activator.CreateInstance(typeof(T), new object[] { OnCreateViewHolder() });
                vh.CurrentIndex = pos;
                vh.LastIndex    = pos;
                vh.Status       = ViewHolder.Status.SCRAP;
                AddToAttachedScrap(vh, true);
                layoutManager.SetPositionViewHolder(vh);
                OnBindViewHolder((T)Convert.ChangeType(vh, typeof(T)), pos);



                layoutManager.OnDataChange(vh.ItemView, pos);

                int ATTACHED_SCRAP_SIZE = layoutManager.GetScreenListSize() + 1;

                for (int i = pos + 1; i < ATTACHED_SCRAP_SIZE + pos; i++)
                {
                    if (i < GetItemCount())
                    {
                        IViewHolderInfo vh2 = (T)Activator.CreateInstance(typeof(T), new object[] { OnCreateViewHolder() });
                        vh2.CurrentIndex = i;
                        vh2.LastIndex    = i;
                        vh2.Status       = ViewHolder.Status.SCRAP;
                        AddToAttachedScrap(vh2, true);
                        layoutManager.SetPositionViewHolder(vh2);
                        OnBindViewHolder((T)Convert.ChangeType(vh2, typeof(T)), i);
                    }
                }
                layoutManager.ClampList();
            }

            layoutManager.IsCreating = false;
        }
        private void UpdateScrap()
        {
            int firstPosition = layoutManager.GetFirstPosition();
            List <IViewHolderInfo> TmpScrap = new List <IViewHolderInfo>();

            for (int i = firstPosition - 1; i < firstPosition + layoutManager.GetScreenListSize() + 1; i++)
            {
                IViewHolderInfo vh = TryGetViewHolderForPosition(i);
                if (vh != null)
                {
                    TmpScrap.Add(vh);
                }
            }

            ThrowAttachedScrapToCache();
            AttachedScrap.Clear();
            AttachedScrap.AddRange(TmpScrap);

            if (DEBUG)
            {
                Debug.Log(ToString());
            }
        }
 private void AddToAttachedScrap(IViewHolderInfo vh, bool up)
 {
     layoutManager.AttachToGrid(vh, up);
     vh.ItemView.SetActive(true);
     AttachedScrap.Add(vh);
 }