RecyclerView OnScrollListener () Problem

I have about 32 entries in json, I use RecyclerView to show them, and I implemented OnScrollListener (...)

Question

I started work, I extracted all 32 records, now when I scroll, why do I get the same 32 records again and again, whenever I 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); } } } }); } 
+7
android android-recyclerview appcompat onscrolllistener
source share
3 answers

The implementation seems to be correct, but on one condition it fails, try when dy> 0, like this (also put this in OnCreate from 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); } } } } }); 
+5
source share

You use this

  if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) 

This means that after viewing 22 records you are downloading again. And when you download again, you get the same data and add it to your list.

I see that you are using RXjava. When you sign up for loadPost, check what data you see. I think it emits the same data again, and your 32 records and these records are added again, and this is an endless loop.

0
source share

Here's another way to call something when the last item is reached

 @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { //When last item is reached if(position == yourList.size() - 1){ loadMoreMessages(); } } public abstract void loadMoreMessages(); 
0
source share

All Articles