Android:在 ListView 中无限滚动到顶部

标签 android infinite-scroll endlessscroll

我正在尝试实现无限滚动到顶部,当用户接近 ListView 的顶部时,我会在其中加载更多数据(就像在 Messenger 应用程序中一样)。我有一个滚动到底部的类,它工作正常,但我没有设法找到一个库或修改此代码来检测滚动到顶部。 有人可以帮忙吗?

这是类(class):

public abstract class EndlessScrollListener implements AbsListView.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
    }
}

最佳答案

我对你的类做了一些修改,添加了一个参数来决定方向 setScrollDirection 默认情况下它将是 SCROLL_DIRECTION_DOWN 请测试它并让我知道它是如何工作的: )

public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {

    public final static int SCROLL_DIRECTION_UP = 0;
    public final static int SCROLL_DIRECTION_DOWN = 1;

    // 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;

    private int scrollDirection = SCROLL_DIRECTION_DOWN;

    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)
    {
        Log.v("EndlessScroll", "firstVisibleItem: "+firstVisibleItem);
        Log.v("EndlessScroll", "visibleItemCount: "+visibleItemCount);
        Log.v("EndlessScroll", "totalItemCount: "+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)
        {
            if( scrollDirection == SCROLL_DIRECTION_DOWN && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
                onLoadMore(currentPage + 1, totalItemCount);
                loading = true;
            }
            else if( scrollDirection == SCROLL_DIRECTION_UP && 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
    }

    public int getScrollDirection() {
        return scrollDirection;
    }

    public void setScrollDirection(int scrollDirection) {
        if (scrollDirection == SCROLL_DIRECTION_DOWN || scrollDirection == SCROLL_DIRECTION_UP)
        { this.scrollDirection = scrollDirection; }
    }

    public boolean isLoading() {
        return loading;
    }

    public void finishedLoading() {
        this.loading = false;
    }

}

关于Android:在 ListView 中无限滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32140209/

相关文章:

c# - Xamarin Android 文件路径太长

android - 如何登录 Fabrics 发送长消息

android - 如何存储 2d 横向卷轴游戏的屏幕对象?

objective-c - 如何在iOS 6 的UICollectionView 中实现无限水平和垂直滚动?

android - Firebase 无限滚动 ListView 在滚动时加载 10 个项目

javascript - 获取新内容后重新加载脚本(无限滚动 wordpress + fancybox)

android - 实现无尽的 RecyclerView

android - Recyclerview addOnScrollListener问题

java - 与 firebase 数据库连接的问题(每当我尝试保存到数据库时,应用程序都会崩溃)

android - recyclerview 交错网格布局管理器加载更多进度条