实现 onScrollListener 检测 ListView 中的滚动结束 [英] Implementation of onScrollListener to detect the end of scrolling in a ListView

查看:18
本文介绍了实现 onScrollListener 检测 ListView 中的滚动结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView 显示一些项目.我想对当前显示在 ListView 的可见部分中的项目执行一些操作,具体取决于 ListView 的滚动方式;因此我想实现ListViewOnScrollListener.根据 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.

P.S.:我看过一些类似的话题,但没有什么能帮助我理解整个事情是如何运作的..!

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!

问候

这篇关于实现 onScrollListener 检测 ListView 中的滚动结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆