如何显示进度对话框,直到数据加载到FirestoreRecyclerAdapter中 [英] How to show progress dialog until the data loads in FirestoreRecyclerAdapter

查看:171
本文介绍了如何显示进度对话框,直到数据加载到FirestoreRecyclerAdapter中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FirestoreRecyclerAdapter 将数据从 Firestore 加载到 RecyclerView 中.我想显示进度对话框,直到数据完全加载.我怎么做?预先感谢.

I am using FirestoreRecyclerAdapter to load the data from Firestore into RecyclerView. I would like to show progress dialog until the data loads completely. How do I do that? Thanks in advance.

到目前为止,这是我的代码.

Here is my code so far.

void loadPosts() {
    FirestoreRecyclerOptions<Post> options = new FirestoreRecyclerOptions.Builder<Post>()
            .setQuery(query, Post.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Post, DataViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull DataViewHolder dataViewHolder, int i, @NonNull Post post) {
            dataViewHolder.setData(post.getTitle(), post.getDescription());
        }

        @NonNull
        @Override
        public DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_post, parent, false);
            return new DataViewHolder(view);
        }
    };
    recyclerView.setAdapter(adapter);
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    if (adapter != null) {
        adapter.stopListening();
    }
}

@Override
protected void onStart() {
    super.onStart();
    if (adapter != null) {
        adapter.startListening();
    }
}

推荐答案

虽然Gastón的答案有效,但有一种稍微简单的方法,不需要额外的侦听器. FirestoreRecyclerAdapter 具有一种在收到完整的 QuerySnapshot 时发出信号的方法.

While Gastón's answer works, there is a slightly simpler way, that doesn't require an extra listener. The FirestoreRecyclerAdapter has a method that signals when it has received a complete QuerySnapshot.

void loadPosts() {
    FirestoreRecyclerOptions<Post> options = new FirestoreRecyclerOptions.Builder<Post>()
            .setQuery(query, Post.class)
            .build();

    progressDialog.show();

    adapter = new FirestoreRecyclerAdapter<Post, DataViewHolder>(options) {
        @Override
        public void onDataChanged() {
            progressDialog.hide();
        }

        @Override
        protected void onBindViewHolder(@NonNull DataViewHolder dataViewHolder, int i, @NonNull Post post) {
            dataViewHolder.setData(post.getTitle(), post.getDescription());
        }

        @NonNull
        @Override
        public DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_post, parent, false);
            return new DataViewHolder(view);
        }
    };
    recyclerView.setAdapter(adapter);
    adapter.startListening();
}

另请参见 FirestoreRecyclerAdapter 数据和错误事件.

Also see the FirebaseUI documentation on FirestoreRecyclerAdapter data and error events.

请注意, onDataChanged 可能会被多次调用,最初调用一次,然后每次 query 的数据更改时调用一次.因此,请确保其中的代码可以处理多次.例如:如果您破坏了 onDataChanged 中的进度对话框,而不是隐藏它,那么您也将要检查它的存在.

Note that onDataChanged may be called multiple times, once initially and then each time when the data for query changes. So make sure that the code in there can handle being called multiple times. For example: if you destroy the progress dialog in onDataChanged instead of hiding it, you'll want to check for its existence too.

这篇关于如何显示进度对话框,直到数据加载到FirestoreRecyclerAdapter中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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