问题
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();
}
}
回答1:
Before .setQuery(query)
in your FirebaseRecyclerAdapter
builder you can attach a listener to that query to know when it has completed in order to build the RecyclerView
Example
progressDialog.show();
Query query = db.collection("Users").child(my_user_id).addListenerForSingleValue(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Data has been fetched, hide progressDialog , check if data exists at the location with if(dataSnapshot.exists()) if you want to be sure theres something to fetch
progressDialog.hide();
}
@Override
public void onCancelled(DatabaseError databaseError) {
//The fetch failed, dismiss the dialog and show an error message
progressDialog.hide();
}
});
Check the methods of Query
回答2:
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();
}
Also see the FirebaseUI documentation on FirestoreRecyclerAdapter data and error events.
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.
来源:https://stackoverflow.com/questions/60475056/how-to-show-progress-dialog-until-the-data-loads-in-firestorerecycleradapter