T - The Java class that maps to the type of objects stored in the Firebase location.VH - The ViewHolder class that contains the Views in the layout that is shown for each object.public abstract class FirebaseIndexRecyclerAdapter<T,VH extends RecyclerView.ViewHolder> extends FirebaseRecyclerAdapter<T,VH>
To use this class in your app, subclass it passing in all required parameters and implement the populateViewHolder method.
private static class ChatMessageViewHolder extends RecyclerView.ViewHolder {
TextView messageText;
TextView nameText;
public ChatMessageViewHolder(View itemView) {
super(itemView);
nameText = (TextView)itemView.findViewById(android.R.id.text1);
messageText = (TextView) itemView.findViewById(android.R.id.text2);
}
}
FirebaseIndexRecyclerAdapter adapter;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
adapter = new FirebaseIndexRecyclerAdapter(
ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, keyRef, dataRef) {
public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder,
ChatMessage chatMessage,
int position) {
chatMessageViewHolder.nameText.setText(chatMessage.getName());
chatMessageViewHolder.messageText.setText(chatMessage.getMessage());
}
};
recycler.setAdapter(mAdapter);
mModelLayout, mViewHolderClass| Constructor and Description |
|---|
FirebaseIndexRecyclerAdapter(java.lang.Class<T> modelClass,
int modelLayout,
java.lang.Class<VH> viewHolderClass,
Query keyRef,
Query dataRef) |
cleanup, getItem, getItemCount, getItemId, getItemViewType, getRef, onBindViewHolder, onCancelled, onChildChanged, onCreateViewHolder, onDataChanged, parseSnapshot, populateViewHolderpublic FirebaseIndexRecyclerAdapter(java.lang.Class<T> modelClass, @LayoutRes int modelLayout, java.lang.Class<VH> viewHolderClass, Query keyRef, Query dataRef)
modelClass - Firebase will marshall the data at a location into an instance
of a class that you providemodelLayout - This is the layout used to represent a single item in the list.
You will be responsible for populating an
instance of the corresponding view with the data from an instance of modelClass.viewHolderClass - The class that hold references to all sub-views in an instance modelLayout.keyRef - The Firebase location containing the list of keys to be found in dataRef.
Can also be a slice of a location, using some
combination of limit(), startAt(), and endAt().dataRef - The Firebase location to watch for data changes.
Each key key found at keyRef's location represents
a list item in the RecyclerView.