onScrollListener的实施,以检测滚动月底在ListView月底、onScrollListener、ListView

2023-09-12 00:18:12 作者:—— “物是人非”

我有一个的ListView 显示一些项目。我想对那些在的ListView 的可见部分,这取决于如何使用的ListView 已滚动;因此,我认为实现了 OnScrollListener 的ListView 。 因此到Android API参考,onScroll法将被调用后,滚动完成。这似乎是我的权利我所需要的,因为一旦滚动完成后,我执行我的的ListView行动(该onScroll方法返回的第一个项目显示的索引和显示的项目的数目)。

I've a ListView displaying some items. I'd like to perform some operation on the items that are currently displayed in the visible portion of the ListView, depending on how the ListView has been scrolled; thus I thought to implements the OnScrollListener of the ListView. Accordingly to the Android api reference, the onScroll method "will be called after the scroll has completed". This seems to me right what I needed, as once the scroll has completed, I perform my actions on the ListView (the onScroll method returns the index of the first item displayed and the number of items displayed).

但一旦实施,我从看到 LogCat中该onScroll方法不只是开除一旦滚动完成,但解雇的进入显示每一个新项目查看,从开始到滚动的末端。这不是我希望和我需要的行为。收听者的其它方法(onScrollStateChanged),相反,不提供有关当前在的ListView显示的项目信息

But once implemented, I see from the LogCat that the onScroll method is not just fired once the scroll has completed, but is fired for every new item that enters the displaying view, from the beginning to the end of the scrolling. This is not the behavior I expect nor I need. The other method of the listener (onScrollStateChanged), instead, does not provide information about the items currently displayed in the ListView.

那么,有没有人知道如何使用这两个方法来检测滚动的结束,并获得有关显示项目的信息? API参考和方法的实际行为之间的错位混淆了我一下。 先谢谢了。

So, does anyone know how to use this couple of methods to detect the ending of the scroll and get the information about the displayed items? The misalignment between the api reference and the actual behavior of the method confused me a bit. Thanks in advance.

PS:我已经看到了一些类似主题的周围,但没有帮助我了解如何在整个事情的作品。

P.S.: I've seen some similar topics around, but nothing helps me understanding how the whole thing works..!

推荐答案

在最后,我已经到了没有那么多优雅,但为我工作的一个解决方案;已经想通了,onScroll方法被调用的每一步滚动只是被称作在滚动结束,而不是,那onScrollStateChanged实际上是被称为只有当滚动完成后,我做这样的事情:

In the end I've reached a solution not so much elegant but that worked for me; having figured out that onScroll method is called for every step of the scrolling instead of just being called at the scroll end, and that onScrollStateChanged is actually being called only when scrolling is completed, I do something like this:

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
 }

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work! ***/
    }
}

实际上,每次在ListView正在滚动我保存的关于第一个可见项目,可见项目数(onScroll法)中的数据;当滚动变化(onScrollStateChanged)国家我保存状态,我调用另一个其中真正了解是否有一直滚动,如果它的完成方法。就这样我也有关于我所需要的可见项目的数据。 也许不干净,但工程!

Practically, everytime the ListView is being scrolled I save the data about the first visible item and the visible item count (onScroll method); when the state of scrolling changes (onScrollStateChanged) I save the state and I call another method which actually understand if there's been a scroll and if it's completed. In this way I also have the data about the visible items that I need. Maybe not clean but works!

问候