我正在使用以下示例(数据库)来练习我的Android开发技能。
https://github.com/firebase/quickstart-android/tree/master/database
当我滚动到帖子列表的底部时,我想实现一个自动加载更多功能。如何在MyPostsFragment中初始查询后加载更多数据?这是我的代码:我在recyclerview上添加了一个scrolllistener,当它到达列表的底部时,我的函数loadData()查询
PostListFragment.java:
private final int THREAT_SHOT = 5;
mRecycler.setAdapter(mAdapter);
mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if( mManager.getItemCount() - THREAT_SHOT == mManager.findLastVisibleItemPosition() - 1){
loadMoreData();
}
}
});
}
private void loadMoreData() {
mDatabase.child("user-posts").child(getUid()).orderByKey().startAt(oldestPostId).limitToFirst(5).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
oldestPostId = child.getKey(); ////HERE WE ARE SAVING THE LAST POST_ID FROM URS POST
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
MyPostsFragment.java:
@Override
public Query getQuery(DatabaseReference databaseReference) {
// All my posts
return databaseReference.child("user-posts").child(getUid()).orderByKey().limitToFirst(5);
}