【发布时间】:2018-09-15 18:06:40
【问题描述】:
我已经查看了https://firebase.google.com/docs/firestore/query-data/query-cursors 的文档
我想让 Firestore 分页与 FirestoreRecyclerAdapter 一起工作。
有人有这个用例的示例工作代码吗?我有一个列表,该列表可能很长。我想设置一个限制并遵循通过将查询游标与 limit() 方法组合来对查询进行分页的策略。
到目前为止,这就是我的 ListActivity 中的内容:
// Construct query for first 25 teachers, ordered by firstName
firstQuery = FirebaseFirestore.getInstance()
.collection("School").document(user.getUid())
.collection("teachers")
.orderBy("firstName")
.limit(25);
FirestoreRecyclerOptions<Teacher> options = new FirestoreRecyclerOptions.Builder<Teacher>()
.setQuery(firstQuery, Teacher.class)
.build();
adapter = new FirestoreRecyclerAdapter<Teacher, TeacherHolder>(options) {
@Override
public void onBindViewHolder(final TeacherHolder holder, final int position, Teacher teacherItem) {
// Bind the Teacher object to the TeacherHolder
//progressBar.setVisibility(View.GONE);
....
}
要实施 firestore 文档给出的策略,下一步我该如何进行。
// Construct query for first 25 cities, ordered by population
Query first = db.collection("cities")
.orderBy("population")
.limit(25);
first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// ...
// Get the last visible document
DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);
// Construct a new query starting at this document,
// get the next 25 cities.
Query next = db.collection("cities")
.orderBy("population")
.startAfter(lastVisible)
.limit(25);
// Use the query for pagination
// ...
}
});
如何在使用 FirestoreRecyclerAdapter 时将 firestore 给出的上述建议连接到我的应用程序中。我是否将 scrollListener 添加到上面的适配器?并监听滚动事件,重新运行查询。将所有这些联系在一起的示例代码将帮助我清除完成这一切所需的布线。
我确实查看了围绕这个主题的其他一些聊天,我发现最接近的是https://github.com/Malik333/RecyclerviewFirestorePagination,但这并没有使用我想要使用的 FirestoreRecyclerAdapter。
讨论回收器适配器的 Firestore 文档 https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorerecycleradapter
(但关于如何将其与分页联系起来的信息不多)。
我在想也许我需要做一些类似于this的事情
但正在寻找与 FirestoreRecyclerAdapter 代码的集成。
我正在考虑从
开始myRecyclerView.setOnScrollChangeListener(new EndlessScrollListener() {
@Override
public boolean onLoadMore(int page, int totalItemsCount) {
// Triggered only when new data needs to be appended to the list
// Add whatever code is needed to append new items to your AdapterView
//loadNextDataFromApi(page);
// or loadNextDataFromApi(totalItemsCount);
return true; // ONLY if more data is actually being loaded; false otherwise.
}
});
并遵循本教程中概述的步骤 https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView
【问题讨论】:
-
这个问题你解决了吗?
-
好吧,我发现无法使用 FirebaseRecylerAdapter,而是使用了 github.com/Malik333/RecyclerviewFirestorePagination/tree/master/…,这确实有效。
-
感谢您的回复。同时,我也问了一个问题,我得到了这个answer。也许将来会有所帮助。
标签: android firebase android-recyclerview google-cloud-firestore infinite-scroll