Recycler view doesn't always add new items












0















I am using recycler view with a spinner in my app. it loads posts about different subjects as physics , chemistry ... etc. the spinner contains physics and the other subjects. the default item in the spinner is "All subjects" which load all the questions. I am using endless recycler view to load new data when reaching the end. at the begging it works well and after I change the subject in the spinner the new list also works well but after that when I change the subject in the spinner for a third time the new list come with the initial data only and doesn't load more data when I scroll to the end.



this is my presenter code :



    public void getPosts(final String currentSubject) {
postsList = new ArrayList<>();
view.startRecyclerAdapter(postsList);
view.showProgressBar();
view.hideNoPostsText();
view.notifyAdapter();

OnCompleteListener postsListener = new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull final Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
getPostData(document);
}

if (postsList.size() == 0) {
view.onNoPostsExists();
} else {
view.onDataFound();
}

if(task.getResult().size() > 0 ) {
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
lastPosition = task.getResult().size() - 1;
}


RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();

if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;

OnCompleteListener secondQueryCompleteListener = new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
getPostData(d);
}
view.notifyAdapter();

if(t.getResult().size() > 0) {
lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
lastPosition += 5;
}

Log.v("LoggingPosition", "last position is : " + lastPosition);

if (t.getResult().size() < limit) {
isLastItemReached = true;
}
}
}
};

if (currentSubject.equals("كل المواد")) {
Query nextQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING)
.startAfter(lastVisible).limit(limit);
nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
}
else {
Query nextQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
.orderBy("date", Query.Direction.DESCENDING).startAfter(lastVisible).limit(limit);
nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
}
}
}
};
view.setOnScrollListener(onScrollListener);


} else {
Log.v("TAG", "failed");
}
}
};

if (currentSubject.equals("كل المواد")) {
firstQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING).limit(limit);
firstQuery.get().addOnCompleteListener(postsListener);
} else {
firstQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
.orderBy("date", Query.Direction.DESCENDING).limit(limit);
firstQuery.get().addOnCompleteListener(postsListener);
}
}


and this is the related methods in my view :



@Override
public void notifyAdapter() {
recyclerAdapter.notifyDataSetChanged();
}

@Override
public void setOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
recyclerView.addOnScrollListener(onScrollListener);
}









share|improve this question



























    0















    I am using recycler view with a spinner in my app. it loads posts about different subjects as physics , chemistry ... etc. the spinner contains physics and the other subjects. the default item in the spinner is "All subjects" which load all the questions. I am using endless recycler view to load new data when reaching the end. at the begging it works well and after I change the subject in the spinner the new list also works well but after that when I change the subject in the spinner for a third time the new list come with the initial data only and doesn't load more data when I scroll to the end.



    this is my presenter code :



        public void getPosts(final String currentSubject) {
    postsList = new ArrayList<>();
    view.startRecyclerAdapter(postsList);
    view.showProgressBar();
    view.hideNoPostsText();
    view.notifyAdapter();

    OnCompleteListener postsListener = new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull final Task<QuerySnapshot> task) {
    if (task.isSuccessful()) {
    for (DocumentSnapshot document : task.getResult()) {
    getPostData(document);
    }

    if (postsList.size() == 0) {
    view.onNoPostsExists();
    } else {
    view.onDataFound();
    }

    if(task.getResult().size() > 0 ) {
    lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
    lastPosition = task.getResult().size() - 1;
    }


    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
    isScrolling = true;
    }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
    int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
    int visibleItemCount = linearLayoutManager.getChildCount();
    int totalItemCount = linearLayoutManager.getItemCount();

    if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
    isScrolling = false;

    OnCompleteListener secondQueryCompleteListener = new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> t) {
    if (t.isSuccessful()) {
    for (DocumentSnapshot d : t.getResult()) {
    getPostData(d);
    }
    view.notifyAdapter();

    if(t.getResult().size() > 0) {
    lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
    lastPosition += 5;
    }

    Log.v("LoggingPosition", "last position is : " + lastPosition);

    if (t.getResult().size() < limit) {
    isLastItemReached = true;
    }
    }
    }
    };

    if (currentSubject.equals("كل المواد")) {
    Query nextQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING)
    .startAfter(lastVisible).limit(limit);
    nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
    }
    else {
    Query nextQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
    .orderBy("date", Query.Direction.DESCENDING).startAfter(lastVisible).limit(limit);
    nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
    }
    }
    }
    };
    view.setOnScrollListener(onScrollListener);


    } else {
    Log.v("TAG", "failed");
    }
    }
    };

    if (currentSubject.equals("كل المواد")) {
    firstQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING).limit(limit);
    firstQuery.get().addOnCompleteListener(postsListener);
    } else {
    firstQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
    .orderBy("date", Query.Direction.DESCENDING).limit(limit);
    firstQuery.get().addOnCompleteListener(postsListener);
    }
    }


    and this is the related methods in my view :



    @Override
    public void notifyAdapter() {
    recyclerAdapter.notifyDataSetChanged();
    }

    @Override
    public void setOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
    recyclerView.addOnScrollListener(onScrollListener);
    }









    share|improve this question

























      0












      0








      0








      I am using recycler view with a spinner in my app. it loads posts about different subjects as physics , chemistry ... etc. the spinner contains physics and the other subjects. the default item in the spinner is "All subjects" which load all the questions. I am using endless recycler view to load new data when reaching the end. at the begging it works well and after I change the subject in the spinner the new list also works well but after that when I change the subject in the spinner for a third time the new list come with the initial data only and doesn't load more data when I scroll to the end.



      this is my presenter code :



          public void getPosts(final String currentSubject) {
      postsList = new ArrayList<>();
      view.startRecyclerAdapter(postsList);
      view.showProgressBar();
      view.hideNoPostsText();
      view.notifyAdapter();

      OnCompleteListener postsListener = new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull final Task<QuerySnapshot> task) {
      if (task.isSuccessful()) {
      for (DocumentSnapshot document : task.getResult()) {
      getPostData(document);
      }

      if (postsList.size() == 0) {
      view.onNoPostsExists();
      } else {
      view.onDataFound();
      }

      if(task.getResult().size() > 0 ) {
      lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
      lastPosition = task.getResult().size() - 1;
      }


      RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
      super.onScrollStateChanged(recyclerView, newState);
      if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
      isScrolling = true;
      }
      }

      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
      super.onScrolled(recyclerView, dx, dy);

      LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
      int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
      int visibleItemCount = linearLayoutManager.getChildCount();
      int totalItemCount = linearLayoutManager.getItemCount();

      if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
      isScrolling = false;

      OnCompleteListener secondQueryCompleteListener = new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull Task<QuerySnapshot> t) {
      if (t.isSuccessful()) {
      for (DocumentSnapshot d : t.getResult()) {
      getPostData(d);
      }
      view.notifyAdapter();

      if(t.getResult().size() > 0) {
      lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
      lastPosition += 5;
      }

      Log.v("LoggingPosition", "last position is : " + lastPosition);

      if (t.getResult().size() < limit) {
      isLastItemReached = true;
      }
      }
      }
      };

      if (currentSubject.equals("كل المواد")) {
      Query nextQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING)
      .startAfter(lastVisible).limit(limit);
      nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
      }
      else {
      Query nextQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
      .orderBy("date", Query.Direction.DESCENDING).startAfter(lastVisible).limit(limit);
      nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
      }
      }
      }
      };
      view.setOnScrollListener(onScrollListener);


      } else {
      Log.v("TAG", "failed");
      }
      }
      };

      if (currentSubject.equals("كل المواد")) {
      firstQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING).limit(limit);
      firstQuery.get().addOnCompleteListener(postsListener);
      } else {
      firstQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
      .orderBy("date", Query.Direction.DESCENDING).limit(limit);
      firstQuery.get().addOnCompleteListener(postsListener);
      }
      }


      and this is the related methods in my view :



      @Override
      public void notifyAdapter() {
      recyclerAdapter.notifyDataSetChanged();
      }

      @Override
      public void setOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
      recyclerView.addOnScrollListener(onScrollListener);
      }









      share|improve this question














      I am using recycler view with a spinner in my app. it loads posts about different subjects as physics , chemistry ... etc. the spinner contains physics and the other subjects. the default item in the spinner is "All subjects" which load all the questions. I am using endless recycler view to load new data when reaching the end. at the begging it works well and after I change the subject in the spinner the new list also works well but after that when I change the subject in the spinner for a third time the new list come with the initial data only and doesn't load more data when I scroll to the end.



      this is my presenter code :



          public void getPosts(final String currentSubject) {
      postsList = new ArrayList<>();
      view.startRecyclerAdapter(postsList);
      view.showProgressBar();
      view.hideNoPostsText();
      view.notifyAdapter();

      OnCompleteListener postsListener = new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull final Task<QuerySnapshot> task) {
      if (task.isSuccessful()) {
      for (DocumentSnapshot document : task.getResult()) {
      getPostData(document);
      }

      if (postsList.size() == 0) {
      view.onNoPostsExists();
      } else {
      view.onDataFound();
      }

      if(task.getResult().size() > 0 ) {
      lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
      lastPosition = task.getResult().size() - 1;
      }


      RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
      super.onScrollStateChanged(recyclerView, newState);
      if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
      isScrolling = true;
      }
      }

      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
      super.onScrolled(recyclerView, dx, dy);

      LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
      int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
      int visibleItemCount = linearLayoutManager.getChildCount();
      int totalItemCount = linearLayoutManager.getItemCount();

      if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
      isScrolling = false;

      OnCompleteListener secondQueryCompleteListener = new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull Task<QuerySnapshot> t) {
      if (t.isSuccessful()) {
      for (DocumentSnapshot d : t.getResult()) {
      getPostData(d);
      }
      view.notifyAdapter();

      if(t.getResult().size() > 0) {
      lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
      lastPosition += 5;
      }

      Log.v("LoggingPosition", "last position is : " + lastPosition);

      if (t.getResult().size() < limit) {
      isLastItemReached = true;
      }
      }
      }
      };

      if (currentSubject.equals("كل المواد")) {
      Query nextQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING)
      .startAfter(lastVisible).limit(limit);
      nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
      }
      else {
      Query nextQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
      .orderBy("date", Query.Direction.DESCENDING).startAfter(lastVisible).limit(limit);
      nextQuery.get().addOnCompleteListener(secondQueryCompleteListener);
      }
      }
      }
      };
      view.setOnScrollListener(onScrollListener);


      } else {
      Log.v("TAG", "failed");
      }
      }
      };

      if (currentSubject.equals("كل المواد")) {
      firstQuery = fireStorePosts.orderBy("date", Query.Direction.DESCENDING).limit(limit);
      firstQuery.get().addOnCompleteListener(postsListener);
      } else {
      firstQuery = fireStorePosts.whereEqualTo("subject", currentSubject)
      .orderBy("date", Query.Direction.DESCENDING).limit(limit);
      firstQuery.get().addOnCompleteListener(postsListener);
      }
      }


      and this is the related methods in my view :



      @Override
      public void notifyAdapter() {
      recyclerAdapter.notifyDataSetChanged();
      }

      @Override
      public void setOnScrollListener(RecyclerView.OnScrollListener onScrollListener) {
      recyclerView.addOnScrollListener(onScrollListener);
      }






      android android-recyclerview pagination adapter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 22:54









      MostafaMostafa

      165




      165
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463071%2frecycler-view-doesnt-always-add-new-items%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463071%2frecycler-view-doesnt-always-add-new-items%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin