0

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();
}
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • The `progressBar` already visible in layout.. what you need to do is to hide it when the RecyclerView adapter receives data .. can you provide the `carAdapter` code? – Zain Feb 08 '21 at 12:20
  • Simply override onDataChanged method si dismiss the ProgressBar. – Alex Mamo Feb 08 '21 at 12:23
  • My question is how can I access in carAdapter.java the progressbar with id progressBarHome from layout "fragment_home.xml"? I researched a little bit and someone spoke about Inflate but I don't know in which method should I use the inflator or how to make it possible. Thanks in advance. – Maris Bogdan Feb 08 '21 at 13:12

1 Answers1

0

I don't think you want to make it GONE as soon as Fragment is created, to show a ProgressBar you should make it VISIBLE inside onViewCreated instead. Then once your firebase data is fetched, make it GONE to hide it.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    progressBar = view.findViewById(R.id.progressBarHome);
    progressBar.setVisibility(View.VISIBLE);
}
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37