Android:无限滚动到列表视图中的TOP

Android:无限滚动到列表视图中的TOP,第1张

概述我正在尝试实现无限滚动到顶部,当用户接近列表视图的顶部时(如在Messenger应用程序中),我将加载更多数据.我有一个滚动到底层的工作正常,但我没有设法找到一个库或修改此代码来检测滚动到顶部. 有人可以帮忙吗? 这是班级: public abstract class EndlessScrollListener implements AbsListView.OnScrollListener { 我正在尝试实现无限滚动到顶部,当用户接近列表视图的顶部时(如在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    }}
解决方法 我对你的类做了一些修改,添加一个参数来决定方向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 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,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 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:无限滚动到列表视图中的TOP全部内容,希望文章能够帮你解决Android:无限滚动到列表视图中的TOP所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1125574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存