1

我正在尝试为保存在ArrayList. 基本上我想做的是首先渲染前五个元素ArrayList。然后当用户点击Next (increment by another 5)Previous (decrease by 5)

我的逻辑是这样的:

class foo
{
   private static final int defaultStep = 5;
   private int moveCounter;
   private List<String> values;

   public foo()
   {
     values = new ArrayList<String>();
     values.add("Fiber Channel");
     values.add("Copper Channel");
     ...
   }

  private void pageNext()
  {
   if (moveCounter > -1 && moveCounter < values.size())
   {
    int currentIndex = (moveCounter + 1);
    renderValues(currentIndex, false);
   }
  }

  private void pagePrevious()
  {
   if (moveCounter > -1 && moveCounter <= values.size())
   {
    renderValues(moveCounter-1, true);
   }
  }

 private void renderValues(int startIndex, boolean isPreviousCall)
 {
  if (startIndex > -1)
  {
   StringBuilder html = new StringBuilder();
   List<String> valuesToRender = new ArrayList<String>();
   int checkSteps = 1;
   while (startIndex < values.size())
   {
     valuesToRender.add(values.get(startIndex));
     if (checkSteps == defaultStep) break;
     startIndex++;
     checkSteps++;
   }
   moveCounter = startIndex;

   //TODO: Build html String
   ...
   }
 }
}

我有一个pagePrevious电话问题,你们能帮我在添加要渲染到数组的值之前建立valuesToRender 5 个步骤吗?values arrayvaluesToRender

我也尝试做这样的事情:

  for (int start = startIndex, end = values.size() - 1; start < end; start++, end--)
  {
    if (isPreviousCall) valuesToRender.add(values.get(end));
    else valuesToRender.add(values.get(start));

    if (checkSteps == defaultStep) break;
    checkSteps++;
  }

但这似乎也不起作用。你们能发现并帮助我解决这个问题。多谢你们。

4

4 回答 4

2

基于这里的“pscuderi”解决方案, 我构建了一个包装类,对寻找这个的人有帮助:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PaginatedList<T> {

private static final int DEFAULT_PAGE_SIZE = 10;

private List<T> list;
private List<List<T>> listOfPages;
private int pageSize = DEFAULT_PAGE_SIZE;
private int currentPage = 0;

public PaginatedList(List<T> list) {
    this.list = list;
    initPages();
}

public PaginatedList(List<T> list, int pageSize) {
    this.list = list;
    this.pageSize = pageSize;
    initPages();
}

public List<T> getPage(int pageNumber) {
    if (listOfPages == null || 
        pageNumber > listOfPages.size() ||
        pageNumber < 1) {
        return Collections.emptyList();
    }

    currentPage = pageNumber;
    List<T> page = listOfPages.get(--pageNumber);
    return page;
}

public int numberOfPages() {
    if (listOfPages == null) {
        return 0;
    }

    return listOfPages.size();
}

public List<T> nextPage() {
    List<T> page = getPage(++currentPage);
    return page;
}

public List<T> previousPage() {
    List<T> page = getPage(--currentPage);
    return page;
}

public void initPages() {
    if (list == null || listOfPages != null) {
        return;
    }

    if (pageSize <= 0 || pageSize > list.size()) {
        pageSize = list.size();
    }

    int numOfPages = (int) Math.ceil((double) list.size() / (double) pageSize);
    listOfPages = new ArrayList<List<T>>(numOfPages);
    for (int pageNum = 0; pageNum < numOfPages;) {
        int from = pageNum * pageSize;
        int to = Math.min(++pageNum * pageSize, list.size());
        listOfPages.add(list.subList(from, to));
    }
}

public static void main(String[] args) {
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 1; i <= 62; i++) {
        list.add(i);
    }

    PaginatedList<Integer> paginatedList = new PaginatedList<Integer>(list);
    while (true) {
        List<Integer> page = paginatedList.nextPage();
        if (page == null || page.isEmpty()) {
            break;
        }

        for (Integer value : page) {
            System.out.println(value);
        }

        System.out.println("------------");
    }
}

}
于 2016-07-31T12:56:10.523 回答
1

改变

if (moveCounter > -1 && moveCounter <= archive.size())
{
  renderValues(moveCounter-1, true);
}

if (moveCounter > 0 && moveCounter <= archive.size())
{
  renderValues(moveCounter-1, true);
}
于 2012-04-25T10:29:27.187 回答
1

我会这样做:

我不确定 renderValues 做了什么,以及我们是否必须从 moveCounter 的上限减去 1 或 defaultStep。

private void pageMove (int step)
{
    moveCounter = moveCounter + step;
    if (moveCounter < 0) moveCounter = 0;
    if (moveCounter > values.size ()) moveCounter = values.size ();
    renderValues (currentIndex, false);
}

private void pageNext ()
{
    pageMove (defaultStep);
}

private void pagePrevious ()
{
    pageMove (-defaultStep);
}

前 3 行可以打包成两个大的三元表达式,如下所示:

mc = ((mc + s) < 0) ? 0 : ((mc + s) > vs) ? vs : (mc + s); 

但最好遵循 3 行解决方案。

于 2012-04-25T11:03:15.420 回答
0

这是一个用于分页的简单java函数。请注意,page从 0 开始(第一页)

public List<Object> pagedResponse(List<Object> allItems, int page, int limit){
    int totalItems = allItems.size();
    int fromIndex = page*limit;
    int toIndex = fromIndex+limit;
    if(fromIndex <= totalItems) {
        if(toIndex > totalItems){
            toIndex = totalItems;
        }
        return allItems.subList(fromIndex, toIndex);
    }else {
        return Collections.emptyList();
    }
}
于 2021-08-18T10:17:29.907 回答