即使FirebaseListAdapter中没有视图,也要关闭进度条? [英] How to dismiss a progress bar even if there is no view to populate in the FirebaseListAdapter?

查看:156
本文介绍了即使FirebaseListAdapter中没有视图,也要关闭进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FirebaseUI FirebaseListAdapter。加载数据需要一些时间,我想显示一个旋转的圆圈。通过将视图可见性设置为在populateView中不可见,我可以关闭进度条,但如果没有视图来填充,则不起作用。如何处理这个问题?

解决方案

更新:现在的FirebaseUI适配器有一个 onDataChanged()方法,您可以覆盖以检测何时加载一组数据。

请参阅 github上的源代码。从那里:


每次数据库更新完成后,都会触发此方法。所以第一次调用这个方法时,初始数据已经被加载 - 包括根本没有数据可用的情况。每次下一次该方法被调用时,一个完整的更新(可能包含对多个子项目的更新)已经完成。

您通常会覆盖此方法以隐藏加载指示器(初始加载后)或完成对UI元素的批量更新。

FirebaseUI示例应用程序覆盖 onDataChanged()来隐藏它的加载指标:

  public void onDataChanged(){ 
//如果没有聊天消息,则显示邀请用户添加消息的视图。
mEmptyListMessage.setVisibility(getItemCount()== 0?View.VISIBLE:View.GONE);

原始答案

FirebaseUI列表适配器在内部使用Firebase ChildEventListener 。这个聆听者只会为相关的小孩事件而发火。如果没有孩子,则不会触发事件。

您可以通过将附加的侦听器附加到引用/查询来检测这种情况你传递给适配器。

  DatabaseReference list = mDatabase.child(messages); 
showSpinner();
mAdapter = new FirebaseListAdapter< Message>(......,list){
void populateView(View view,Message message,int position){
//第一条消息被加载
hideSpinner();
}
});
list.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
//初始数据被加载(即使没有)
hideSpinner();
}

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


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?

解决方案

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());
    }
});

这篇关于即使FirebaseListAdapter中没有视图,也要关闭进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆