简书链接:unity的SuperScrollView集成实现超级列表框表格案例踩坑详解
文章字数:999,阅读全文大约需要3分钟
SuperScrollView 实现列表框
市场价格50美元,不便宜,我从网上找到了免费版本。
https://download.csdn.net/download/u010042660/86247323

http://wjhsh.net/big-zhou-p-11266840.html
https://blog.csdn.net/qq_40120946/article/details/101704791

本教程是基于csdn作者教程补充的,里面有一些细节我整好了,有几个地方不太对劲。不知道咋回事
导入进去里面的demo就可以打开食用。
item预制体实现
在画布下面添加空对象,然后添加Horizontal layout group,
添加 UI Student Info脚本,添加Loop List item2脚本
添加Image组件(用于选中的变色)

然后添加子节点, text , id ,name ,age ,grade,
image.png

image.png

做好了预制体之后,在画布下面添加scrollview

再给scrollview添加 Loop List view2
然后 展开给这个脚本挂载一个预制体 ,这里为MyItem,在接下来的代码会从缓存池里面取,也应该保持一致。
image.png

依次创建源代码
image.png

StudentInfo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class StudentInfo
{
public String id { get; set; }
public String name { get; set; }
public String grade { get; set; }
public String score { get; set; }
public String age { get; set; }

}


StudentInfoMgr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class StudentInfoMgr
{

List<StudentInfo> mItemDataList = new List<StudentInfo>();

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

set
{
mItemDataList = value;
}
}

/// <summary>
/// 查找指定数据
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public StudentInfo FindInfoByIndex(int index)
{
if (index < 0 || index >= mItemDataList.Count)
{
return null;
}
return mItemDataList[index];
}
/// <summary>
/// 查找指定数据
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 添加一组数据
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
public void AddOrUpdateInfo(StudentInfo item)
{
var student = FindInfoById(item.id);
if (student == null)
{
mItemDataList.Add(item);
}
else
{
student = item;
}
}
/// <summary>
/// 添加多组数据
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
public void AddOrUpdateInfo(List<StudentInfo> 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;
}
}
}

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


SuperListBoxInit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using SuperScrollView;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SuperListBoxInit : MonoBehaviour
{
public SuperScrollView.LoopListView2 mLoopListView;
public RectTransform Content;

private List<StudentInfo> studentInfos = new List<StudentInfo>();
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("MyItem");
var itemInfo = item.GetComponent<UStudentInfo>();
itemInfo.Init(info, index);
itemInfo.OnClick += OnClick;
itemInfo.OnEnter += OnEnter;
itemInfo.OnExit += OnExit;
return item;
}

private void OnClick(UStudentInfo item)
{
Debug.Log("点击!!");

}
private void OnEnter(UStudentInfo item)
{
Debug.Log("移入!!");
item.GetComponent<Image>().color = Color.clear;
}
private void OnExit(UStudentInfo item)
{
Debug.Log("移出!!");
item.GetComponent<Image>().color = new Color(1, 1, 1, 130 / 255f);
}
}


UStudentInfo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UStudentInfo : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
/// <summary>
/// 实现点击事件
/// </summary>
public Action<UStudentInfo> OnClick;
/// <summary>
/// 实现点击事件
/// </summary>
public Action<UStudentInfo> OnEnter;
/// <summary>
/// 实现点击事件
/// </summary>
public Action<UStudentInfo> 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()
{
this.gameObject.transform.GetChild(0).GetComponent<Text>().text = info.id;
this.gameObject.transform.GetChild(1).GetComponent<Text>().text = info.name;
this.gameObject.transform.GetChild(2).GetComponent<Text>().text = info.age;
this.gameObject.transform.GetChild(3).GetComponent<Text>().text = info.grade;
this.gameObject.transform.GetChild(4).GetComponent<Text>().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);
}
}

其中UStudentInfo挂载给预制体 item ,
SuperListBoxInit是用来初始化的,可以挂载给相机

image.png
这个脚本 的listview 就传递 scrollview,
content 则把scrllview的content拖动进去。

另外标题列 也是用预制体做的,不过把脚本解除了。

image.png
上图还是有一点点没对齐,这可能是因为滑块的空间导致的问题。

其实主要问题还是 界面的操作问题,我这里再弄详细一点的截图
初始化代码挂载
image.png
初始化代码中的参数拖拽
image.png

预制体挂载
image.png
scrllview挂载
image.png