ScrollView.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * FancyScrollView (https://github.com/setchi/FancyScrollView)
  3. * Copyright (c) 2020 setchi
  4. * Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
  5. */
  6. using System;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using EasingCore;
  10. namespace FancyScrollView.FocusOn
  11. {
  12. class ScrollView : FancyScrollView<ItemData, Context>
  13. {
  14. [SerializeField] Scroller scroller = default;
  15. [SerializeField] GameObject cellPrefab = default;
  16. Action<int,bool> onSelectionChanged;
  17. protected override GameObject CellPrefab => cellPrefab;
  18. protected override void Initialize()
  19. {
  20. base.Initialize();
  21. Context.OnCellClicked = onClick;
  22. scroller.OnValueChanged(UpdatePosition);
  23. scroller.OnSelectionChanged((index) => {
  24. Context.ScrollSelectedIndex = index;
  25. Debug.Log("Context.ScrollSelectedIndex:"+ Context.ScrollSelectedIndex);
  26. UpdateSelection(index);
  27. });
  28. }
  29. void UpdateSelection(int index)
  30. {
  31. if (Context.SelectedIndex == index)
  32. {
  33. return;
  34. }
  35. Context.SelectedIndex = index;
  36. Refresh();
  37. onSelectionChanged?.Invoke(index,false);
  38. }
  39. public void UpdateData(IList<ItemData> items)
  40. {
  41. UpdateContents(items);
  42. scroller.SetTotalCount(items.Count);
  43. }
  44. public void OnClickEvent(Action<int> OnCellClicked) {
  45. }
  46. public void OnSelectionChanged(Action<int,bool> callback)
  47. {
  48. onSelectionChanged = callback;
  49. }
  50. //public void SelectNextCell()
  51. //{
  52. // int nextIndex = Context.SelectedIndex + 4;
  53. // int index = nextIndex < ItemsSource.Count ? nextIndex : nextIndex - ItemsSource.Count;
  54. // Debug.LogWarning("Context.SelectedIndex:"+ Context.SelectedIndex + ",nextIndex:" + nextIndex+ ",index:"+ index + ",ItemsSource.Count:"+ ItemsSource.Count);
  55. // //翻页(4个一组)
  56. // SelectCell(index);
  57. //}
  58. public void SelectNextCell()
  59. {
  60. int itemsPerPage = 4;
  61. int totalItems = ItemsSource.Count;
  62. // 计算目标索引
  63. int targetIndex = Context.ScrollSelectedIndex + itemsPerPage;
  64. // 确保 targetPageStartIndex 在总项范围内
  65. if (targetIndex >= totalItems)
  66. {
  67. targetIndex -= totalItems;
  68. }
  69. //Debug.LogWarning(
  70. // "Context.ScrollSelectedIndex:" + Context.ScrollSelectedIndex +
  71. // "Context.SelectedIndex:" + Context.SelectedIndex +
  72. // ", targetIndex:" + targetIndex +
  73. // ", totalItems:" + totalItems);
  74. // 翻页(4个一组)
  75. SelectCell(targetIndex);
  76. }
  77. //public void SelectPrevCell()
  78. //{
  79. // int nextIndex = Context.SelectedIndex - 4;
  80. // int index = nextIndex >= 0 ? nextIndex : ItemsSource.Count + nextIndex;
  81. // // Debug.LogWarning("nextIndex:" + nextIndex + ",index:" + index + ",ItemsSource.Count:" + ItemsSource.Count);
  82. // SelectCell(index);
  83. //}
  84. public void SelectPrevCell()
  85. {
  86. int itemsPerPage = 4;
  87. int totalItems = ItemsSource.Count;
  88. // 计算目标索引
  89. int prevIndex = Context.ScrollSelectedIndex - itemsPerPage;
  90. // 确保 prevIndex 在总项范围内
  91. if (prevIndex < 0)
  92. {
  93. prevIndex += totalItems;
  94. }
  95. //Debug.LogWarning(
  96. // "Context.ScrollSelectedIndex:" + Context.ScrollSelectedIndex +
  97. // ", Context.SelectedIndex:" + Context.SelectedIndex +
  98. // ", prevIndex:" + prevIndex +
  99. // ", totalItems:" + totalItems);
  100. // 翻页(4个一组)
  101. SelectCell(prevIndex);
  102. }
  103. public void SelectCell(int index,bool bScrollTo = true)
  104. {
  105. if (index < 0 || index >= ItemsSource.Count || index == Context.SelectedIndex)
  106. {
  107. //if (index == Context.SelectedIndex) {
  108. // onSelectionChanged?.Invoke(index,true);
  109. //}
  110. return;
  111. }
  112. UpdateSelection(index);
  113. if(bScrollTo)
  114. scroller.ScrollTo(index, 0.35f, Ease.OutCubic);
  115. }
  116. public void onClick(int index, bool bSelectedAndNav = true) {
  117. Context.SelectedIndex = index;
  118. Refresh();
  119. //点击按钮算触发
  120. onSelectionChanged?.Invoke(index, bSelectedAndNav);
  121. }
  122. }
  123. }