简体   繁体   中英

how to paginate data in viewModel with recycler view in fragment using firestore

I am trying to paginate data from fire Store into recycler view with limit of 10 documents I am using staggered grid layout recycler view and view pager, I am holding data in view model class and observing it from two different fragments -- one containing view pager and another containing grid recycler view, how can implement Pagination? I know how to implement it without view model but I need the idea to implement it with view model !

I just want rough idea of steps to implement this not the code.

You have to make local pagination from firebase data array. then set limit and get data from the array.

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
   @Override
   public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
      if (dy > 0) //check for scroll down
      {
        visibleItemCount = mLayoutManager.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();

        if (loading) {
            if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                loading = false;
                if (adapter.countOfShowing < adapter.allChallenges.size()) {
                 //Last data.....
                    adapter.notifyDataSetChanged();
                }
                loading = true;
                //fetch new data
            }
          }
        }
      }
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM