Unity ScrollView列表自适应(代码)

列表长度刷新+内容刷新+非常简单的对象缓存 

/***********************************************
Copyright (C) 2018 The Company Name
File Name:           ServerViewListPanel.cs
Author:              #AuthorName
CreateTime:          #CreateTime
User:                列表
***********************************************/

using Newtonsoft.Json;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ServerViewListPanel : MonoBehaviour
{
    public GameObject ItemPerfab;

    public RectTransform content;

    private List<ServerViewListItem> itemList = new List<ServerViewListItem>();

    private List<ServerViewListItem> hideItemList = new List<ServerViewListItem>();

    private MaintenancePageListData serverData;

    private float originalHeight, itemHeight, spacing, nowcontentHeight;

    void Awake()
    {
        originalHeight = content.rect.height;

        itemHeight = ItemPerfab.GetComponent<RectTransform>().rect.height;

        spacing = content.GetComponent<VerticalLayoutGroup>().spacing;
    }

    void OnEnable()
    {
        GetServerViewListData();
    }

    /// <summary>
    /// 获取服务器数据
    /// </summary>
    private void GetServerViewListData()
    {
        RefreshViewList();
    }

    /// <summary>
    /// 刷新列表
    /// </summary>
    private void RefreshViewList(string receiveStr)
    {
        serverData = JsonConvert.DeserializeObject<MaintenancePageListData>(receiveStr);

        if (serverData.data.resultList.Count != itemList.Count)
        {
            if (serverData.data.resultList.Count > itemList.Count)
            {
                for (int i = itemList.Count; i < serverData.data.resultList.Count; i++)
                {
                    CreateItem();
                }
            }
            else
            {
                for (int i = itemList.Count - 1; i >= serverData.data.resultList.Count; i--)
                {
                    HideItem(i);
                }
            }
        }

        for (int i = 0; i < itemList.Count; i++)
        {
            itemList[i].gameObject.SetActive(true);

            itemList[i].InitParamaters(serverData.data.resultList[i]);
        }

        ResizeContentSize();
    }

    /// <summary>
    /// 补充元素
    /// </summary>
    private void CreateItem()
    {
        if (hideItemList.Count != 0)
        {
            ServerViewListItem item = hideItemList[0];
            itemList.Add(item);
            hideItemList.RemoveAt(0);
        }
        else
        {
            ServerViewListItem item = Instantiate(ItemPerfab, content).GetComponent<ServerViewListItem>();
            itemList.Add(item);
        }
    }

    /// <summary>
    /// 删除多余元素
    /// </summary>
    /// <param name="index"></param>
    private void HideItem(int index)
    {
        hideItemList.Add(itemList[index]);
        itemList[index].gameObject.SetActive(false);
        itemList.RemoveAt(index);
    }

    /// <summary>
    /// 调整滑动框大小
    /// </summary>
    private void ResizeContentSize()
    {
        nowcontentHeight = (itemHeight + spacing) * itemList.Count - spacing;

        content.sizeDelta = new Vector2(content.sizeDelta.x, nowcontentHeight > originalHeight ? nowcontentHeight : originalHeight);
    }

    void OnDestory()
    {
        hideItemList.Clear();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值