Java 按顺序对Firestore值进行分页
我有这个云Firestore数据库&要访问它,我使用:Java 按顺序对Firestore值进行分页,java,android,android-recyclerview,pagination,google-cloud-firestore,Java,Android,Android Recyclerview,Pagination,Google Cloud Firestore,我有这个云Firestore数据库&要访问它,我使用: CollectionReference robDetails = db.collection("ROBOTS").document(RobotA).collection(RobotB); 每个“连接”都包含两个带值的字段(ConnectionType和ConnectionTime) 我想按ConnectionTime加载连接排序(最新的在底部,如下图所示),但只想显示最后的3个连接,当用户向上滚动时,在SwipeRe
CollectionReference robDetails = db.collection("ROBOTS").document(RobotA).collection(RobotB);
每个“连接”都包含两个带值的字段(ConnectionType和ConnectionTime)
我想按ConnectionTime加载连接排序(最新的在底部,如下图所示),但只想显示最后的3个连接,当用户向上滚动时,在SwipeRefreshLayout的帮助下,在前一个连接的基础上再显示3个:
我当前必须加载所有连接的代码,如上图所示:
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);
}
}
});
}
private void loadConnections(){
CollectionReference loadConnect=db.collection(“机器人”).document(RobotA.collection(RobotB);
loadConnections.orderBy(“connectionTime”,Query.Direction.升序)。addSnapshotListener(新的EventListener()){
@凌驾
public void OneEvent(@Nullable QuerySnapshot queryDocumentSnapshots,@Nullable FirebaseFirestoreException e){
对于(DocumentChange DocumentChange:queryDocumentSnapshots.getDocumentChanges()){
Connections connection=documentChange.getDocument().toObject(Connections.class);
mConnectionsList.add(连接);
mAdapter.notifyDataSetChanged();
conRecyclerView.scrollToPosition(mcConnectionsList.size()-1);
conswitrefresh.setRefreshing(false);
}
}
});
}
TL;医生:
我想先在我的RecyclerView中加载Part1,当用户向上滚动加载Part2时,当用户向上滚动加载Part3时。。。使用SwipeRefreshLayout。我基本上想出了一个分页逻辑,检查当前日期并加载当天发生的连接,当用户向上滚动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 loadmore消息(字符串日){
mMessagesList.clear();
CollectionReference loadConnect=db.collection(“机器人”).document(RobotA.collection(RobotB);
loadConnect.orderBy(“timestamp”,Query.Direction.ASCENDING).startAt(day).endBefore(currentDay).addSnapshotListener(new EventListener()){
@凌驾
public void OneEvent(@Nullable QuerySnapshot queryDocumentSnapshots,@Nullable FirebaseFirestoreException e){
如果(e!=null){
Toast.makeText(ChatActivity.this,“错误:+e.toString(),Toast.LENGTH_SHORT).show();
}
对于(DocumentChange DocumentChange:queryDocumentSnapshots.getDocumentChanges()){
Connections connection=documentChange.getDocument().toObject(Connections.class);
mConnectionsList.add(连接);
mAdapter.notifyDataSetChanged();
conswitrefresh.setRefreshing(false);
}
}
});
加载连接();
}
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();
}