How to dismiss a progress bar even if there is no view to populate in the FirebaseListAdapter?

假装没事ソ 提交于 2019-11-26 12:17:38

问题


I use the FirebaseUI FirebaseListAdapter. It takes some time to load the data and I want to show a spinning circle. I can dismiss the progress bar in by setting the view visibility to invisible in the populateView, but it doesn\'t work if there is no view to populate. How to handle this?


回答1:


Update: the FirebaseUI adapters nowadays have an onDataChanged() method that you can override to detect when they're done loading a set of data.

See the source code on github. From there:

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.

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.

The FirebaseUI sample app overrides onDataChanged() to hide its "loading" indicator:

public void onDataChanged() {
    // If there are no chat messages, show a view that invites the user to add a message.
    mEmptyListMessage.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
}

Original answer

The FirebaseUI list adapters internally use a Firebase ChildEventListener. This listener only fires for relevant child events. If there are no children, no event will fire.

You can detect this situation by attaching an additional value listener to the reference/query that you pass into the adapter.

DatabaseReference list = mDatabase.child("messages");
showSpinner();
mAdapter = new FirebaseListAdapter<Message>(......, list) {
    void populateView(View view, Message message, int position) {
        // the first message was loaded
        hideSpinner();
    }
});
list.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // the initial data is loaded (even if there was none)
        hideSpinner();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});


来源:https://stackoverflow.com/questions/40201574/how-to-dismiss-a-progress-bar-even-if-there-is-no-view-to-populate-in-the-fireba

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