Ad

How To Add Progressbar For Loading In Firebase RecyclerView

I am developing an Android app The App fetches the data from Firebase using FirebaseRecyclerAdapter and I need to implement a ProgressBar in that Recyler View.

How to add RrogressBar in RecyclerView before the data is loaded..and after the data is loaded the loading screen need to be Dismiss

code:

protected void onStart() {
    super.onStart();

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Vacancy");

    FirebaseRecyclerOptions<VacancyModel> options = new FirebaseRecyclerOptions.Builder<VacancyModel>()
            .setQuery(reference.orderByChild("companyname"), VacancyModel.class)
            .build();


    FirebaseRecyclerAdapter<VacancyModel, VacancyViewHolder> adapter =
            new FirebaseRecyclerAdapter<VacancyModel, VacancyViewHolder>(options) {

                @Override
                protected void onBindViewHolder(@NonNull VacancyViewHolder holder, int position, @NonNull final VacancyModel model) {

                    jprog.setVisibility(View.VISIBLE);


                    holder.vacompany_name.setText(model.getCompanyname());
                    holder.vajob_type.setText("Job Title: " + model.getJob_title());
                    holder.vatiming.setText(model.getTiming());
                    holder.vatotal_vacancy.setText("Total Vacancy: " +model.getTotal_vacancy());
                    holder.vasp.setText(model.getS_p());
                    holder.vaexpected_vasalary.setText("Expected Salary: " +model.getExpexted_salary());
                    holder.vaarea.setText("Area: " +model.getPincode());
                    holder.vaphone_number.setText("Contact Number: " +model.getContact_number());



                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            Intent intent = new Intent(Home.this, VacancyViewActivity.class);
                            intent.putExtra("pid", model.getCompanyID());
                            startActivity(intent);

                        }

                    });
                }


                @NonNull
                @Override
                public VacancyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.vacancy_list_item, viewGroup, false);
                    VacancyViewHolder holder = new VacancyViewHolder(view);
                    return holder;
                }

            };

    recyclerView.setAdapter(adapter);
    adapter.startListening();


}
Ad

Answer

Because you are using FirebaseRecyclerAdapter, you can override the onDataChanged() method in your adapter class. So create a new object of ProgressBar and start showing it in the onCreate() method, or if you want, you can add it directly in your .XML file. Finally in your adapter class, use the following lines of code:

@Override
public void onDataChanged() {
    if (progressBar != null) {
        progressBar.setVisibility(View.GONE);
    }
}
Ad
source: stackoverflow.com
Ad