如何在Android中实现Paginated List View

时间:2012-03-30 06:55:47

标签: android pagination

我想在android中实现Paginated list View所以当我向下滚动到最后,每次有更多的项目应该添加到我的列表中,目前我从web服务中获取10个项目并在列表中显示它们,现在我当用户向下滚动到列表末尾时,想要再添加10个项目。 有没有办法做到这一点?

3 个答案:

答案 0 :(得分:7)

编辑:这个更好: http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - -

http://benjii.me/2010/08/endless-scrolling-listview-in-android/

public class EndlessScrollListener implements OnScrollListener {

    private int visibleThreshold = 5;
    private int currentPage = 0;
    private int previousTotal = 0;
    private boolean loading = true;

    public EndlessScrollListener() {
    }
    public EndlessScrollListener(int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            // I load the next page of gigs using a background task,
            // but you can call any function here.
            new LoadGigsTask().execute(currentPage + 1);
            loading = true;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
}

答案 1 :(得分:7)

我在这里找到了一个更好的解决方案,实际上看起来像是对Waza_Be解决方案的增强:https://github.com/thecodepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews(我不是作者,因此归功于https://github.com/thecodepath)。

抽象侦听器类使用负责加载数据的抽象方法实现,这对于发送特定请求的活动非常有用。

我只是在这里发布代码,如果代码以某种方式消失在wiki中。

public abstract class EndlessScrollListener implements OnScrollListener {
    // The minimum amount of items to have below your current scroll position
    // before loading more.
    private int visibleThreshold = 5;
    // The current offset index of data you have loaded
    private int currentPage = 0;
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    // True if we are still waiting for the last set of data to load.
    private boolean loading = true;
    // Sets the starting page index
    private int startingPageIndex = 0;

    public EndlessScrollListener() {
    }

    public EndlessScrollListener(int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
    }

    public EndlessScrollListener(int visibleThreshold, int startPage) {
        this.visibleThreshold = visibleThreshold;
        this.startingPageIndex = startPage;
        this.currentPage = startPage;
    }

    // This happens many times a second during a scroll, so be wary of the code you place here.
    // We are given a few useful parameters to help us work out if we need to load some more data,
    // but first we check if we are waiting for the previous load to finish.
    @Override
    public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount) {
        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) { this.loading = true; } 
        }

        // If it’s still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
            currentPage++;
        }

        // If it isn’t currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
            onLoadMore(currentPage + 1, totalItemCount);
            loading = true;
        }
    }

    // Defines the process for actually loading more data based on page
    public abstract void onLoadMore(int page, int totalItemsCount);

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // Don't take any action on changed
    }
}

答案 2 :(得分:0)

我认为raveN发布的滚动监听器是一个很好的方法。 但是在Android中处理数据加载的简洁方法是使用Loader(CursorLoader或AsyncTaskLoader,具体取决于您从何处获取数据)。

令人惊讶的是,我无法找到任何与加载器进行分页的干净方法,所以我继续创建了一个支持它的基础加载器。这是非常早期的阶段,如果您发现错误,请随时提交拉取请求:

https://github.com/nbarraille/paginated_loader

它几乎为Loader添加了loadMore()方法,你只需要实现loadInBackground()来加载单页数据,并appendResults()告诉加载器如何组合结果一起,这就是它。

示例应用程序使用无限滚动侦听器在列表结束时立即请求更多数据,将请求更多数据。