我正在开发Q / A应用程序。我已成功加载firebase的问题。但我无法从Firebase中应用分页数据库。如何识别我们已经到达了回收站视图的末尾,以便可以加载下几个问题。
答案 0 :(得分:7)
要认识到我们已到达RecyclerView
的末尾,您可以使用此课程EndlessRecyclerOnScrollListener.java
要加载更多下一个问题,您应该在Question class
中再定义一个字段,如数字
public class Question {
private int number; // it must unique and auto increase when you add new question
...
}
然后当您从FireBase
加载问题时,您可以这样做
public class MainActivity extends AppCompatActivity {
private static final int TOTAL_ITEM_EACH_LOAD = 10;
private DatabaseReference mDatabase;
final List<Question> questionList = new ArrayList<>();
private int currentPage = 0;
private RecyclerView recyclerView;
private RecyclerViewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// init and set layout manager for your RecyclerView
...
mAdapter = new RecyclerViewAdapter(questionList);
recyclerView.setAdapter(mAdapter);
recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
@Override
public void onLoadMore(int current_page) { // when we have reached end of RecyclerView this event fired
loadMoreData();
}
});
loadData(); // load data here for first time launch app
}
private void loadData() {
// example
// at first load : currentPage = 0 -> we startAt(0 * 10 = 0)
// at second load (first loadmore) : currentPage = 1 -> we startAt(1 * 10 = 10)
mDatabase.child("questions")
.limitToFirst(TOTAL_ITEM_EACH_LOAD)
.startAt(currentPage*TOTAL_ITEM_EACH_LOAD)
.orderByChild("number")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.hasChildren()){
Toast.makeText(MainActivity.this, "No more questions", Toast.LENGTH_SHORT).show();
currentPage--;
}
for (DataSnapshot data : dataSnapshot.getChildren()) {
Question question = data.getValue(Question.class);
questionList.add(question);
mAdapter.notifyDataSetChanged();
}
}
@Override public void onCancelled(DatabaseError databaseError) {}});
}
private void loadMoreData(){
currentPage++;
loadData();
}
}
答案 1 :(得分:1)
要检查您是否已到达RecyclerView的底部,您可以使用onScrolled监听器,如下所示,此处的if条件很重要,并且定义用户何时到达底部。
mRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mTotalItemCount = mLayoutManager.getItemCount();
mLastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition();
if (!mIsLoading && mTotalItemCount <= (mLastVisibleItemPosition + mPostsPerPage)) {
getUsers(mAdapter.getLastItemId());
mIsLoading = true;
}
}
});
其次,您可以使用startAt
和limitToFirst
方法分批获取问题,如下所示:
query = FirebaseDatabase.getInstance().getReference()
.child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
.orderByKey()
.startAt(nodeId)
.limitToFirst(mPostsPerPage);
我创建了一个开源应用程序,可以准确显示它是如何完成的。请看一下:https://blog.shajeelafzal.com/2017/12/13/firebase-realtime-database-pagination-guide-using-recyclerview/
答案 2 :(得分:0)
您可以使用Firebase Queries和FirebaseRecyclerAdapter
为startAt
创建limitToFirst
,以便批量下载记录。
limitToFirst
页面大小会因事件而增加,例如点击或pull to refresh。