Skip to content

Commit bfe220e

Browse files
committed
Added instructions for RecyclerView
1 parent 4c43a35 commit bfe220e

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

README.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ We can now use that in our activity to allow sending a message:
191191
Firebase.setAndroidContext(this);
192192
Firebase ref = new Firebase("https://nanochat.firebaseio.com");
193193

194-
mAdapter = new FirebaseListAdapter<ChatMessage>(ChatMessage.class, android.R.layout.two_line_list_item, this, ref) {
194+
mAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, android.R.layout.two_line_list_item, ref) {
195195
@Override
196196
protected void populateView(View view, ChatMessage chatMessage) {
197197
((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
@@ -218,6 +218,54 @@ We can now use that in our activity to allow sending a message:
218218

219219
Et voila: a minimal, yet fully functional, chat app in about 30 lines of code. Not bad, right?
220220

221+
## Using a RecyclerView
222+
223+
RecyclerView is the new preferred way to handle potentially long lists of items. Since Firebase collections
224+
can contain many items, there is an `FirebaseRecyclerViewAdapter` too. Here's how you use it:
225+
226+
1. Create a custom ViewHolder class
227+
2. Create a custom subclass FirebaseListAdapter
228+
229+
The rest of the steps is the same as for the `FirebaseListAdapter` above, so be sure to read that first.
230+
231+
### Create a custom ViewHolder
232+
233+
A ViewHolder is similar to container of a ViewGroup that allows simple lookup of the sub-views of the group.
234+
If we use the same layout as before (`android.R.layout.two_line_list_item`), there are two `TextView`s in there.
235+
We can wrap that in a ViewHolder with:
236+
237+
private static class ChatMessageViewHolder extends RecyclerView.ViewHolder {
238+
TextView messageText;
239+
TextView nameText;
240+
241+
public ChatMessageViewHolder(View itemView) {
242+
super(itemView);
243+
nameText = (TextView)itemView.findViewById(android.R.id.text1);
244+
messageText = (TextView) itemView.findViewById(android.R.id.text2);
245+
}
246+
}
247+
248+
There's nothing magical going on here; we're just mapping numeric IDs and casts into a nice, type-safe contract.
249+
250+
### Create a custom FirebaseListAdapter
251+
252+
Just like we did for FirebaseListAdapter, we'll create an anonymous subclass for our ChatMessages:
253+
254+
RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler);
255+
recycler.setHasFixedSize(true);
256+
recycler.setLayoutManager(new LinearLayoutManager(this));
257+
258+
mAdapter = new FirebaseRecyclerViewAdapter<ChatMessage, ChatMessageViewHolder>(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, mRef) {
259+
@Override
260+
public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage) {
261+
chatMessageViewHolder.nameText.setText(chatMessage.getName());
262+
chatMessageViewHolder.messageText.setText(chatMessage.getMessage());
263+
}
264+
};
265+
recycler.setAdapter(mAdapter);
266+
267+
Like before, we get a custom RecyclerView populated with data from Firebase by setting the properties to the correct fields.
268+
221269
## Installing locally
222270

223271
We are still working on deploying FirebaseUI to Maven Central. In the meantime, you can download the
@@ -229,7 +277,7 @@ with:
229277

230278
## Deployment
231279

232-
### To get the build server ready to build/deploy FirebaseUI-Android
280+
### To get the build server ready to build FirebaseUI-Android
233281

234282
* Install a JDK (if it's not installed yet):
235283
* `sudo apt-get install default-jdk`
@@ -262,8 +310,12 @@ with:
262310
sonatypeUsername=YourSonatypeJiraUsername
263311
sonatypePassword=YourSonatypeJiraPassword
264312

313+
### to build a release
314+
315+
* build the project in Android Studio or with Gradle
316+
*
265317

266-
### to build/deploy
318+
### to deploy a release
267319

268320
* log onto the build box
269321
* checkout and update the master branch

0 commit comments

Comments
 (0)