android – RecyclerView Infinite Scroll监听器被调用两次

android – RecyclerView Infinite Scroll监听器被调用两次,第1张

概述我有一个RecyclerView,我试图实现无限滚动功能.问题是,OnScrolled方法中if语句中的代码正在执行两次.继承我目前的代码: public abstract class InfiniteScroll extends RecyclerView.OnScrollListener {private LinearLayoutManager mLayoutManager;private 我有一个RecyclerVIEw,我试图实现无限滚动功能.问题是,OnScrolled方法中if语句中的代码正在执行两次.继承我目前的代码:
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监听器被调用两次所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存