繁体   English   中英

如何在Android中实现Paginated List View

[英]How to implement Paginated List View in Android

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

我在这里找到了一个更好的解决方案,实际上看起来像是对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
    }
}

编辑:这个更好: 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) {
    }
}

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

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

https://github.com/nbarraille/paginated_loader

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM