ScrollRect滑动优化(二)--动态创建Item列表

首先是效果图:

ScrollRect滑动优化(二)--动态创建Item列表_第1张图片


单个item的

ScrollRect滑动优化(二)--动态创建Item列表_第2张图片


每个item的位置排序根据:

    public static Vector2 GetPosByIndex(int index)
    {  //宽度都为75,需要和Layout Element配合使用
        return new Vector2(0, -75 * index);
    }


通过资源载入的item,这个只是生成了GameObject,还咩有对其进行位置调整

    static GameObject _downloadItem;
    static GameObject downloadItem
    {
        get
        {
            if (_downloadItem == null)
                _downloadItem = GameObject.Instantiate(Resources.Load("offlineMapItem")) as GameObject;
            return _downloadItem;
        }
    }


对item限制高宽,位置的调整

    public static void SetMapDataItemView(MapDataItem item, GameObject root, Vector2 pos)
    {   
        RectTransform rt = GameObject.Instantiate(downloadItem, root.transform).transform as RectTransform;
        rt.anchoredPosition = pos;
        rt.offsetMax = new Vector2(0, rt.offsetMax.y);
        rt.offsetMin = new Vector2(0, rt.offsetMin.y);
        
        rt.localScale = Vector3.one;

        rt.GetComponent().Show(item);//进行每个item的数据绑定


    }

开始创建item

        public void Test() {
            int mainIndex = 0;
            foreach (var item in downloadedList)
            {
                MapDownloadHelper.SetMapDataItemView(item, mainRoot,GetPosByIndex(mainIndex));
                mainIndex++;
            }
        }

单个Item的数据类

public class OfflineMapItem : MonoBehaviour
{

    public Text mTitle;
    public Text mSize;

    public Button mDownloadButton;
    public Button mDeletedButton;

    public Slider mProgress;

    MapDataItem data;
    private bool isDownload = false;

    public void Show(MapDataItem item)
    {
        data = item;
        mTitle.text = data.getName();
        mSize.text = data.getSize();

        mDownloadButton.onClick.AddListener(OnDownloadClick);
        mDeletedButton.onClick.AddListener(OnDeletedClick);
  
    }


你可能感兴趣的:(Unity)