如何添加进度条以在Firebase RecyclerView中加载

时间:2019-08-15 09:30:51

标签: java android firebase firebase-realtime-database firebaseui

我正在开发一个Android应用。该应用使用FirebaseRecyclerAdapter从Firebase获取数据,并且我需要在ProgressBar中实现一个Recyler View

  

如何在加载数据之前在RecyclerView中添加RrogressBar。   数据加载后,需要关闭加载屏幕

代码:

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


}

1 个答案:

答案 0 :(得分:2)

由于使用的是FirebaseRecyclerAdapter,因此可以在适配器类中覆盖onDataChanged()方法。因此,创建一个ProgressBar的新对象并开始在onCreate()方法中显示它,或者,如果需要,可以将其直接添加到.XML文件中。最后,在您的适配器类中,使用以下代码行:

@Override
public void onDataChanged() {
    if (progressBar != null) {
        progressBar.setVisibility(View.GONE);
    }
}