diff --git a/Examples/FancyScrollView/01_Basic/Example01ScrollView.cs b/Examples/FancyScrollView/01_Basic/Example01ScrollView.cs index 91a03ab..ffb191d 100644 --- a/Examples/FancyScrollView/01_Basic/Example01ScrollView.cs +++ b/Examples/FancyScrollView/01_Basic/Example01ScrollView.cs @@ -4,7 +4,6 @@ namespace UnityEngine.UI.Extensions.Examples { public class Example01ScrollView : FancyScrollView { - [SerializeField] ScrollPositionController scrollPositionController; diff --git a/Examples/FancyScrollView/02_CellEventHandling/Example02ScrollView.cs b/Examples/FancyScrollView/02_CellEventHandling/Example02ScrollView.cs index d1e56c9..f78a32f 100644 --- a/Examples/FancyScrollView/02_CellEventHandling/Example02ScrollView.cs +++ b/Examples/FancyScrollView/02_CellEventHandling/Example02ScrollView.cs @@ -2,30 +2,41 @@ namespace UnityEngine.UI.Extensions.Examples { - public class Example02ScrollView : FancyScrollView - { - [SerializeField] - ScrollPositionController scrollPositionController; + public class Example02ScrollView : FancyScrollView + { + [SerializeField] + ScrollPositionController scrollPositionController; - new void Awake() - { - scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition); - SetContext(new Example02ScrollViewContext { OnPressedCell = OnPressedCell }); - base.Awake(); - } + new void Awake() + { + scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition); + // Add OnItemSelected event listener + scrollPositionController.OnItemSelected.AddListener(CellSelected); - public void UpdateData(List data) - { - cellData = data; - scrollPositionController.SetDataCount(cellData.Count); - UpdateContents(); - } + SetContext(new Example02ScrollViewContext { OnPressedCell = OnPressedCell }); + base.Awake(); + } - void OnPressedCell(Example02ScrollViewCell cell) - { - scrollPositionController.ScrollTo(cell.DataIndex, 0.4f); - context.SelectedIndex = cell.DataIndex; - UpdateContents(); - } - } + public void UpdateData(List data) + { + cellData = data; + scrollPositionController.SetDataCount(cellData.Count); + UpdateContents(); + } + + void OnPressedCell(Example02ScrollViewCell cell) + { + scrollPositionController.ScrollTo(cell.DataIndex, 0.4f); + context.SelectedIndex = cell.DataIndex; + UpdateContents(); + } + + // An event triggered when a cell is selected. + void CellSelected(int cellIndex) + { + // Update context.SelectedIndex and call UpdateContents for updating cell's content. + context.SelectedIndex = cellIndex; + UpdateContents(); + } + } } diff --git a/Examples/FancyScrollView/03_InfiniteScroll/Example03ScrollView.cs b/Examples/FancyScrollView/03_InfiniteScroll/Example03ScrollView.cs index c51e273..c89b91f 100644 --- a/Examples/FancyScrollView/03_InfiniteScroll/Example03ScrollView.cs +++ b/Examples/FancyScrollView/03_InfiniteScroll/Example03ScrollView.cs @@ -10,6 +10,10 @@ namespace UnityEngine.UI.Extensions.Examples new void Awake() { scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition); + + // Add OnItemSelected event listener + scrollPositionController.OnItemSelected.AddListener(CellSelected); + SetContext(new Example03ScrollViewContext { OnPressedCell = OnPressedCell }); base.Awake(); } @@ -28,5 +32,12 @@ namespace UnityEngine.UI.Extensions.Examples UpdateContents(); } + // An event triggered when a cell is selected. + void CellSelected(int cellIndex) + { + // Update context.SelectedIndex and call UpdateContents for updating cell's content. + context.SelectedIndex = cellIndex; + UpdateContents(); + } } }