简体   繁体   English

如何使用firestore在片段中使用回收器视图对viewModel中的数据进行分页

[英]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?我正在尝试将来自 fire Store 的数据分页到回收器视图中,限制为 10 个文档和另一个包含网格回收器的视图,如何实现分页? I know how to implement it without view model but I need the idea to implement it with view model !我知道如何在没有视图 model 的情况下实现它,但我需要用视图 model 来实现它的想法!

I just want rough idea of steps to implement this not the code.我只想粗略地了解实现这个而不是代码的步骤。

You have to make local pagination from firebase data array.您必须从 firebase 数据数组进行本地分页。 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
            }
          }
        }
      }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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