FirebaseListAdapter 完成时如何得到通知?

How to be notified when FirebaseListAdapter finishes?

我正在使用 FirebaseListAdapter,我需要显示 ProgressDialog,但我不知道如何知道它何时完成加载。

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


}

我什么时候可以放置 ProgressDialog.dismiss()?

Firebase 建立在同步数据的前提下。因此,从来没有真正 完成 同步的时刻。

一个位置的 初始 数据集一次性下载,结果会为每个 child 调用 onChildAdded(),然后调用单身onDataChanged()。如果你想在这个初始数据加载后隐藏进度条,你可以为单个事件注册一个ValueEventListener

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) {

    }
});

在上面的代码片段中,所有 children 都已经添加到您的 ListView 中。根据您的 use-case,这可能是隐藏进度条的正确时间,也可能比预期的要晚。

如果你想知道第一个数据何时被渲染,你可以用你的适配器注册一个观察者:

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

您可以使用此方法在第一个数据从 Firebase 到达后立即隐藏进度条。

自 2017 年 1 月起 FirebaseListAdapter 具有以下可以覆盖的方法(Frank 在 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() {
    }