How to create Pagination with Android Firebase Database

In this android programming source code example, we are going to create Pagination with Android Firebase Database.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create Pagination with Android Firebase Database.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_display"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.shreyaspatil.firebase.recyclerpagination.DatabasePagingOptions;
import com.shreyaspatil.firebase.recyclerpagination.FirebaseRecyclerPagingAdapter;
import com.shreyaspatil.firebase.recyclerpagination.LoadingState;

public class FirebaseActivity9 extends AppCompatActivity {
    private RecyclerView list;
    private DatabaseReference mDatabase;
    private FirebaseRecyclerPagingAdapter<Data, DataViewHolder> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firebase9);
        list = (RecyclerView) findViewById(R.id.list);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("AndroidView");
        list.setHasFixedSize(true);
        LinearLayoutManager mManager = new LinearLayoutManager(this);
        list.setLayoutManager(mManager);
        PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPrefetchDistance(5)
                .setPageSize(10)
                .build();
        DatabasePagingOptions<Data> options = new DatabasePagingOptions.Builder<Data>()
                .setLifecycleOwner(this)
                .setQuery(mDatabase, config, Data.class)
                .build();

        mAdapter = new FirebaseRecyclerPagingAdapter<Data, DataViewHolder>(options) {
            @NonNull
            @Override
            public DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new DataViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cardview, parent, false));
            }
            @Override
            protected void onBindViewHolder(@NonNull DataViewHolder holder, int position, @NonNull Data model) {
                holder.setItem(model);
            }
            @Override
            protected void onLoadingStateChanged(@NonNull LoadingState state) {
                switch (state) {
                    case LOADING_INITIAL:
                    case LOADING_MORE:
                        // Do your loading animation
                        Toast.makeText(FirebaseActivity9.this,"Loading..",Toast.LENGTH_LONG).show();
                        break;
                    case LOADED:
                        // Stop Animation
                        break;
                    case FINISHED:
                        //Reached end of Data set
                        Toast.makeText(FirebaseActivity9.this,"End of data..",Toast.LENGTH_LONG).show();
                        break;
                    case ERROR:
                        break;
                }
            }
            @Override
            protected void onError(@NonNull DatabaseError databaseError) {
                super.onError(databaseError);
                databaseError.toException().printStackTrace();
            }
        };

        //Set Adapter to RecyclerView
        list.setAdapter(mAdapter);

        }

    //Start Listening Adapter
    @Override
    protected void onStart() {
        super.onStart();
        mAdapter.startListening();
    }

    //Stop Listening Adapter
    @Override
    protected void onStop() {
        super.onStop();
        mAdapter.stopListening();
    }

    public class DataViewHolder extends RecyclerView.ViewHolder {
        TextView textViewTitle;
        TextView textViewContent;
        public DataViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.title);
            textViewContent = itemView.findViewById(R.id.content);
        }
        public void setItem(Data data){
            textViewTitle.setText(data.getTitle());
            textViewContent.setText(data.getContent());
        }
    }

}

Data.java

public class Data {
    public String title, content;

    public Data(){

    }

    public Data(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

}

list_cardview.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="3dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f1f1f1">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textColor="@color/colorAccent"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textSize="18sp"
            android:lineSpacingMultiplier="0.6"
            android:layout_marginTop="10dp"
            android:textStyle="bold" />

        <View
            android:id="@+id/line1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorAccent"
            android:layout_below="@id/title"/>

        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"
            android:textSize="15sp"
            android:textColor="#2b2b2b"
            android:layout_marginBottom="10dp"
            android:layout_below="@id/line1"/>
    </RelativeLayout>

</androidx.cardview.widget.CardView>

app/build.gradle

implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'com.firebaseui:firebase-ui-database:4.3.1'
implementation 'androidx.paging:paging-runtime:2.1.0'
implementation 'com.shreyaspatil:FirebaseRecyclerPagination:1.0.1'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.