/* * FancyScrollView (https://github.com/setchi/FancyScrollView) * Copyright (c) 2020 setchi * Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE) */ using System; using System.Collections.Generic; using UnityEngine; using EasingCore; namespace FancyScrollView.FocusOn { class ScrollView : FancyScrollView { [SerializeField] Scroller scroller = default; [SerializeField] GameObject cellPrefab = default; Action onSelectionChanged; protected override GameObject CellPrefab => cellPrefab; protected override void Initialize() { base.Initialize(); Context.OnCellClicked = onClick; scroller.OnValueChanged(UpdatePosition); scroller.OnSelectionChanged(UpdateSelection); } void UpdateSelection(int index) { if (Context.SelectedIndex == index) { return; } Context.SelectedIndex = index; Refresh(); onSelectionChanged?.Invoke(index,false); } public void UpdateData(IList items) { UpdateContents(items); scroller.SetTotalCount(items.Count); } public void OnClickEvent(Action OnCellClicked) { } public void OnSelectionChanged(Action callback) { onSelectionChanged = callback; } public void SelectNextCell() { int nextIndex = Context.SelectedIndex + 4; int index = nextIndex < ItemsSource.Count ? nextIndex : nextIndex - ItemsSource.Count; // Debug.LogWarning("nextIndex:" + nextIndex+ ",index:"+ index + ",ItemsSource.Count:"+ ItemsSource.Count); //翻页(4个一组) SelectCell(index); } public void SelectPrevCell() { int nextIndex = Context.SelectedIndex - 4; int index = nextIndex >= 0 ? nextIndex : ItemsSource.Count + nextIndex; // Debug.LogWarning("nextIndex:" + nextIndex + ",index:" + index + ",ItemsSource.Count:" + ItemsSource.Count); SelectCell(index); } public void SelectCell(int index,bool bScrollTo = true) { if (index < 0 || index >= ItemsSource.Count || index == Context.SelectedIndex) { //if (index == Context.SelectedIndex) { // onSelectionChanged?.Invoke(index,true); //} return; } UpdateSelection(index); if(bScrollTo) scroller.ScrollTo(index, 0.35f, Ease.OutCubic); } public void onClick(int index, bool bSelectedAndNav = true) { Context.SelectedIndex = index; Refresh(); //点击按钮算触发 onSelectionChanged?.Invoke(index, bSelectedAndNav); } } }