How to be notified when FirebaseListAdapter finishes?

前端 未结 2 876
野的像风
野的像风 2021-01-07 11:57

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 TabFragme         


        
相关标签:
2条回答
  • 2021-01-07 12:30

    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.

    0 讨论(0)
  • 2021-01-07 12:52

    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() {
        }
    
    0 讨论(0)
提交回复
热议问题