Unity 超级列表SuperScrollview使用Demo

首先,说一下使用SuperScrollview的基本配置:

创建一个scroll view,然后把横纵滑动条设置可配置的,然后再加上Loop List View2代码,如下:
Unity 超级列表SuperScrollview使用Demo_第1张图片

然后我们创建四个代码类:

1、数据类:

Unity 超级列表SuperScrollview使用Demo_第2张图片

2、数据管理类方便增删查改:

public class StudentInfoMgr
{

    List mItemDataList = new List();

    public StudentInfoMgr() { }
    public StudentInfoMgr(List students)
    {
        mItemDataList.Clear();
        mItemDataList = students;
    }
    /// 
    /// 获取数据总数量
    /// 
    public int TotalItemCount
    {
        get
        {
            return mItemDataList.Count;
        }
    }
    /// 
    /// data数据
    /// 
    public List MItemDataList
    {
        get
        {
            return mItemDataList;
        }

        set
        {
            mItemDataList = value;
        }
    }

    /// 
    /// 查找指定数据
    /// 
    /// 
    /// 
    public StudentInfo FindInfoByIndex(int index)
    {
        if (index < 0 || index >= mItemDataList.Count)
        {
            return null;
        }
        return mItemDataList[index];
    }
    /// 
    /// 查找指定数据
    /// 
    /// 
    /// 
    public StudentInfo FindInfoById(string itemId)
    {
        int count = mItemDataList.Count;
        for (int i = 0; i < count; ++i)
        {
            if (mItemDataList[i].id == itemId)
            {
                return mItemDataList[i];
            }
        }
        return null;
    }
    /// 
    /// 添加一组数据
    /// 
    /// 
    /// 
    public void AddOrUpdateInfo(StudentInfo item)
    {
        var student = FindInfoById(item.id);
        if (student == null)
        {
            mItemDataList.Add(item);
        }
        else
        {
            student = item;
        }
    }
    /// 
    /// 添加多组数据
    /// 
    /// 
    /// 
    public void AddOrUpdateInfo(List items)
    {
        if (items.Count<= 0) return;
        foreach (var info in items)
        {
            var student = FindInfoById(info.id);
            if (student == null)
            {
                mItemDataList.Add(info);
            }
            else
            {
                student = info;
            }
        }
    }

    /// 
    ///删除一组数据
    /// 
    /// 
    /// 
    public void RemoveInfo(StudentInfo item)
    {
        var student = FindInfoById(item.id);
        if (student == null) return;
        mItemDataList.Remove(student);
    }
    /// 
    ///删除一组数据
    /// 
    /// 
    /// 
    public void RemoveInfo(string  id)
    {
        var student = FindInfoById(id);
        if (student == null) return;
        mItemDataList.Remove(student);
    }
    /// 
    ///删除所有数据
    /// 
    /// 
    /// 
    public void RemoveInfos()
    {
        if (mItemDataList.Count<=0) return;
        mItemDataList.Clear();
    }
}

3、单条数据显示类:

public class ItemStudentInfo : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
    /// 
    /// 实现点击事件
    /// 
    public Action OnClick;
    /// 
    /// 实现点击事件
    /// 
    public Action OnEnter;
    /// 
    /// 实现点击事件
    /// 
    public Action OnExit;

    private StudentInfo info;
    private int index;
    [SerializeField]
    private RectTransform item;

    public void Init(StudentInfo info,int index)
    {
        this.info = info;
        this.index = index;
        InitData();
    }
    private void InitData()
    {
        item.GetChild(0).GetComponent().text = info.id;
        item.GetChild(1).GetComponent().text = info.name;
        item.GetChild(2).GetComponent().text = info.age;
        item.GetChild(3).GetComponent().text = info.grade;
        item.GetChild(4).GetComponent().text = info.score;
    }

    void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
    {
       if(this.gameObject!=null) OnClick(this);
    }

    void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
    {
        if (this.gameObject != null) OnEnter(this);
    }

    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
    {
        if (this.gameObject != null) OnExit(this);
    }
}

4、整体UI显示类:

public class UI_StudentInfo : MonoBehaviour {
    public LoopListView2 mLoopListView;
    public RectTransform Content;
    
    private List studentInfos = new List();
    private StudentInfoMgr infoMgr;
    // Use this for initialization
    void Start () {
        for (int i=0;i<100;i++)
        {
            StudentInfo student = new StudentInfo
            {
                id = (i + 100).ToString(),
                name = "zhang" + i,
                age = (i + 1).ToString(),
                grade = "十一班",
                score = i.ToString(),
            };
            studentInfos.Add(student);
        }
        InitListView();

    }

    private void InitListView()
    {
        infoMgr = new StudentInfoMgr(studentInfos);
        Debug.Log(infoMgr.TotalItemCount);
        mLoopListView.InitListView(infoMgr.TotalItemCount, OmGetItemByIndex);
        mLoopListView.MovePanelToItemIndex(infoMgr.TotalItemCount, 0);
    }
    private LoopListViewItem2 OmGetItemByIndex(LoopListView2 loopView, int index)
    {
        if (index < 0 || index > infoMgr.TotalItemCount)
        {
            return null;
        }
        var info = infoMgr.FindInfoByIndex(index);
        if (info == null)
        {
            return null;
        }
        /*2018.11以后MonoBehaviour不能  new  直接赋值就可*/
       // LoopListViewItem2 item = new LoopListViewItem2();
       var item = loopView.NewListViewItem("Item_SuperPrefab");
        var itemInfo = item.GetComponent();
        itemInfo.Init(info,index);

        itemInfo.OnClick += OnClick;
        itemInfo.OnEnter += OnEnter;
        itemInfo.OnExit += OnExit;
        return item;
    }

    private void  OnClick(ItemStudentInfo item)
    {
        Debug.Log("点击!!");
    }
    private void OnEnter(ItemStudentInfo item)
    {
        Debug.Log("移入!!");
        item.GetComponent().color = Color.clear;
    }
    private void OnExit(ItemStudentInfo item)
    {
        Debug.Log("移出!!");
        item.GetComponent().color =new Color(1,1,1,130/255f);
    }
}

这里基本成了,最核心的代码:

Unity 超级列表SuperScrollview使用Demo_第3张图片

基本原理就是利用对象池,做重复利用。

注意:初始化函数LoopListViewItem2.InitListView(lineLossMgr.TotalCount, OmGetItemByIndex)只能调用一次,如果想重置数据,只需要改变数据管理类中的数据(List ),然后LoopListViewItem2中的   loopListView2.RefreshAllShownItem();刷新一下即可。

显示效果如下:

Unity 超级列表SuperScrollview使用Demo_第4张图片

 

你可能感兴趣的:(Unity,unity,超级列表,superScrollview)