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 FirebaseRecyclerAdapter<T,VH extends RecyclerView.ViewHolder>
extends <any>
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); } } FirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder> adapter; DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler); recycler.setHasFixedSize(true); recycler.setLayoutManager(new LinearLayoutManager(this)); adapter = new FirebaseRecyclerAdapter<ChatMessage, ChatMessageViewHolder>(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, ref) { public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage, int position) { chatMessageViewHolder.nameText.setText(chatMessage.getName()); chatMessageViewHolder.messageText.setText(chatMessage.getMessage()); } }; recycler.setAdapter(mAdapter);
Modifier and Type | Field and Description |
---|---|
protected int |
mModelLayout |
Constructor and Description |
---|
FirebaseRecyclerAdapter(java.lang.Class<T> modelClass,
int modelLayout,
java.lang.Class<VH> viewHolderClass,
DatabaseReference ref) |
FirebaseRecyclerAdapter(java.lang.Class<T> modelClass,
int modelLayout,
java.lang.Class<VH> viewHolderClass,
Query ref) |
Modifier and Type | Method and Description |
---|---|
void |
cleanup() |
T |
getItem(int position) |
int |
getItemCount() |
long |
getItemId(int position) |
int |
getItemViewType(int position) |
DatabaseReference |
getRef(int position) |
void |
onBindViewHolder(VH viewHolder,
int position) |
protected void |
onCancelled(DatabaseError databaseError)
This method will be triggered in the event that this listener either failed at the server,
or is removed as a result of the security and Firebase Database rules.
|
VH |
onCreateViewHolder(android.view.ViewGroup parent,
int viewType) |
protected T |
parseSnapshot(DataSnapshot snapshot)
This method parses the DataSnapshot into the requested type.
|
protected abstract void |
populateViewHolder(VH viewHolder,
T model,
int position)
Each time the data at the given Firebase location changes, this method will be called for each item that needs
to be displayed.
|
public FirebaseRecyclerAdapter(java.lang.Class<T> modelClass, int modelLayout, java.lang.Class<VH> viewHolderClass, Query ref)
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.ref
- The Firebase location to watch for data changes. Can also be a slice of a location, using some
combination of limit()
, startAt()
, and endAt()
public FirebaseRecyclerAdapter(java.lang.Class<T> modelClass, int modelLayout, java.lang.Class<VH> viewHolderClass, DatabaseReference ref)
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.ref
- The Firebase location to watch for data changes. Can also be a slice of a location, using some
combination of limit()
, startAt()
, and endAt()
public void cleanup()
public int getItemCount()
public T getItem(int position)
protected T parseSnapshot(DataSnapshot snapshot)
snapshot
- the DataSnapshot to extract the model frompublic DatabaseReference getRef(int position)
public long getItemId(int position)
public VH onCreateViewHolder(android.view.ViewGroup parent, int viewType)
public void onBindViewHolder(VH viewHolder, int position)
public int getItemViewType(int position)
protected void onCancelled(DatabaseError databaseError)
databaseError
- A description of the error that occurredprotected abstract void populateViewHolder(VH viewHolder, T model, int position)
Your implementation should populate the view using the data contained in the model.
viewHolder
- The view to populatemodel
- The object containing the data used to populate the viewposition
- The position in the list of the view being populated