/// <summary>
        /// 绑定列表控件和数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1"></typeparam>
        /// <param name="list"></param>
        public void ObserverListAll <T, T1>(UniRx.IReactiveCollection <T1> list, bool autoWidth = false) where T : UIBase where T1 : IViewModel
        {
            System.Type type = typeof(T);
            bool        isImpleIViewModelCtrl = typeof(T1).IsSubclassOf(typeof(IViewModelCtrl));
            var         g          = gObject;
            var         u          = uiBase;
            var         observeAdd = list.ObserveAdd();
            var         subAdd     = observeAdd.Subscribe((o) =>
            {
                if (isImpleIViewModelCtrl)
                {
                    type = (o.Value as IViewModelCtrl).CtrlType;
                }
                var ctrl = System.Activator.CreateInstance(type, g, o.Value) as T;
                ctrl.SetMainViewModel(o.Value as IViewModel);
                ctrl.CreateUIInstance(false);
                //g.AddSelection(g.numItems, true);
                u.TempKv[o.Value] = ctrl;
                if (autoWidth)
                {
                    adjustWidth(ctrl, g);
                }
                ctrl.AddToPanel(u);
            });
            var it = list.GetEnumerator();

            while (it.MoveNext())
            {
                if (isImpleIViewModelCtrl)
                {
                    type = (it.Current as IViewModelCtrl).CtrlType;
                }

                var ctrl = System.Activator.CreateInstance(type, g, it.Current) as T;
                ctrl.SetMainViewModel(it.Current as IViewModel);
                ctrl.CreateUIInstance(false);
                //g.AddSelection(g.numItems, true);
                u.TempKv[it.Current] = ctrl;
                if (autoWidth)
                {
                    adjustWidth(ctrl, g);
                }
                ctrl.AddToPanel(uiBase);
            }

            uiBase.AddDisposable(subAdd);


            var subRemove = list.ObserveRemove().Subscribe((o) =>
            {
                var item = g.GetChildAt(o.Index);
                g.RemoveChildAt(o.Index, true);
                //item.RemoveFromParent();
                //item.Dispose();
                if (u.TempKv.ContainsKey(o.Value))
                {
                    var uiBase1 = u.TempKv[o.Value] as UIBase;
                    uiBase1.Close();
                    u.TempKv.Remove(o.Value);
                    if (autoWidth)
                    {
                        adjustWidth(uiBase1, g);
                    }
                }
            });

            uiBase.AddDisposable(subRemove);

            //TODO Maybe bug  try:g.container.GetChildAt
            var subMove = list.ObserveMove().Subscribe((o) =>
            {
                var child = g.GetChildAt(o.OldIndex);
                g.SetChildIndex(child, o.NewIndex);
                //child.RemoveFromParent();
                //if (o.NewIndex <= o.OldIndex)
                //{
                //    g.SetChildIndex(child, o.NewIndex);
                //}
                //else
                //{
                //    g.SetChildIndex(child, o.NewIndex + 1);
                //}
            });

            uiBase.AddDisposable(subMove);
        }
Exemple #2
0
        //public void ObserverAdd<T, T1>(UniRx.IReadOnlyReactiveCollection<T1> list) where T : UIBase
        //{
        //    var g = gObject;
        //    var sub = list.ObserveAdd().Subscribe((add) =>
        //    {
        //        var item = System.Activator.CreateInstance(typeof(T), add.Value, g) as UIBase;

        //    });
        //    uiBase.AddDisposable(sub);

        //}

        /// <summary>
        /// 绑定列表控件和数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1"></typeparam>
        /// <param name="list"></param>
        public void ObserverListAll <T, T1>(UniRx.IReactiveCollection <T1> list, bool autoWidth = false) where T : UIBase where T1 : IViewModel
        {
            System.Type type = typeof(T);
            bool        isImpleIViewModelCtrl = typeof(T1).IsSubclassOf(typeof(IViewModelCtrl));
            var         g          = gObject;
            var         u          = uiBase;
            var         observeAdd = list.ObserveAdd();
            var         subAdd     = observeAdd.Subscribe((o) =>
            {
                if (isImpleIViewModelCtrl)
                {
                    type = (o.Value as IViewModelCtrl).CtrlType;
                }
                var ctrl = System.Activator.CreateInstance(type, g, o.Value) as  T;
                ctrl.SetMainViewModel(o.Value as IViewModel);
                ctrl.CreateUIInstance(false);
                //g.AddSelection(g.numItems, true);
                u.TempKv[o.Value] = ctrl;
                if (autoWidth)
                {
                    adjustWidth(ctrl, g);
                }
                ctrl.AddToPanel(u);
            });
            var it = list.GetEnumerator();

            while (it.MoveNext())
            {
                if (isImpleIViewModelCtrl)
                {
                    type = (it.Current as IViewModelCtrl).CtrlType;
                }

                var ctrl = System.Activator.CreateInstance(type, g, it.Current) as T;
                ctrl.SetMainViewModel(it.Current as IViewModel);
                ctrl.CreateUIInstance(false);
                //g.AddSelection(g.numItems, true);
                u.TempKv[it.Current] = ctrl;
                if (autoWidth)
                {
                    adjustWidth(ctrl, g);
                }
                ctrl.AddToPanel(uiBase);
            }

            uiBase.AddDisposable(subAdd);


            var subRemove = list.ObserveRemove().Subscribe((o) =>
            {
                Debug.Log("$$ will remove item...");
                var item = g.GetChildAt(o.Index);
#if _CREATE_LISTITEM_FROMPOOL_
                g.RemoveChildToPoolAt(o.Index);
#else
                g.RemoveChildAt(o.Index, true);
                //item.RemoveFromParent();
                //item.Dispose();
                if (u.TempKv.ContainsKey(o.Value))
                {
                    var uiBase1 = u.TempKv[o.Value] as UIBase;
                    uiBase1.Close();
                    u.TempKv.Remove(o.Value);
                    if (autoWidth)
                    {
                        adjustWidth(uiBase1, g);
                    }
                }
#endif
            });

            uiBase.AddDisposable(subRemove);

            //TODO Maybe bug  try:g.container.GetChildAt
            var subMove = list.ObserveMove().Subscribe((o) =>
            {
                //var child = g.GetChildAt(o.OldIndex);
                //g.SetChildIndex(child, o.NewIndex);
                g.SwapChildrenAt(o.OldIndex, o.NewIndex);
            });

            var subReset = list.ObserveReset().Subscribe((unit) =>
            {
#if _CREATE_LISTITEM_FROMPOOL_
                g.RemoveChildrenToPool();
#else
                g.RemoveChildren();
                foreach (var o in u.TempKv)
                {
                    if (o.Key is T1 && list.Contains((T1)o.Key))
                    {
                        var uiBase1 = o.Value as UIBase;
                        if (uiBase1 != null)
                        {
                            uiBase1.Close();
                        }
                    }
                }
                //u.TempKv.Clear();
#endif
            });

            uiBase.AddDisposable(subMove);
        }