问题
I'm using the FirebaseListAdapter and I need to display a ProgressDialog, but I don't know how to know when it finishes loading.
public class TabFragment1 extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
ListView eventosView = (ListView) rootView.findViewById(android.R.id.list);
Firebase ref = new Firebase(//myappurl);
Firebase usersRef = new Firebase(//myappurl/eventos/users);
Singleton singletonUser = Singleton.getInstance();
setUser(singletonUser);
Firebase ref = new Firebase(//myappurl/eventos);
FirebaseListAdapter<Evento> mAdapter = new FirebaseListAdapter<Evento>(getActivity(), Evento.class, R.layout.event_row, ref) {
@Override
protected void populateView(View view, Evento evento) {
((TextView)view.findViewById(R.id.eventTitle)).setText(evento.getNome());
((TextView)view.findViewById(R.id.eventPlace)).setText(evento.getLocal());
}
};
setListAdapter(mAdapter);
return mAdapter;
return rootView;
}
private void setUser(Singleton singletonUser){
singletonUser.getToken();
singletonUser.setUuid();
singletonUser.getUuid();
}
}
When can I place the ProgressDialog.dismiss()?
回答1:
Firebase is built on the premise of synchronizing data. For this reason there is never really a moment when it is done synchronizing.
The initial set of data for a location is downloaded in one go and results in a call to onChildAdded()
for each child, followed by a single onDataChanged()
. If you want to hide the progress bar when this initial data has loaded, you can register a ValueEventListener
for a single event:
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// the initial data has been loaded, hide the progress bar
}
@Override
public void onCancelled(DatabaseError firebaseError) {
}
});
In the above snippet, all the children will have been added to your ListView
already. Depending on your use-case, this may be the right time to hide the progress bar or it may be later than wanted.
If you want to know when the first data has been rendered, you can register an observer with your adapter:
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
// the first time you get here, hide the progress bar
}
@Override
public void onInvalidated() {
super.onInvalidated();
}
});
You could use this approach to hide the progress bar as soon as the first data has arrived from Firebase.
回答2:
Since january 2017 FirebaseListAdapter
has the following method that can be overridden (commit by Frank on github):
/**
* This method will be triggered each time updates from the database have been completely processed.
* So the first time this method is called, the initial data has been loaded - including the case
* when no data at all is available. Each next time the method is called, a complete update (potentially
* consisting of updates to multiple child items) has been completed.
* <p>
* You would typically override this method to hide a loading indicator (after the initial load) or
* to complete a batch update to a UI element.
*/
protected void onDataChanged() {
}
来源:https://stackoverflow.com/questions/34982347/how-to-be-notified-when-firebaselistadapter-finishes