canto/Assets/Scripts/UI/TestRecycleView.cs
2025-08-06 12:51:11 -04:00

57 lines
1.2 KiB
C#

using KitsuneCafe.System.Collections;
using UnityEngine;
using UnityEngine.UIElements;
namespace KitsuneCafe.UI
{
public class TestRecycleView : MonoBehaviour
{
[SerializeField]
private UIDocument doc;
private ListView listView;
private void OnValidate()
{
if (doc == null)
{
doc = GetComponent<UIDocument>();
}
}
private void Awake()
{
listView = doc.rootVisualElement.Q<ListView>();
listView.Q<ScrollView>().verticalScrollerVisibility = ScrollerVisibility.Hidden;
listView.makeItem = () => new Label();
listView.bindItem = (visualElement, index) =>
{
Label label = visualElement as Label;
label.text = listView.itemsSource[index].ToString();
};
}
private void Start()
{
var list = new CycleList<string>() { "one", "two", "three" };
listView.itemsSource = list;
listView.RefreshItems();
}
private void OnEnable()
{
listView.itemsSourceChanged += ScrollToMiddle;
}
private void OnDisable()
{
listView.itemsSourceChanged -= ScrollToMiddle;
}
private void ScrollToMiddle()
{
listView.ScrollToItem(listView.itemsSource.Count / 2);
}
}
}