Example #1
0
    /// <summary>
    /// 初始化界面与数据
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    /// <param name="datas"></param>
    public override void InitData <TData>(IEnumerable <TData> datas)
    {
        if (scrollView != null)
        {
            if (itemPrefab != null)
            {
                scrollRect = scrollView.GetComponent <ScrollRect>();
                DemoManager.Instance.SendTerminalMessage("RecyclerViewType:" + recyclerViewType);
                switch (recyclerViewType)
                {
                case RecyclerViewType.Vertical:
                    scrollRect.horizontal = false;
                    scrollRect.vertical   = true;
                    VerticalLayoutSettings layoutSetting = new VerticalLayoutSettings();
                    list = new VerticalScrollList(scrollRect, itemPrefab, layoutSetting);
                    break;

                case RecyclerViewType.Horizontal:
                    scrollRect.horizontal = true;
                    scrollRect.vertical   = false;
                    HorizontalLayoutSettings horizontalSetting = new HorizontalLayoutSettings();
                    list = new HorizontalScrollList(scrollRect, itemPrefab, horizontalSetting);
                    break;

                case RecyclerViewType.Grid:
                    scrollRect.horizontal = true;
                    scrollRect.vertical   = true;
                    GridLayoutSettings layout = new GridLayoutSettings();
                    list = new GridScrollList(scrollRect, itemPrefab, layout);
                    break;
                }

                list.onRenderItem     += OnItemRender;
                list.onRebuildContent += OnRebuildContent;
                list.onRefresh        += OnListRefresh;
                SetData(datas);
                Debug.Log("recyclerViewType: " + recyclerViewType.ToString() + "datas.count:" + datas.Count());
            }
            else
            {
                Debug.LogError("请先设置 TamRecyclerView 的 itemPrefab !");
            }
        }
        else
        {
            Debug.LogError("请先设置 TamRecyclerView 的 ScrollView !");
        }
    }
    protected override void InitItems()
    {
        var datas = new int[itemCount];

        for (int i = 0; i < datas.Length; i++)
        {
            datas[i] = i;
        }

        GameObject itemPrefab = transform.Find("Scroll View/ListItem").gameObject;

        var ls = new HorizontalLayoutSettings();

        ls.gap                 = 10;
        ls.paddingRight        = 500;
        list                   = new HorizontalScrollList(scrollView.GetComponent <ScrollRect>(), itemPrefab, ls);
        list.onRenderItem     += OnItemRender;
        list.onRebuildContent += OnRebuildContent;
        list.onRefresh        += OnListRefresh;
        list.AddRange(datas);
    }