| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /*
- * 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<ItemData, Context>
- {
- [SerializeField] Scroller scroller = default;
- [SerializeField] GameObject cellPrefab = default;
- Action<int,bool> 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<ItemData> items)
- {
- UpdateContents(items);
- scroller.SetTotalCount(items.Count);
- }
- public void OnClickEvent(Action<int> OnCellClicked) {
- }
- public void OnSelectionChanged(Action<int,bool> 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);
- }
- }
- }
|