2018-12-31 23:16:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions.Examples
|
|
|
|
|
{
|
|
|
|
|
public class Example04ScrollView : FancyScrollView<Example04CellDto, Example04ScrollViewContext>
|
|
|
|
|
{
|
|
|
|
|
[SerializeField]
|
2019-03-08 02:11:51 +08:00
|
|
|
|
ScrollPositionController scrollPositionController = null;
|
2018-12-31 23:16:28 +08:00
|
|
|
|
|
|
|
|
|
Action<int> onSelectedIndexChanged;
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
scrollPositionController.OnUpdatePosition(p => UpdatePosition(p));
|
|
|
|
|
scrollPositionController.OnItemSelected(HandleItemSelected);
|
|
|
|
|
|
|
|
|
|
SetContext(new Example04ScrollViewContext
|
|
|
|
|
{
|
|
|
|
|
OnPressedCell = OnPressedCell,
|
|
|
|
|
OnSelectedIndexChanged = index =>
|
|
|
|
|
{
|
|
|
|
|
if (onSelectedIndexChanged != null)
|
|
|
|
|
{
|
|
|
|
|
onSelectedIndexChanged(index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateData(List<Example04CellDto> data)
|
|
|
|
|
{
|
|
|
|
|
cellData = data;
|
|
|
|
|
scrollPositionController.SetDataCount(cellData.Count);
|
|
|
|
|
UpdateContents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateSelection(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index < 0 || index >= cellData.Count)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrollPositionController.ScrollTo(index, 0.4f);
|
|
|
|
|
Context.SelectedIndex = index;
|
|
|
|
|
UpdateContents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnSelectedIndexChanged(Action<int> onSelectedIndexChanged)
|
|
|
|
|
{
|
|
|
|
|
this.onSelectedIndexChanged = onSelectedIndexChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNextCell()
|
|
|
|
|
{
|
|
|
|
|
UpdateSelection(Context.SelectedIndex + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevCell()
|
|
|
|
|
{
|
|
|
|
|
UpdateSelection(Context.SelectedIndex - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HandleItemSelected(int selectedItemIndex)
|
|
|
|
|
{
|
|
|
|
|
Context.SelectedIndex = selectedItemIndex;
|
|
|
|
|
UpdateContents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnPressedCell(Example04ScrollViewCell cell)
|
|
|
|
|
{
|
|
|
|
|
UpdateSelection(cell.DataIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|