using System.Collections.Generic;
namespace GFGGame
{
public class ListUtil
{
///
/// 用来做泛型数据切换,上一条,下一条...
///
///
///
/// 当前处于的索引
/// 操作数据之后返回的数据的索引,也就是最新的索引
///
///
public static T Navigate(List 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];
}
///
/// 动作枚举
///
public enum NavigateType
{
///
/// 不作切换动作,返回当前索引的数据
///
None = 0,
///
/// 上一条
///
Previous = 1,
///
/// 下一条
///
Next = 2
}
}
}