其他分享
首页 > 其他分享> > android – 即使没有在FirebaseListAdapter中填充的视图,如何关闭进度条?

android – 即使没有在FirebaseListAdapter中填充的视图,如何关闭进度条?

作者:互联网

我使用FirebaseUI FirebaseListAdapter.加载数据需要一些时间,我想显示一个旋转的圆圈.我可以通过在populateView中将视图可见性设置为不可见来忽略进度条,但如果没有要填充的视图则它不起作用.怎么办呢?

解决方法:

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

source code 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.

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.

FirebaseUI示例应用程序覆盖onDataChanged()以隐藏其“加载”指示符:

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

原始答案

FirebaseUI列表适配器在内部使用Firebase ChildEventListener.此侦听器仅针对相关的子事件触发.如果没有孩子,则不会发生任何事件.

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

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

标签:firebaseui,android,firebase,firebase-realtime-database
来源: https://codeday.me/bug/20190917/1810213.html