|
@@ -1,90 +0,0 @@
|
|
-using System.Collections.Generic;
|
|
|
|
-
|
|
|
|
-namespace GFGGame
|
|
|
|
-{
|
|
|
|
- public class ListUtil
|
|
|
|
- {
|
|
|
|
- /// <summary>
|
|
|
|
- /// 用来做泛型数据切换,上一条,下一条...
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="list"></param>
|
|
|
|
- /// <param name="type"></param>
|
|
|
|
- /// <param name="currentIndex">当前处于的索引</param>
|
|
|
|
- /// <param name="newIndex">操作数据之后返回的数据的索引,也就是最新的索引</param>
|
|
|
|
- /// <typeparam name="T"></typeparam>
|
|
|
|
- /// <returns></returns>
|
|
|
|
- public static T Navigate<T>(List<T> list, NavigateType type, int currentIndex, out int newIndex) where T : new()
|
|
|
|
- {
|
|
|
|
- if (type == NavigateType.Previous)
|
|
|
|
- {
|
|
|
|
- //如果已经是最前面的一条数据,则切换到最后一条数据,形成一个切换循环
|
|
|
|
- if (currentIndex <= 0)
|
|
|
|
- {
|
|
|
|
- currentIndex = list.Count - 1;
|
|
|
|
- newIndex = currentIndex;
|
|
|
|
- return list[currentIndex];
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else if (type == NavigateType.Next)
|
|
|
|
- {
|
|
|
|
- //如果已经是最后一条数据,则切换到第一条数据,形成一个切换循环
|
|
|
|
- if (currentIndex >= list.Count - 1)
|
|
|
|
- {
|
|
|
|
- currentIndex = 0;
|
|
|
|
- newIndex = currentIndex;
|
|
|
|
- return list[currentIndex];
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- newIndex = currentIndex;
|
|
|
|
- return list[currentIndex];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- int previousIndex = currentIndex - 1;
|
|
|
|
- int nextIndex = currentIndex + 1;
|
|
|
|
-
|
|
|
|
- if (previousIndex < 0 && nextIndex >= list.Count)
|
|
|
|
- {
|
|
|
|
- newIndex = currentIndex;
|
|
|
|
- return list[currentIndex];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (type == NavigateType.Previous && previousIndex >= 0)
|
|
|
|
- {
|
|
|
|
- newIndex = previousIndex;
|
|
|
|
- return list[previousIndex];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (type == NavigateType.Next && nextIndex < list.Count)
|
|
|
|
- {
|
|
|
|
- newIndex = nextIndex;
|
|
|
|
- return list[nextIndex];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- newIndex = currentIndex;
|
|
|
|
- return list[currentIndex];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 动作枚举
|
|
|
|
- /// </summary>
|
|
|
|
- public enum NavigateType
|
|
|
|
- {
|
|
|
|
- /// <summary>
|
|
|
|
- /// 不作切换动作,返回当前索引的数据
|
|
|
|
- /// </summary>
|
|
|
|
- None = 0,
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 上一条
|
|
|
|
- /// </summary>
|
|
|
|
- Previous = 1,
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 下一条
|
|
|
|
- /// </summary>
|
|
|
|
- Next = 2
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|