public abstract class InfiniteScroll extends RecyclerVIEw.OnScrollListener {private @R_502_659@ mLayoutManager;private int prevIoUsTotal = 0;private boolean loading = true;private int visibleThreshold = 5;int firstVisibleItem,visibleItemCount,totalitemCount;private int current_page = 1;public InfiniteScroll(@R_502_659@ layoutManager) { mLayoutManager = layoutManager;}@OverrIDepublic voID onScrolled(RecyclerVIEw recyclerVIEw,int dx,int dy) { visibleItemCount = recyclerVIEw.getChildCount(); totalitemCount = mLayoutManager.getItemCount(); firstVisibleItem = mLayoutManager.findFirstVisibleItemposition(); if (loading) { if (totalitemCount > prevIoUsTotal) { loading = false; prevIoUsTotal = totalitemCount; } } if (!loading && (totalitemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { // End has been reached // Do something current_page++; Log.d("moreitems","why is this being called twice?"); loadMore(current_page); loading = true; }}public abstract voID loadMore(int current_page);}
这个代码是作为一个单独的SO问题的答案发布的,其他示例似乎功能类似,但我仍然得到loadMore()方法执行两次到达RecyclerVIEw的底部,我不知道为什么.
解决方法 它被调用,因为当你加载更多项时,再次调用onScrolled.This callback will also be called if visible item range changes after a layout calculation. In that case,dx and dy will be 0.
所以你可以检查一下if(!loading&& dy> 0)同样
总结以上是内存溢出为你收集整理的android – RecyclerView Infinite Scroll监听器被调用两次全部内容,希望文章能够帮你解决android – RecyclerView Infinite Scroll监听器被调用两次所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)