我查看了https://firebase.google.com/docs/firestore/query-data/query-cursors的文档
我想让FirestoreRecyclerAdapter
与firestore分页合作。
有没有人有这个用例的示例工作代码?我有一个列表,该列表可能很长。我想设置一个限制并遵循策略,通过将查询游标与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 doc讨论了回收器适配器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
你可以做到这一点。这很简单,而不是使用FirestoreRecyclerAdapter。您只需要使用FirestorePagingAdapter。它还支持firestoreUi。
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setPageSize(20)
.build();
通过使用此代码,您可以定义第一次加载时要加载的项目数,以及从DataSource一次加载的项目数。而已。你可以知道更多from here
FirestoreRecyclerAdapter和FirestorePagingAdapter之间存在细微差别