Cavad
Cavad

Reputation: 75

How can I add progressbar before the load data from firebase and dismiss this progressbar after loaded data from firebase with firebasercycler adapter

I was created one application, in this application I used firebase and firebaserecycler adapter. I can load data, it works fine. But I want add progressbar until data is loaded and dismiss it after the data is loaded. Is anyone have some experience with this?

My Fragment is this //

    public class CategoryFragment extends Fragment {
    View view;

    RecyclerView listCategory;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;

    FirebaseDatabase database;
    DatabaseReference categories;

    public static CategoryFragment newInstance(){
    CategoryFragment categoryfragment = new CategoryFragment();
    return categoryfragment;
    }

    public CategoryFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    database = FirebaseDatabase.getInstance();
    categories = database.getReference("Category");

    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup 
    container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_category, container, 
    false);

    listCategory = view.findViewById(R.id.listCategory);
    listCategory.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(container.getContext());
    listCategory.setLayoutManager(layoutManager);

    loadCategories();

    return view;
    }

    private void loadCategories(){
    adapter =  new FirebaseRecyclerAdapter<Category, CategoryViewHolder>
       (
            Category.class,
            R.layout.category_layout,
            CategoryViewHolder.class,
            categories
       ) {
        @Override
        protected void populateViewHolder(CategoryViewHolder viewHolder, 
        final Category model, int position) {
            viewHolder.category_name.setText(model.getName());

            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean 
         isLongClick) {
                    Intent game = new Intent(getActivity(),Start.class);
                    Common.categoryId = 
         adapter.getRef(position).getKey();
                    startActivity(game);
                }
            });
        }
       };
       adapter.notifyDataSetChanged();
       listCategory.setAdapter(adapter);
       }
       }

My xml file is this //

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".CategoryFragment">

  <android.support.v7.widget.RecyclerView
    android:id="@+id/listCategory"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </android.support.v7.widget.RecyclerView>

  </FrameLayout>

Upvotes: 0

Views: 429

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Because you are using a FirebaseRecyclerAdapter, this can be solved very simple. Create a new object of ProgressBar and start showing it in the onCreate() method, or if you want, add it directly in your .XML file. Finally in your adapter class, override the following method:

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

P.S. You are using a very old version of the Firebase-UI library. Please update to the latest version.

Upvotes: 2

Related Questions