Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

android - How to load batches of data in a recycler view using firestore?

I wanted to know how to load more data in recylcer view using firestore.

 Query query = FirebaseFirestore.getInstance()
            .collection("ie").limit(5);  
adapter=new InterviewAdapter(this,query);    
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

Adapter class looks like this:

public class InterviewAdapter extends FireStoreAdapter<InterviewAdapter.ViewHolder> {

public interface OnInterviewSelectedListener {

    void onInterviewSelected(DocumentSnapshot interview);

}

private InterviewAdapter.OnInterviewSelectedListener mListener;

public InterviewAdapter(Query query, OnInterviewSelectedListener listener) {
    super(query);
    mListener = listener;
}

@Override
public InterviewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    return new InterviewAdapter.ViewHolder(inflater.inflate(R.layout.ie, parent, false));
}

@Override
public void onBindViewHolder(InterviewAdapter.ViewHolder holder, int position) {
    holder.bind(getSnapshot(position), mListener);
}

static class ViewHolder extends RecyclerView.ViewHolder {

    TextView title,companyName,username,views,isHired;

    public ViewHolder(View itemView) {
        super(itemView);
        title= (TextView) itemView.findViewById(R.id.title);
        companyName= (TextView) itemView.findViewById(R.id.companyName);
        username= (TextView) itemView.findViewById(R.id.username);
        views= (TextView) itemView.findViewById(R.id.views);
        isHired= (TextView) itemView.findViewById(R.id.isHired);
    }

    public void bind(final DocumentSnapshot snapshot,
                     final OnInterviewSelectedListener listener) {


        InterviewExperience experience;
        String companyName=snapshot.getString("companyName");
        boolean isHired=Boolean.valueOf(snapshot.getBoolean("isHired"));
        String username=snapshot.getString("username");
        long views=new Double(Double.valueOf(snapshot.getDouble("views"))).longValue();

        String id=snapshot.getId();


        String title=snapshot.getString("title");
        experience=new InterviewExperience(id,title,companyName,username,isHired,views,null,null);


        this.title.setText(experience.getTitle());
        this.companyName.setText("Company Name: "+experience.getCompanyName());
        this.isHired.setText("Hired: "+experience.isHired());
        this.views.setText("Views: "+experience.getViews()+"");
        this.username.setText("Created By: "+experience.getUsername());



        // Click listener
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onInterviewSelected(snapshot);
                }
            }
        });
    }

}
}

public abstract class FireStoreAdapter<VH extends RecyclerView.ViewHolder>
    extends RecyclerView.Adapter<VH>
    implements EventListener<QuerySnapshot> {

private static final String TAG = "FirestoreAdapter";

private Query mQuery;
private ListenerRegistration mRegistration;

private ArrayList<DocumentSnapshot> mSnapshots = new ArrayList<>();

public FireStoreAdapter(Query query) {
    mQuery = query;
}

@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
    if (e != null) {
        Log.w(TAG, "onEvent:error", e);
        onError(e);
        return;
    }

    // Dispatch the event
    Log.d(TAG, "onEvent:numChanges:" + documentSnapshots.getDocumentChanges().size());
    for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
        switch (change.getType()) {
            case ADDED:
                onDocumentAdded(change);
                break;
            case MODIFIED:
                onDocumentModified(change);
                break;
            case REMOVED:
                onDocumentRemoved(change);
                break;
        }
    }

    onDataChanged();
}

public void startListening() {
    if (mQuery != null && mRegistration == null) {
        mRegistration = mQuery.addSnapshotListener(this);
    }
}

public void stopListening() {
    if (mRegistration != null) {
        mRegistration.remove();
        mRegistration = null;
    }

    mSnapshots.clear();
    notifyDataSetChanged();
}

public void setQuery(Query query) {
    // Stop listening
    stopListening();

    // Clear existing data
    mSnapshots.clear();
    notifyDataSetChanged();

    // Listen to new query
    mQuery = query;
    startListening();
}

@Override
public int getItemCount() {
    return mSnapshots.size();
}

protected DocumentSnapshot getSnapshot(int index) {
    return mSnapshots.get(index);
}

protected void onDocumentAdded(DocumentChange change) {
    mSnapshots.add(change.getNewIndex(), change.getDocument());
    notifyItemInserted(change.getNewIndex());
}

protected void onDocumentModified(DocumentChange change) {
    if (change.getOldIndex() == change.getNewIndex()) {
        // Item changed but remained in same position
        mSnapshots.set(change.getOldIndex(), change.getDocument());
        notifyItemChanged(change.getOldIndex());
    } else {
        // Item changed and changed position
        mSnapshots.remove(change.getOldIndex());
        mSnapshots.add(change.getNewIndex(), change.getDocument());
        notifyItemMoved(change.getOldIndex(), change.getNewIndex());
    }
}

protected void onDocumentRemoved(DocumentChange change) {
    mSnapshots.remove(change.getOldIndex());
    notifyItemRemoved(change.getOldIndex());
}

protected void onError(FirebaseFirestoreException e) {};

protected void onDataChanged() {}
}

I used Firestore Adapter code which was given in samples of firestore documentation. Can anyone tell how to use the query object to load more data?

How to load the next 5 items in the recycler view when users scrolls to the end of the list?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...