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>
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);
}
}
FirebaseRecyclerAdapter 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.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);
To avoid Context leaks, make sure you invoke cleanup
| Modifier and Type | Field and Description |
|---|---|
protected int |
mModelLayout |
protected java.lang.Class<VH> |
mViewHolderClass |
| Constructor and Description |
|---|
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 error) |
protected void |
onChildChanged(ChangeEventListener.EventType type,
int index,
int oldIndex) |
VH |
onCreateViewHolder(android.view.ViewGroup parent,
int viewType) |
protected void |
onDataChanged() |
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.
|
protected java.lang.Class<VH extends RecyclerView.ViewHolder> mViewHolderClass
protected int mModelLayout
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 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 onChildChanged(ChangeEventListener.EventType type, int index, int oldIndex)
protected void onDataChanged()
ChangeEventListener.onDataChanged()protected void onCancelled(DatabaseError error)
protected 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