So I have 3
fragments which can be accessed from a bottom navigation menu. When I select "Home", it opens a fragment
which contains a RecyclerView
populated with data from Firebase Database. How can I display a progressbar
while the recyclerview
gets its data from firebase? It took ~1-2 seconds and I want to have a progressbar
.
EDIT: I made a progressbar
but if I set it's visibility to GONE after the fragment is created, the progressbar
doesn't appear at all.
XML FILE:
<?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">
<ProgressBar
android:id="@+id/progressBarHome"
android:layout_width="100dp"
android:layout_height="39dp"
android:elevation="7dp"
android:visibility="visible"
android:indeterminateTint="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Java file:
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
carAdapter adapter; // Create Object of the Adapter class
DatabaseReference mbase;
private ProgressBar progressBar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container,false);
mbase = FirebaseDatabase.getInstance().getReference("Cars");
recyclerView = view.findViewById(R.id.recycler1);
// To display the Recycler view linearly
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// It is a class provide by the FirebaseUI to make a
// query in the database to fetch appropriate data
FirebaseRecyclerOptions<car> options = new FirebaseRecyclerOptions.Builder<car>()
.setQuery(mbase, car.class)
.build();
// Connecting object of required Adapter class to
// the Adapter class itself
adapter = new carAdapter(options);
// Connecting Adapter class with the Recycler view*/
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
progressBar = view.findViewById(R.id.progressBarHome);
progressBar.setVisibility(View.GONE);
}
@Override public void onStart()
{
super.onStart();
adapter.startListening();
}
// Function to tell the app to stop getting
// data from database on stoping of the activity
@Override public void onStop()
{
super.onStop();
adapter.stopListening();
}
}