问题
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