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

4
我正在尝试实现一个无限滚动到顶部的功能,在用户接近列表视图顶部时加载更多数据(就像在Messenger应用中一样)。我有一个可以正常工作的滚动到底部的类,但我无法找到一个库或修改这个代码来检测滚动到顶部。有人能帮忙吗?
以下是该类:
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
    }
}

1
只需检测顶部滚动,然后加载数据,之后您可以通知适配器。 - Gopal Singh Sirvi
你可以使用swiperefreshlayout。 - IntelliJ Amiya
@GopalSinghSirvi 嗯,当然这可能不是什么大问题,但由于我刚开始学习Android,而且我找不到如何检测顶部滚动的方法,所以希望能得到一些帮助。 - Kalianey
@IntelliJAmiya 但是此时当用户滚动到顶部时,它不会自动执行吗? - Kalianey
你能看到 firstVisibleItem 参数吗?只需使用 Log.d 记录其值并查看 logcat,其余部分应该很明显。 - pskink
1
@GopalSinghSirvi 如果您不愿意提供帮助,请避免发表无用的评论。 - Cesar
1个回答

1

我对你的类进行了一些修改,添加了一个参数来决定方向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;
    }

}

KaliAney @Cesar 在哪里和何时调用 setScrollDirection 方法? - androidevil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接