如何在列表上实现分页? [英] How to implement pagination on a list?

查看:127
本文介绍了如何在列表上实现分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可用于实现列表分页的库?

Is there any library that can be used to implement paging for a list?

让我假设我有10行的空间,并且用户可以选择是否要逐页向前或向后滚动(因此+-10个项目).例如,这可以由-1, 0, +1控制.

Let' assume I have a space of 10 lines, and the user can select if he wants to scroll forward or backward by page (thus +- 10 items). This might eg be controlled by -1, 0, +1.

要构建一个类以防止在没有足够的项目显示时可以向后/向前滚动,并自我保存用户当前在页面上的状态,这可能是一项艰巨的工作.

This is probably much work to build a class that prevents scrolling backward/forward if there are not enough items to display, and to self-save the state on which page the user is currently.

那么有什么吗?

推荐答案

我之前已经解决了这个问题.我做了一个静态的getPages方法,该方法将通用集合分解为页面列表(也就是列表).我提供了以下代码.

I've solved this before. I made a static getPages method that breaks a generic collection into a list of pages (which are also lists). I've provided the code below.

public static <T> List<List<T>> getPages(Collection<T> c, Integer pageSize) {
    if (c == null)
        return Collections.emptyList();
    List<T> list = new ArrayList<T>(c);
    if (pageSize == null || pageSize <= 0 || pageSize > list.size())
        pageSize = list.size();
    int numPages = (int) Math.ceil((double)list.size() / (double)pageSize);
    List<List<T>> pages = new ArrayList<List<T>>(numPages);
    for (int pageNum = 0; pageNum < numPages;)
        pages.add(list.subList(pageNum * pageSize, Math.min(++pageNum * pageSize, list.size())));
    return pages;
}

这篇关于如何在列表上实现分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆