How to implement ProgressBar in FireBaseHandler

时光毁灭记忆、已成空白 提交于 2019-12-25 01:33:36

问题


I have the problem, Fragment has content and I want to add simple ProgressBar while of Firebase loading content. How can I do this and where ? (I try to add in onDataChange but it dosn't work, code below)

public void onDataChange(DataSnapshot dataSnapshot) {
            progressBar.setVisibility(View.VISIBLE);
            mContentLayout.setVisibility(LinearLayout.GONE);

            for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                item = snapshot.getValue(Item.class);
                if(item.isSales()) {
                    arrayOfItemProduct.add(item);
                }
            }
            adapter.notifyDataSetChanged();

            progressBar.setVisibility(View.GONE);
            mContentLayout.setVisibility(LinearLayout.VISIBLE);
        }

回答1:


To sovle this, you can add in your .XML layout file of your fragment the following lines of code:

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progress_bar"
    android:layout_centerInParent="true"
    style="@style/Widget.AppCompat.ProgressBar"/>

This means that everytime you start your fragment the ProgressBar will show up.

Define the progressBar variable as a global variable and then find it in your fragmnet inside the onCreateView() method like this:

progressBar = yourFragmentView.findViewById(R.id.progress_bar);

And then inside your onDataChange() method use the following code:

if (progressBar != null) {
    progressBar.setVisibility(View.GONE);
}


来源:https://stackoverflow.com/questions/50624746/how-to-implement-progressbar-in-firebasehandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!