从GitHub friendly eat app
中学习firebase firestore我想实现分页限制节点10
private static final int LIMIT = 10;
在firestore示例应用程序中,mAdapter加载数据/节点,如下所示
mFirestore = FirebaseFirestore.getInstance();
// Get ${LIMIT} restaurants
mQuery = mFirestore.collection("restaurants")
.orderBy("avgRating", Query.Direction.DESCENDING)
.limit(LIMIT);
// RecyclerView
mAdapter = new RestaurantAdapter(mQuery, this) {
@Override
protected void onDataChanged() {
// Show/hide content if the query returns empty.
if (getItemCount() == 0) {
mRestaurantsRecycler.setVisibility(View.GONE);
mEmptyView.setVisibility(View.VISIBLE);
} else {
mRestaurantsRecycler.setVisibility(View.VISIBLE);
mEmptyView.setVisibility(View.GONE);
}
}
@Override
protected void onError(FirebaseFirestoreException e) {
// Show a snackbar on errors
Snackbar.make(findViewById(android.R.id.content),
"Error: check logs for info.", Snackbar.LENGTH_LONG).show();
}
};
mRestaurantsRecycler.setLayoutManager(new LinearLayoutManager(this));
mRestaurantsRecycler.setAdapter(mAdapter);
// Filter Dialog
mFilterDialog = new FilterDialogFragment();
}
和
@Override
public void onStart() {
super.onStart();
// Start sign in if necessary
if (shouldStartSignIn()) {
startSignIn();
return;
}
// Apply filters
onFilter(mViewModel.getFilters());
// Start listening for Firestore updates
if (mAdapter != null) {
mAdapter.startListening();
}
}
关于firestore docs说要分页
// 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
// ...
}
});
结合上述代码,我应该如何实现paginate加载10个以上 滚动到回收站视图底部时要加载的节点
//使用查询分页
// ...
更新:我的工作基于关于Paginate query的firestore文档,并查看了possible duplicate of another question我没有让它完成工作
谢谢
答案 0 :(得分:0)
我在这里找到了解决方案但不是更好的解决方案,如果有更好的方法请发布
通过在加载更多节点之前保存RecyclerView状态并在增加限制后重新加载RecyclerView状态
add bl, 48
mov dl, bl
mov ah, 02h
int 21h
已更改为
private newsFeeds : Array<any> = ...
当recyclerView滚动到底部
时private static final int LIMIT = 10;
在限制之后加载节点时看起来很不寻常,但现在暂不行...... 是的我正在寻找加载更多没有缺陷的节点
答案 1 :(得分:0)
我已将FirestoreAdapter扩展为:
private Set<String> mIdentifier = new HashSet<>();
/**
* Extends the query to load even more data rows. This method will do nothing if the query has
* not yet been set.
* @param limit the new limit
*/
public void paginate(long limit) {
if (mQuery != null) {
if (mRegistration != null) {
mRegistration.remove();
mRegistration = null;
}
// Expect the query to stay the same, only the limit will change
mQuery = mQuery.limit(limit);
startListening();
}
}
setQuery(Query)
mIdentifier.clear()
方法中的标识符
onDocumentAdded(DocumentChange)
和onDocumentRemoved(DocumentChange)
,如下所示protected void onDocumentAdded(DocumentChange change) {
if (!mIdentifier.contains(change.getDocument().getId())) {
mSnapshots.add(change.getNewIndex(), change.getDocument());
mIdentifier.add(change.getDocument().getId());
notifyItemInserted(change.getNewIndex());
}
}
protected void onDocumentRemoved(DocumentChange change) {
mSnapshots.remove(change.getOldIndex());
mIdentifier.remove(change.getDocument().getId());
notifyItemRemoved(change.getOldIndex());
}