using System.Collections.Generic; using UnityEngine; using PolyAndCode.UI; /// /// Demo controller class for Recyclable Scroll Rect. /// A controller class is responsible for providing the scroll rect with datasource. Any class can be a controller class. /// The only requirement is to inherit from IRecyclableScrollRectDataSource and implement the interface methods /// //Dummy Data model for demostraion public struct ContactInfo { public string Name; public string Gender; public string id; } public class RecyclableScrollerDemo : MonoBehaviour, IRecyclableScrollRectDataSource { [SerializeField] RecyclableScrollRect _recyclableScrollRect; [SerializeField] private int _dataLength; //Dummy data List private List _contactList = new List(); //Recyclable scroll rect's data source must be assigned in Awake. private void Awake() { InitData(); _recyclableScrollRect.DataSource = this; } //Initialising _contactList with dummy data private void InitData() { if (_contactList != null) _contactList.Clear(); string[] genders = { "Male", "Female" }; for (int i = 0; i < _dataLength; i++) { ContactInfo obj = new ContactInfo(); obj.Name = i + "_Name"; obj.Gender = genders[Random.Range(0, 2)]; obj.id = "item : " + i; _contactList.Add(obj); } } #region DATA-SOURCE /// /// Data source method. return the list length. /// public int GetItemCount() { return _contactList.Count; } /// /// Data source method. Called for a cell every time it is recycled. /// Implement this method to do the necessary cell configuration. /// public void SetCell(ICell cell, int index) { //Casting to the implemented Cell var item = cell as DemoCell; item.ConfigureCell(_contactList[index], index); } #endregion }