Page splitting in endless view by recycler using firebase

I am working on a Q / A application. I have successfully downloaded questions from firebase. But I can't seem to apply pagination from Firebase database. And how to recognize when we have reached the end of the recycler scan so that the next few questions will load.

+3


source to share


3 answers


To understand that we have reached the end RecyclerView

, you can use this EndlessRecyclerOnScrollListener.java class

To load the next question you have to 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
    ...
}

      



Then when you download the questions from FireBase

you can do for example

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

      

Here is my DEMO project

+7


source


To check if you have reached the bottom of the RecyclerView you can use the onScrolled listener as shown below, the if condition is important here and it determines when the user has hit the bottom.

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

      

Second, you can use methods startAt

and limitToFirst

to get questions in batches, as shown below:



query = FirebaseDatabase.getInstance().getReference()
                    .child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
                    .orderByKey()
                    .startAt(nodeId)
                    .limitToFirst(mPostsPerPage);

      

I've created an open source app that shows exactly how it's done. Please take a look: https://blog.shajeelafzal.com/2017/12/13/firebase-realtime-database-pagination-guide-using-recyclerview/

+2


source


0


source







All Articles