2017-08-02 17:21:45 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions.Examples
|
|
|
|
|
{
|
2017-08-08 02:33:53 +08:00
|
|
|
|
public class Example02ScrollView : FancyScrollView<Example02CellDto, Example02ScrollViewContext>
|
|
|
|
|
{
|
|
|
|
|
[SerializeField]
|
|
|
|
|
ScrollPositionController scrollPositionController;
|
2017-08-02 17:21:45 +08:00
|
|
|
|
|
2017-08-08 02:33:53 +08:00
|
|
|
|
new void Awake()
|
|
|
|
|
{
|
|
|
|
|
scrollPositionController.OnUpdatePosition.AddListener(UpdatePosition);
|
|
|
|
|
// Add OnItemSelected event listener
|
|
|
|
|
scrollPositionController.OnItemSelected.AddListener(CellSelected);
|
2017-08-02 17:21:45 +08:00
|
|
|
|
|
2017-08-08 02:33:53 +08:00
|
|
|
|
SetContext(new Example02ScrollViewContext { OnPressedCell = OnPressedCell });
|
|
|
|
|
base.Awake();
|
|
|
|
|
}
|
2017-08-02 17:21:45 +08:00
|
|
|
|
|
2017-08-08 02:33:53 +08:00
|
|
|
|
public void UpdateData(List<Example02CellDto> 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-02 17:21:45 +08:00
|
|
|
|
}
|