概述
我有一个RecyclerView,我试图实现无限滚动功能.问题是,OnScrolled方法中if语句中的代码正在执行两次.继承我目前的代码:
public abstract class InfiniteScroll extends RecyclerView.OnScrollListener {
private LinearLayoutManager mLayoutManager;
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
public InfiniteScroll(LinearLayoutManager layoutManager) {
mLayoutManager = layoutManager;
}
@Override
public 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的底部,我不知道为什么.
最后
以上就是呆萌玫瑰为你收集整理的android监听网络调用2次,android – RecyclerView Infinite Scroll监听器被调用两次的全部内容,希望文章能够帮你解决android监听网络调用2次,android – RecyclerView Infinite Scroll监听器被调用两次所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复