|
|
@@ -28,7 +28,10 @@ namespace FancyScrollView.FocusOn
|
|
|
Context.OnCellClicked = onClick;
|
|
|
|
|
|
scroller.OnValueChanged(UpdatePosition);
|
|
|
- scroller.OnSelectionChanged(UpdateSelection);
|
|
|
+ scroller.OnSelectionChanged((index) => {
|
|
|
+ Context.ScrollSelectedIndex = index;
|
|
|
+ UpdateSelection(index);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
void UpdateSelection(int index)
|
|
|
@@ -59,23 +62,100 @@ namespace FancyScrollView.FocusOn
|
|
|
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 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);
|
|
|
+ int itemsPerPage = 4;
|
|
|
+ int totalItems = ItemsSource.Count;
|
|
|
+
|
|
|
+ // 计算目标索引
|
|
|
+ int nextIndex = Context.SelectedIndex + 1;
|
|
|
+ int targetIndex = Context.SelectedIndex + itemsPerPage;
|
|
|
+
|
|
|
+ //计算目标索引和起始索引的差值
|
|
|
+ int difIndex = targetIndex - Context.ScrollSelectedIndex;
|
|
|
+
|
|
|
+ int targetPageStartIndex = nextIndex;
|
|
|
+ if (difIndex >= itemsPerPage)
|
|
|
+ {
|
|
|
+ // 计算目标页的起始索引
|
|
|
+ targetPageStartIndex = targetIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保 targetPageStartIndex 在总项范围内
|
|
|
+ if (targetPageStartIndex >= totalItems)
|
|
|
+ {
|
|
|
+ targetPageStartIndex -= totalItems;
|
|
|
+ }
|
|
|
+
|
|
|
+ Debug.LogWarning(
|
|
|
+ "Context.ScrollSelectedIndex:" + Context.ScrollSelectedIndex +
|
|
|
+ "Context.SelectedIndex:" + Context.SelectedIndex +
|
|
|
+ ", targetIndex:" + targetIndex +
|
|
|
+ ", targetPageStartIndex:" + targetPageStartIndex +
|
|
|
+ ", totalItems:" + totalItems);
|
|
|
+
|
|
|
+ // 翻页(4个一组)
|
|
|
+ Context.ScrollSelectedIndex = targetPageStartIndex;
|
|
|
+ SelectCell(targetPageStartIndex);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ //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 nextIndex = Context.SelectedIndex - 4;
|
|
|
- int index = nextIndex >= 0 ? nextIndex : ItemsSource.Count + nextIndex;
|
|
|
- // Debug.LogWarning("nextIndex:" + nextIndex + ",index:" + index + ",ItemsSource.Count:" + ItemsSource.Count);
|
|
|
- SelectCell(index);
|
|
|
+ int itemsPerPage = 4;
|
|
|
+ int totalItems = ItemsSource.Count;
|
|
|
+
|
|
|
+ // 计算目标索引
|
|
|
+ int prevIndex = Context.SelectedIndex - 1;
|
|
|
+ int targetIndex = Context.SelectedIndex - itemsPerPage;
|
|
|
+
|
|
|
+ //计算目标索引和起始索引的差值
|
|
|
+ int difIndex = Context.ScrollSelectedIndex - targetIndex;
|
|
|
+
|
|
|
+ int targetPageStartIndex = prevIndex;
|
|
|
+
|
|
|
+ // 如果当前页面与目标页面之间的差距超过了 itemsPerPage,则跳到上一页的第一项
|
|
|
+ if (difIndex >= itemsPerPage)
|
|
|
+ {
|
|
|
+ targetPageStartIndex = targetIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保 targetPageStartIndex 在总项范围内
|
|
|
+ if (targetPageStartIndex < 0)
|
|
|
+ {
|
|
|
+ targetPageStartIndex += totalItems;
|
|
|
+ }
|
|
|
+
|
|
|
+ Debug.LogWarning(
|
|
|
+ "Context.ScrollSelectedIndex:" + Context.ScrollSelectedIndex +
|
|
|
+ ", Context.SelectedIndex:" + Context.SelectedIndex +
|
|
|
+ ", targetIndex:" + targetIndex +
|
|
|
+ ", targetPageStartIndex:" + targetPageStartIndex +
|
|
|
+ ", totalItems:" + totalItems);
|
|
|
+
|
|
|
+ // 翻页(4个一组)
|
|
|
+ Context.ScrollSelectedIndex = targetPageStartIndex;
|
|
|
+ SelectCell(targetPageStartIndex);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public void SelectCell(int index,bool bScrollTo = true)
|
|
|
{
|
|
|
if (index < 0 || index >= ItemsSource.Count || index == Context.SelectedIndex)
|