本文介绍了RecyclerView OnScrollListener()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在json中有大约32条记录,我正在使用RecyclerView来显示它们,并且已经实现了OnScrollListener(...)
I have around 32 records in json, I am using RecyclerView to show them and I have implemented OnScrollListener(...)
问题
我开始了一个Activity,我获取了所有32条记录,现在当我滚动时,为什么每次滚动时又一次又一次获得相同的32条记录,这是我对OnScrollListener()的实现
I started an Activity, I fetched all 32 records, now when I do scroll, why I am again getting same 32 records again and again, whenever I do scroll, here is my implementation of OnScrollListener()
public void initializeOnScrollForRecyclerView() {
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
int totalItemCount = recyclerView.getLayoutManager().getItemCount();
int pastVisiblesItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
if (!isLoading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
isLoading = true;
mPostPresenter.loadPosts(false);
}
}
}
});
}
推荐答案
实现似乎是正确的,但对于一种情况它失败,请尝试在dy> 0这样的情况下尝试(也将其放在Activity的OnCreate中):
The Implementation seems to be correct but for one condition it fails, try when dy > 0 like this (Also put this in the OnCreate of the Activity) :
private boolean loading = true;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if (loading) {
if (dy > 0) //check for scroll down
{
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loading = false;
Log.v("...", " Reached Last Item");
loadMoreVideos(searchVideos);
}
}
}
}
});
这篇关于RecyclerView OnScrollListener()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!