使用Firebase在无休止的回收者视图中进行分页

问题描述:

我正在使用Q/A应用程序.我已经成功地从firebase加载了问题.但是我无法从Firebase像数据库那样应用分页.以及如何识别我们已经到达了回收站视图的末端,以便可以加载下几个问题.

I am working on Q/A app . I have successfully loaded questions from firebase . But I am not able to apply pagination from Firebase like database . And how to recognize that we have reached end of recycler view so that next few questions can loaded .

要认识到我们已经到了RecyclerView的结尾,可以使用此类

To recognize that we have reached end of RecyclerView you can use this class EndlessRecyclerOnScrollListener.java

要加载更多下一个问题,您应该在Question class中再定义一个字段,例如数字

To load more next question, you should define one more field in Question class like number

public class Question {
    private int number; // it must unique and auto increase when you add new question
    ...
}

然后,当您从FireBase加载问题时,您会喜欢

Then when you load questions from FireBase you can do like

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();
    }
}

这是我的DEMO项目