【问题标题】:How to implement Paginated List View in Android如何在Android中实现分页列表视图
【发布时间】:2012-04-13 20:42:13
【问题描述】:

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

【问题讨论】:

    标签: android pagination


    【解决方案1】:

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

    【讨论】:

    • 代码有问题,即使我不滚动列表也总是执行,可能是因为加载列表时它可能会自行滚动,你知道吗。
    • LoadGigsTask 没有很好的文档记录,但我认为你可以处理它;-) 你需要在 listview 适配器中添加你的数据,然后使用 notifidatasetchanged
    【解决方案2】:

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

    【讨论】:

    • 这太棒了。效果很好。
    • 请发送带有适配器的类文件,它对我来说非常有用,请我需要实现分页我的输入有页码和每个寻呼机的项目以及 url 我如何加载服务器数据并填充到 gridview.and 如何要实现 onscrolllistener 我如何加载剩余数据并附加到现有适配器请帮助我
    【解决方案3】:

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

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

    https://github.com/nbarraille/paginated_loader

    它几乎为加载器添加了一个loadMore() 方法,您只需实现loadInBackground() 来加载单页数据,并实现appendResults() 来告诉加载器如何将结果组合在一起,仅此而已.

    示例应用使用无限滚动侦听器请求更多数据,一旦到达列表末尾,就会请求更多数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 2019-01-17
      • 2014-08-17
      • 2012-09-06
      相关资源
      最近更新 更多