FancyScrollView control updated to add an event when the page is changed / Selected

Thanks to @ChongHann (Hann Chong) for the fix

Resolves #157
pull/413/head
Simon Jackson 2017-08-07 19:33:53 +01:00
parent adb09d3481
commit b4a2e81aae
3 changed files with 45 additions and 24 deletions

View File

@ -4,7 +4,6 @@ namespace UnityEngine.UI.Extensions.Examples
{ {
public class Example01ScrollView : FancyScrollView<Example01CellDto> public class Example01ScrollView : FancyScrollView<Example01CellDto>
{ {
[SerializeField] [SerializeField]
ScrollPositionController scrollPositionController; ScrollPositionController scrollPositionController;

View File

@ -2,30 +2,41 @@
namespace UnityEngine.UI.Extensions.Examples namespace UnityEngine.UI.Extensions.Examples
{ {
public class Example02ScrollView : FancyScrollView<Example02CellDto, Example02ScrollViewContext> public class Example02ScrollView : FancyScrollView<Example02CellDto, Example02ScrollViewContext>
{ {
[SerializeField] [SerializeField]
ScrollPositionController scrollPositionController; ScrollPositionController scrollPositionController;
new void Awake() new void Awake()
{ {
scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition); scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition);
SetContext(new Example02ScrollViewContext { OnPressedCell = OnPressedCell }); // Add OnItemSelected event listener
base.Awake(); scrollPositionController.OnItemSelected.AddListener(CellSelected);
}
public void UpdateData(List<Example02CellDto> data) SetContext(new Example02ScrollViewContext { OnPressedCell = OnPressedCell });
{ base.Awake();
cellData = data; }
scrollPositionController.SetDataCount(cellData.Count);
UpdateContents();
}
void OnPressedCell(Example02ScrollViewCell cell) public void UpdateData(List<Example02CellDto> data)
{ {
scrollPositionController.ScrollTo(cell.DataIndex, 0.4f); cellData = data;
context.SelectedIndex = cell.DataIndex; scrollPositionController.SetDataCount(cellData.Count);
UpdateContents(); 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();
}
}
} }

View File

@ -10,6 +10,10 @@ namespace UnityEngine.UI.Extensions.Examples
new void Awake() new void Awake()
{ {
scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition); scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition);
// Add OnItemSelected event listener
scrollPositionController.OnItemSelected.AddListener(CellSelected);
SetContext(new Example03ScrollViewContext { OnPressedCell = OnPressedCell }); SetContext(new Example03ScrollViewContext { OnPressedCell = OnPressedCell });
base.Awake(); base.Awake();
} }
@ -28,5 +32,12 @@ namespace UnityEngine.UI.Extensions.Examples
UpdateContents(); 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();
}
} }
} }