简体   繁体   English

在滚动时检测Listview的最后一行-Android

[英]Detect Listview last row on scrolling - Android

I want to load data in listview when it is displaying last row. 我要在显示最后一行时在ListView中加载数据。 I am writing scroll event for listview. 我正在为listview编写滚动事件。

Now how to detect that listview is displaying last row? 现在如何检测列表视图显示的是最后一行?

Can anybody please help me? 有人可以帮我吗?

        lv.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // Check if the last view is visible
            if (++firstVisibleItem + visibleItemCount > totalItemCount) {

                // if so, download more content

                }
            }
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }
    });

You may use Amazing ListView to have auto-loading of next pages (ie "Loading..." on the last item). 您可以使用Amazing ListView自动加载下一页(即,最后一项上的“正在加载...”)。

底部带有“正在加载...”动画的ListView

As per the documentation implement these methods: 根据文档,实现以下方法:

  • onNextPageRequested Called when user scrolls the list until the "loading" indicator is visible. onNextPageRequested在用户滚动列表直到可见“正在加载”指示器时调用。 This is called on UI thread, so if you have some network requests, make sure you do it on the background. 这是在UI线程上调用的,因此,如果您有一些网络请求,请确保在后台执行。 The page number is also given. 还给出了页码。

By default, you are considered to be in the last page, and you will not see any "Loading..." message on the bottom of the list. 默认情况下,您被认为位于最后一页,并且在列表底部不会看到任何“正在加载...”消息。 You need to control whether there are still pages to be displayed. 您需要控制是否还有要显示的页面。

  • notifyMayHaveMorePages to indicate that we still have more data, hence "Loading..." message will be shown. notifyMayHaveMorePages指示我们还有更多数据,因此将显示“正在加载...”消息。
  • notifyNoMorePages to indicate that we have loaded all the data, hence no more "Loading..." message to be shown. notifyNoMorePages指示我们已经加载了所有数据,因此不再显示“ Loading ...”消息。
  • nextPage to increase the page number. nextPage增加页数。 Do this when you append new data into your adapter. 将新数据追加到适配器时,请执行此操作。
  • resetPage to reset the page number to 1 (or to the value specified by setInitialPage ). resetPage将页码重置为1(或重置为setInitialPage指定的值)。

So what you need to do is fetch more data when onNextPageRequested is called. 因此,您需要做的是在onNextPageRequested时获取更多数据。 And call nextPage when you append new data into your adapter. 将新数据追加到适配器中时,请调用nextPage

在列表视图中尝试此方法。

((ScrollView) findViewById(R.id.scrollDowns)).fullScroll(View.FOCUS_DOWN);

The above implementaion by slezadav may cause errors at edge conditions. slezadav的上述实现可能会在边缘条件下导致错误。 The sum of first visible item & items in page may be 1 count more or less than expected. 页面中的第一个可见项和多个项的总和可能比预期多或少1个计数。 I was using the same thing untill i switched to a new method. 在切换到新方法之前,我一直在使用相同的东西。 What i did was put a listener to the Adapter of listview and in the getview check if(position== totalItemCount of items in array to be displayed), if yes i call up my listener from adapter and the listener will Pull more data from api. 我所做的是将侦听器放置到listview的适配器中,并在getview中检查if(position == totalItemCount of array of items to display),如果是的话,我从适配器中调用侦听器,该侦听器将从api中提取更多数据。 Quite simple 非常简单

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

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