/* * 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((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 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("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); } } }