Android:ListView.getScrollY() - 它有效吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2132370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:40:51  来源:igfitidea点击:

Android: ListView.getScrollY() - does it work?

androidscroll-offset

提问by Danail

I am using it, but it always returns 0, even though I have scrolled till the end of the list.

我正在使用它,但它总是返回 0,即使我已经滚动到列表的末尾。

回答by Cheryl Simon

getScrollY() is actually a method on View, not ListView. It is referring to the scroll amount of the entire view, so it will almost always be 0.

getScrollY() 实际上是 View 上的一个方法,而不是 ListView。它指的是整个视图的滚动量,所以它几乎总是 0。

If you want to know how far the ListView's contents are scrolled, you can use listView.getFirstVisiblePosition();

如果你想知道 ListView 的内容滚动了多远,你可以使用 listView.getFirstVisiblePosition();

回答by Dinedal

It does work, it returns the top part of the scrolled portion of the view in pixels from the top of the visible view.See the getScrollY()documentation. Basically if your list is taking up the full view then you will always get 0, because the top of the scrolled portion of the list is always at the top of the screen.

它确实有效,它从可见视图的顶部以像素为单位返回视图滚动部分的顶部。请参阅getScrollY()文档。基本上,如果您的列表占据完整视图,那么您将始终获得 0,因为列表滚动部分的顶部始终位于屏幕顶部。

What you want to do to see if you are at the end of a list is something like this:

你想看看你是否在列表的末尾是这样的:

public void onCreate(final Bundle bundle) {
     super.onCreate(bundle);
     setContentView(R.layout.main);
     // The list defined as field elswhere
     this.view = (ListView) findViewById(R.id.searchResults);
     this.view.setOnScrollListener(new OnScrollListener() {
          private int priorFirst = -1;
          @Override
          public void onScroll(final AbsListView view, final int first, final int visible, final int total) {
               // detect if last item is visible
               if (visible < total && (first + visible == total)) {
                    // see if we have more results
                    if (first != priorFirst) {
                         priorFirst = first;
                         //Do stuff here, last item is displayed, end of list reached
                    }
               }
          }
     });
}

The reason for the priorFirst counter is that sometimes scroll events can be generated multiple times, so you only need to react to the first time the end of the list is reached.

priorFirst 计数器的原因是有时滚动事件可以生成多次,因此您只需对第一次到达列表末尾时做出反应。

If you are trying to do an auto-growing list, I'd suggest this tutorial.

如果您正在尝试制作自动增长的列表,我建议您阅读本教程

回答by Rahul Ogale

You need two things to precisely define the scroll position of a listView:

您需要做两件事来精确定义 listView 的滚动位置:

To get current position:

获取当前位置:

int firstVisiblePosition = listView.getFirstVisiblePosition(); 
int topEdge=listView.getChildAt(0).getTop(); //This gives how much the top view has been scrolled.

To set the position:

要设置位置:

listView.setSelectionFromTop(firstVisiblePosition,0);
// Note the '-' sign for scrollTo.. 
listView.scrollTo(0,-topEdge);