FirestorePagingAdapter in RecyclerView
Introduction : Every RecyclerView needs a Adapter to assign item and ViewHolder to it . If you using Firestore as your database then its to easy Create a Adapter using FirestorePagingAdapter. Here we create a demo App for better understanding .
Follow the Articles Step by Step :
First Connect Your App to Firebase and Past Jason file to your project
In your root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services Gradle plugin. Check that you have Google's Maven repository, as well.
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
// ...
// Add the following line:
classpath 'com.google.gms:google-services:4.3.5' // Google Services plugin
}
}
allprojects {
// ...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
// ...
}
}
Add a Plugin to your Gridle.Build app level :
apply plugin: 'com.google.gms.google-services' // Google Services pluginAdd Some Dependency to your Gridle.Build App Level
implementation 'com.google.android.material:material:1.2.0'
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:26.5.0')
// When using the BoM, you don't specify versions in Firebase library dependencies
// Declare the dependency for the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-firestore'
// FirebaseUI for Cloud Firestore
implementation 'com.firebaseui:firebase-ui-firestore:7.1.1'
implementation 'androidx.paging:paging-runtime:2.1.2'
Create Firestore Database And Add Some Data like this
There are 3 filed Added name, profession and timestamps . We show two data in our item name and profession and use timestamps to order by newer data.
Now Design activity_main:
acticity_main.xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/feeds"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Create a Layout Recourse File named item :
item.xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
android:padding="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/professionTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profession"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nameTv" />
</androidx.constraintlayout.widget.ConstraintLayout>
Create a java class Named Model :
Model.java code:
public class Model {
private String name,profession;
private Date timestamps;
public Model() {
}
public Model(String name, String profession) {
this.name = name;
this.profession = profession;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
@ServerTimestamp
public Date getmTimestamp(){return timestamps;}
public void setmTimestamp(Date timestamp){timestamps=timestamp;}
}
Create a new java class named ViewHolder :
ViewHolder.java :
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView name,profession;
View view;
public ViewHolder(@NonNull View itemView) {
super(itemView);
view=itemView;
}
public void setView(String Name,String Profession) {
name =view.findViewById(R.id.nameTv);
profession =view.findViewById(R.id.professionTv);
name.setText(Name);
profession.setText(Profession);
}
}
MainActivity.java :
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private ProgressBar progressBar;
private FirestorePagingAdapter<Model,ViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
Query query = FirebaseFirestore.getInstance()
.collection("users")
.orderBy("timestamps", Query.Direction.DESCENDING);
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(6)
.setPageSize(5)
.build();
FirestorePagingOptions<Model> options = new FirestorePagingOptions.Builder<Model>()
.setLifecycleOwner(this)
.setQuery(query, config, Model.class)
.build();
adapter = new FirestorePagingAdapter<Model, ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) {
holder.setView(model.getName(),model.getProfession());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false));
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
super.onLoadingStateChanged(state);
switch (state) {
case LOADING_INITIAL:
// The initial load has
swipeRefreshLayout.setRefreshing(true);
// ...
break;
case LOADING_MORE:
// The adapter has started to load an additional page
progressBar.setVisibility(View.VISIBLE); // ...
break;
case LOADED:
// The previous load (either initial or additional) completed
swipeRefreshLayout.setRefreshing(false);
progressBar.setVisibility(View.INVISIBLE); // ...
// ...
break;
case FINISHED:
progressBar.setVisibility(View.INVISIBLE); // ...
break;
case ERROR:
// The previous load (either initial or additional) failed. Call
adapter.retry();
swipeRefreshLayout.setRefreshing(true);
// the retry() method in order to retry the load operation.
// ...
break;
}
}
};
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
adapter.refresh();
swipeRefreshLayout.setRefreshing(false);
}
});
}
private void init() {
swipeRefreshLayout=findViewById(R.id.swipe);
recyclerView=findViewById(R.id.feeds);
progressBar =findViewById(R.id.progressBar);
}
}
Demo app now completed.
Thank You
Keep Learning
Keep Growing

