| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /*
- * 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((index) => {
- Context.ScrollSelectedIndex = index;
- Debug.Log("Context.ScrollSelectedIndex:"+ Context.ScrollSelectedIndex);
- UpdateSelection(index);
- });
- }
- 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("Context.SelectedIndex:"+ Context.SelectedIndex + ",nextIndex:" + nextIndex+ ",index:"+ index + ",ItemsSource.Count:"+ ItemsSource.Count);
- // //翻页(4个一组)
- // SelectCell(index);
- //}
- public void SelectNextCell()
- {
- int itemsPerPage = 4;
- int totalItems = ItemsSource.Count;
- // 计算目标索引
- int targetIndex = Context.ScrollSelectedIndex + itemsPerPage;
- // 确保 targetPageStartIndex 在总项范围内
- if (targetIndex >= totalItems)
- {
- targetIndex -= totalItems;
- }
- //Debug.LogWarning(
- // "Context.ScrollSelectedIndex:" + Context.ScrollSelectedIndex +
- // "Context.SelectedIndex:" + Context.SelectedIndex +
- // ", targetIndex:" + targetIndex +
- // ", totalItems:" + totalItems);
- // 翻页(4个一组)
- SelectCell(targetIndex);
- }
- //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 SelectPrevCell()
- {
- int itemsPerPage = 4;
- int totalItems = ItemsSource.Count;
- // 计算目标索引
- int prevIndex = Context.ScrollSelectedIndex - itemsPerPage;
- // 确保 prevIndex 在总项范围内
- if (prevIndex < 0)
- {
- prevIndex += totalItems;
- }
- //Debug.LogWarning(
- // "Context.ScrollSelectedIndex:" + Context.ScrollSelectedIndex +
- // ", Context.SelectedIndex:" + Context.SelectedIndex +
- // ", prevIndex:" + prevIndex +
- // ", totalItems:" + totalItems);
- // 翻页(4个一组)
- SelectCell(prevIndex);
- }
- 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);
- }
- }
- }
|