我有一个很好的问题,试图将数据从firebase数据库分页数周,现在都无济于事。我尝试了从android文档到SO的所有示例,但仍然无法正常工作。下面是我的数据节点Firebase数据库分页升序
下面的截图是我的代码,我使用recycleview进行连续滚动。
commentDatabaseReference = FirebaseDatabase.getInstance().getReference().child(CHANNEL_COMMENT).child(postId);
commentDatabaseReference.orderByKey().limitToLast(TOTAL_ITEM_EACH_LOAD).addListenerForSingleValueEvent(commentValueEventListener);
ValueEventListener commentValueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.hasChildren()) {
currentPage--;
}
int counter = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
commentId = snapshot.getKey();
Comment comment = snapshot.getValue(Comment.class);
commentList.add(comment);
adapter.notifyDataSetChanged();
}
//adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
Recycleview连续滚动收听
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
Log.e("more", "onload");
loadMoreData();
}
});
private void loadData() {
Log.e("loading", ""+ commentId);
commentDatabaseReference.orderByChild("timestamp").startAt(commentId).limitToLast(TOTAL_ITEM_EACH_LOAD)
.addListenerForSingleValueEvent(commentValueEventListener);
}
private void loadMoreData(){
//if ((currentPage * TOTAL_ITEM_EACH_LOAD) <= commentCount) {
currentPage++;
loadData();
//}
}
结论:举例来说,如果我有20个数据与10偏移和我用排序依据与limitToLast我会在顶部开始的最后10个数据,即我20,而不是20 - - 11获得数据11.也注意到有的时候我无休止的数据时,我结合了startAt
谢谢@faruk我会试试我t出来,并给你反馈 – Prodigy
我试过上面的,我能够从下面获取数据,但是当我到达顶部它继续拉数据不间断 – Prodigy