How to use FirebaseRecyclerPagingAdapter in RecyclerView in Android Studio ?

0

 FirebasePagingAdapter in RecyclerView

First Connect Your App with Firebase And Get start with Firebase Realtime Database , then Add some Dependency  in Your Gradle.build app level file .

implementation 'com.google.android.material:material:1.2.0'
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:26.5.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-database'
// When using the BoM, you don't specify versions in Firebase library dependencies
// Declare the dependency for the Firebase SDK for Google Analytics

// FirebaseUI for Firebase Realtime Database
implementation 'com.firebaseui:firebase-ui-database:7.1.1'
implementation 'androidx.paging:paging-runtime:2.1.2'

Put some data to Realtime data base. like this

here we add two type data "name" and "profession". 

Now we have to access this data from firebase by a model class into our project. 

So, Create a java class named "Model"

Model.java :

public class Model {

private String name,profession;


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


}

Now need a item for our recycler item layout. So, Create a layout recourse file named "item"

item.xml :

<?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>

Now Create a ViewHolder class to Initialize views and add data to views .

So , Create a 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);
}
}

activity_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>

MainActivity.java code :

public class MainActivity extends AppCompatActivity {

private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private ProgressBar progressBar;
private FirebaseRecyclerPagingAdapter<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= FirebaseDatabase.getInstance().getReference("users");

PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(6)
.setPageSize(5)
.build();

DatabasePagingOptions<Model> options = new DatabasePagingOptions.Builder<Model>()
.setLifecycleOwner(this)
.setQuery(query, config, Model.class)
.build();


adapter = new FirebaseRecyclerPagingAdapter<Model, ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) {
holder.setView(model.getName(),model.getProfession());

}

@Override
protected void onLoadingStateChanged(@NonNull LoadingState 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;
}

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false));
}
};

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

}

Now Demo App is Completed . If you have any problem please Comments bellow

 Thank you 

                        Keep Learning

                                                            Keep Growing

Post a Comment

0Comments
Post a Comment (0)