一个分页类
PagesManager
public class PagesManager
{
private readonly int RECORED_COUNT_PER_PAGE;
private ArrayList _srcList;
private int _totalPages;
public PagesManager(ArrayList SrcList)
{
RECORED_COUNT_PER_PAGE = 25;
_srcList = SrcList;
if(IsOddPageCount())
_totalPages = _srcList.Count / RECORED_COUNT_PER_PAGE + 1;
else
_totalPages = _srcList.Count / RECORED_COUNT_PER_PAGE;
}
private bool IsOddPageCount()
{
return 0 != _srcList.Count % RECORED_COUNT_PER_PAGE;
}
public ArrayList GetNextPage(int idx)
{
return GetCurrentPage(idx);
}
public ArrayList GetPreviousPage(int idx,out int fixedIdx)
{
if(idx >= _totalPages - 1)
fixedIdx = _totalPages - 2;
else
fixedIdx = idx;
return GetCurrentPage(fixedIdx);
}
private ArrayList GetCurrentPage(int pageIdx)
{
if(IsNoNeedToSubPages())
return GetFirstPage();
else if(BeyondTotalPages(pageIdx))
return GetLastPage();
else
return SubPageConverter.GetSubPages(_srcList,
RECORED_COUNT_PER_PAGE * pageIdx,
RECORED_COUNT_PER_PAGE * (pageIdx + 1) - 1);
}
private bool BeyondTotalPages(int pageIdx)
{
return pageIdx >= _totalPages;
}
private ArrayList GetLastPage()
{
return SubPageConverter.GetSubPages(_srcList,
RECORED_COUNT_PER_PAGE *(_totalPages - 1),
_srcList.Count - 1);
}
private ArrayList GetFirstPage()
{
return _srcList;
}
private bool IsNoNeedToSubPages()
{
return RECORED_COUNT_PER_PAGE >= _srcList.Count;
}
private class SubPageConverter
{
public static ArrayList GetSubPages(ArrayList srcList, int from, int to)
{
ArrayList targetList = new ArrayList();
if(from > srcList.Count-1 || to > srcList.Count -1 )
{
throw new Exception("beyond range");
}
if(from > to )
{
throw new Exception("logic error");
}
for(int i = from ;i< to + 1 ;i++)
targetList.Add(srcList[i]);
return targetList;
}
}
}
用法:
PageManager _pm = new PageManager(ArrayList);
_pm.GetPreviousPage(pageIdx,out pageIdx);
_pm.GetNextPage(pageIdx);