这篇文章主要为大家详细介绍了Unity实现首字母检索器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Unity实现首字母检索器的具体代码,供大家参考,具体内容如下
需要实现一个类似 “城市选择器”的功能 网上基本都是用原生或者前端来实现功能 其他大概的思路都差不多 这里提供一个Unity 实现的思路
先看一下效果
这里用到了 SuperScrollView 这个插件 来实现功能
ListText.cs // 核心控制类 代码写的比较随意 大概的功能已经实现,需要根据实际需要进行优化。
using SuperScrollView;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ListText : MonoBehaviour
{
public LoopListView2 mLoopListView;
public Text Select_Text;
public RectTransform Right_Content;
public GameObject Tag_Prefab;
public string[] Tags = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#" };
public int TreeViewItemCount
{
get
{
return mItemDataList.Count;
}
}
// Start is called before the first frame update
void Start()
{
Init();
Keys.Clear();
for (int i = 0; i < Tags.Length; ++i)
{
CityData cityData = new CityData();
cityData.Key = Tags[i];
List<string> value = new List<string>();
value.Add("Item" + Tags[i] + Random.Range(0, 6).ToString());
cityData.Value = value;
Keys.Add(cityData);
}
DoRefreshDataSource();
int count = TreeViewItemCount;
//tells mTreeItemCountMgr there are how many TreeItems and every TreeItem has how many ChildItems.
for (int i = 0; i < count; ++i)
{
int childCount = GetItemDataByIndex(i).ChildCount;
//second param "true" tells mTreeItemCountMgr this TreeItem is in expand status, that is to say all its children are showing.
AddTreeItem(childCount, true);
}
mLoopListView.InitListView(GetTotalItemAndChildCount(), OnGetItemByIndex);
}
public void AddTreeItem(int count, bool isExpand)
{
KeyData data = new KeyData();
data.mTreeItemIndex = mTreeItemDataList.Count;
data.mChildCount = count;
data.mIsExpand = isExpand;
mTreeItemDataList.Add(data);
mIsDirty = true;
}
public int GetTotalItemAndChildCount()
{
int count = mTreeItemDataList.Count;
if (count == 0)
{
return 0;
}
UpdateAllTreeItemDataIndex();
return mTreeItemDataList[count - 1].mEndIndex + 1;
}
LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
{
if (index < 0)
{
return null;
}
KeyData countData = QueryTreeItemByTotalIndex(index);
if (countData == null)
{
return null;
}
int treeItemIndex = countData.mTreeItemIndex;
ValueData treeViewItemData = GetItemDataByIndex(treeItemIndex);
if (countData.IsChild(index) == false)// if is a TreeItem
{
//get a new TreeItem
LoopListViewItem2 item = listView.NewListViewItem("KeyItem");
KeyItem itemScript = item.GetComponent<KeyItem>();
if (item.IsInitHandlerCalled == false)
{
item.IsInitHandlerCalled = true;
itemScript.Init();
//itemScript.SetClickCallBack(this.OnExpandClicked);
}
//update the TreeItem's content
itemScript.mText.text = treeViewItemData.mName;
itemScript.SetItemData(treeItemIndex, countData.mIsExpand);
return item;
}
else // if is a TreeChildItem
{
//childIndex is from 0 to ChildCount.
//for example, TreeChildItem0_0 is the 0'th child of TreeItem0
//and TreeChildItem1_2 is the 2'th child of TreeItem1
int childIndex = countData.GetChildIndex(index);
ItemData itemData = treeViewItemData.GetChild(childIndex);
if (itemData == null)
{
return null;
}
//get a new TreeChildItem
LoopListViewItem2 item = listView.NewListViewItem("ValueItem");
ValueItem itemScript = item.GetComponent<ValueItem>();
if (item.IsInitHandlerCalled == false)
{
item.IsInitHandlerCalled = true;
itemScript.Init();
}
//update the TreeChildItem's content
itemScript.SetItemData(itemData, treeItemIndex, childIndex);
return item;
}
}
List<ValueData> mItemDataList = new List<ValueData>();
int mTreeViewItemCount = 20;
int mTreeViewChildItemCount = 30;
// List<string, List<string>> keys = new List<string, List<string>>();
ArrayList Keys = new ArrayList();
void DoRefreshDataSource()
{
mItemDataList.Clear();
for (int i = 0; i < Keys.Count; i++)
{
ValueData tData = new ValueData();
CityData city = Keys[i] as CityData;
tData.mName = "" + city.Key;
mItemDataList.Add(tData);
// int childCount = Random.Range(0, 6);
for (int j = 0; j < city.Value.Count; j++)
{
ItemData childItemData = new ItemData();
childItemData.mName = "Item" + city.Value[j] + ":Child" + j;
childItemData.mDesc = "Item Desc For " + childItemData.mName;
childItemData.mStarCount = Random.Range(0, 6);
childItemData.mFileSize = Random.Range(20, 999);
tData.AddChild(childItemData);
}
}
//for (int i = 0; i < keys.Count; ++i)
//{
// ValueData tData = new ValueData();
// tData.mName = "" + keys[]
// mItemDataList.Add(tData);
// int childCount = Random.Range(0, 6);
/