【问题标题】:Paginate the Firestore Values in Order按顺序对 Firestore 值进行分页
【发布时间】:2023-03-05 07:56:01
【问题描述】:

我有这个 Cloud Firestore 数据库 并使用它来访问它:

CollectionReference robDetails = db.collection("ROBOTS").document(RobotA).collection(RobotB);

每个“Connections”都包含两个带值的字段(ConnectionType & ConnectionTime)。

我想加载 Connections 按 ConnectionTime 排序(底部最新的,如下图所示),但只想显示 3 个最后的连接,当用户向上滚动时,在前一个之上再显示 3 个在 SwipeRefreshLayout 的帮助下:

我目前要像上图一样加载所有连接的代码是:

 private void loadConnections() {
    CollectionReference loadConnect = db.collection("ROBOTS").document(RobotA).collection(RobotB);
    loadConnections.orderBy("connectionTime", Query.Direction.ASCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
            
            for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
                Connections connection = documentChange.getDocument().toObject(Connections.class);

                mConnectionsList.add(connection);
                mAdapter.notifyDataSetChanged();

                conRecyclerView.scrollToPosition(mConnectionsList.size() - 1);
                conSwipeRefresh.setRefreshing(false);
            }
        }
    });
}

TL;DR: 我想先在我的 RecyclerView 中加载 Part1,当用户向上滚动加载 Part2 时,当用户向上滚动加载 Part3... 时使用 SwipeRefreshLayout。

【问题讨论】:

    标签: java android android-recyclerview pagination google-cloud-firestore


    【解决方案1】:

    我基本上想出了一个分页逻辑,它检查当前日期并加载当天发生的连接,当用户向上滚动 SwipeRefreshLayout 时,当前日期减去 1,并且用户能够获得前一天的连接:

     conSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Calendar cal = Calendar.getInstance();
                //subtracting a day
                cal.add(Calendar.DATE, numberToSubtract);
                numberToSubtract--;
                SimpleDateFormat s = new SimpleDateFormat("dd-MM-yyyy");
                String day = s.format(new Date(cal.getTimeInMillis()));
    
                loadMoreConnections(day);
            }
        });
    

    private void loadMoreMessages(String day) {
        mMessagesList.clear();
        CollectionReference loadConnect = db.collection("ROBOTS").document(RobotA).collection(RobotB);
        loadConnect.orderBy("timestamp", Query.Direction.ASCENDING).startAt(day).endBefore(currentDay).addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Toast.makeText(ChatActivity.this, "Error:" + e.toString(), Toast.LENGTH_SHORT).show();
                }
                for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {
                    Connections connection = documentChange.getDocument().toObject(Connections.class);
    
                    mConnectionsList.add(connection);
                    mAdapter.notifyDataSetChanged();
    
                    conSwipeRefresh.setRefreshing(false);
                }
            }
        });
        loadConnections();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-09
      • 2019-11-14
      • 2021-09-05
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多