├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── matteo │ │ └── firebase_recycleview │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── example │ │ └── matteo │ │ └── firebase_recycleview │ │ ├── FirebaseRecyclerAdapter.java │ │ ├── MainActivity.java │ │ ├── MyAdapter.java │ │ ├── MyItem.java │ │ └── recyclerview-export.json │ └── res │ ├── layout │ ├── activity_main.xml │ └── item.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── extras └── firebase-recyclerview-logo.png ├── firebase-recycleview.iml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | firebase-recycleview -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Disclaimer: This repo is not mainained anymore 3 | 4 | # Firebase Recyclerview 5 | ##### *Easily link your RecyclerView to a Firebase location.* 6 |   7 |   8 | A generic way of backing an Android RecyclerView with a Firebase location. 9 | - It handles all of the child events at the given Firebase location. 10 | - It marshals received data into the given class type. 11 | - Simplifies the management of configuration change (e.g.: device rotation) allowing the restoring of the list. 12 | 13 | #### Installation 14 | No modules, just copy [FirebaseRecyclerAdapter](https://github.com/mmazzarolo/firebase-recycleview/blob/master/app/src/main/java/com/example/matteo/firebase_recycleview/FirebaseRecyclerAdapter.java) in your project and extend it. 15 | 16 | #### Usage 17 | Create an adapter class extending FirebaseRecyclerAdapter and exposing a Viewholder and the Model of the Firebase childs: 18 | `public class MyAdapter extends FirebaseRecyclerAdapter` (example [here](https://github.com/mmazzarolo/firebase-recycleview/blob/master/app/src/main/java/com/example/matteo/firebase_recycleview/MyAdapter.java)). 19 | 20 | FirebaseRecyclerAdapter constructor takes two parameters: 21 | - `query`: The Firebase location to watch for data changes 22 | - `itemClass `: The class of the items (childs) 23 | 24 | FirebaseRecyclerAdapter will handle the item list (listening from a Firebase location) and you can handle the view logic in your new adapter where you must: 25 | - Declare a constructor that calls `super(params...)` with the default FirebaseRecyclerAdapter constructor parameters. 26 | - Override `onCreateViewHolder` and `onBindViewHolder` and handle your viewholder logic here like a classic adapter (in `onBindViewHolder` you can get the item with the `getItem(int position)` method of FirebaseRecyclerAdapter, e.g.:`MyItem item = getItem(position)`. 27 | - Implement the abstract methods `itemAdded`, `itemChanged`, `itemRemoved`, `itemMoved` that will notify you when the list changes. 28 | 29 | Create your adapter just like you always do and pass the interested parameter to its constructor: 30 | `mMyAdapter = new MyAdapter(mQuery, MyItem.class);` 31 | 32 | You're done! 33 | 34 | > Remember to call `MyAdapter.destroy` before destroying the adapter to remove the Firebase location listener! 35 | 36 | #### Handling configurations changes 37 | If you're interested in device rotation handling you should: 38 | - Save FirebaseRecyclerAdapter `mItems` and `mKeys` before destroying the adapter: just put them in a onSavedInstance Bundle (be careful, your must declare the item/child class model as `Parcelable`). You can get them with `getItems()` and `getKeys()`. 39 | - Call `MyAdapter.destroy` before destroying the adapter. 40 | - Re-create the adapter (after the device rotation) passing the saved `mItems` and `mKeys` to the second constructor: `mMyAdapter = new MyAdapter(mQuery, MyItem.class, mAdapterItems, mAdapterKeys);`. 41 | 42 | #### Example 43 | [Here](https://github.com/mmazzarolo/firebase-recycleview/tree/master/app/src/main/java/com/example/matteo/firebase_recycleview) is a working example where I handled device rotation (I used [Parceler](https://github.com/johncarl81/parceler) to make myItem parcelable). 44 | 45 | #### Thanks to... 46 | Thanks to [FirebaseListAdapter](https://github.com/firebase/AndroidChat/blob/master/app/src/main/java/com/firebase/androidchat/FirebaseListAdapter.java) for the starting idea. 47 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.example.matteo.firebase_recycleview" 9 | minSdkVersion 14 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | packagingOptions { 21 | exclude 'META-INF/LICENSE' 22 | exclude 'META-INF/LICENSE-FIREBASE.txt' 23 | exclude 'META-INF/NOTICE' 24 | } 25 | } 26 | 27 | buildscript { 28 | repositories { 29 | mavenCentral() 30 | } 31 | dependencies { 32 | // replace with the current version of the Android plugin 33 | classpath 'com.android.tools.build:gradle:1.3.0' 34 | // the latest version of the android-apt plugin 35 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6' 36 | } 37 | } 38 | 39 | apply plugin: 'com.android.application' 40 | apply plugin: 'com.neenbedankt.android-apt' 41 | 42 | dependencies { 43 | compile fileTree(dir: 'libs', include: ['*.jar']) 44 | compile 'com.android.support:appcompat-v7:22.2.1' 45 | compile 'com.android.support:design:22.2.1' 46 | compile 'com.android.support:recyclerview-v7:22.2.1' 47 | compile 'com.firebase:firebase-client-android:2.3.1' 48 | apt 'org.parceler:parceler:1.0.2' 49 | compile 'org.parceler:parceler-api:1.0.2' 50 | } 51 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\Matteo\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/matteo/firebase_recycleview/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.example.matteo.firebase_recycleview; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/matteo/firebase_recycleview/FirebaseRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.matteo.firebase_recycleview; 2 | 3 | import android.support.annotation.Nullable; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.Log; 6 | import android.view.ViewGroup; 7 | 8 | import com.google.firebase.database.ChildEventListener; 9 | import com.google.firebase.database.DataSnapshot; 10 | import com.google.firebase.database.DatabaseError; 11 | import com.google.firebase.database.Query; 12 | 13 | import java.lang.reflect.ParameterizedType; 14 | import java.util.ArrayList; 15 | 16 | /** 17 | * Created by Matteo on 24/08/2015. 18 | * Updated on 19/06/2016 following https://firebase.google.com/support/guides/firebase-android. 19 | *

20 | * This class is a generic way of backing an Android RecyclerView with a Firebase location. 21 | * It handles all of the child events at the given Firebase location. 22 | * It marshals received data into the given class type. 23 | * Extend this class and provide an implementation of the abstract methods, which will notify when 24 | * the adapter list changes. 25 | *

26 | * This class also simplifies the management of configuration change (e.g.: device rotation) 27 | * allowing the restore of the list. 28 | * 29 | * @param The class type to use as a model for the data contained in the children of the 30 | * given Firebase location 31 | */ 32 | public abstract class FirebaseRecyclerAdapter extends RecyclerView.Adapter { 33 | 34 | private Query mQuery; 35 | private ArrayList mItems; 36 | private ArrayList mKeys; 37 | 38 | /** 39 | * @param query The Firebase location to watch for data changes. 40 | * Can also be a slice of a location, using some combination of 41 | * limit(), startAt(), and endAt(). 42 | */ 43 | public FirebaseRecyclerAdapter(Query query) { 44 | this(query, null, null); 45 | } 46 | 47 | /** 48 | * @param query The Firebase location to watch for data changes. 49 | * Can also be a slice of a location, using some combination of 50 | * limit(), startAt(), and endAt(). 51 | * @param items List of items that will load the adapter before starting the listener. 52 | * Generally null or empty, but this can be useful when dealing with a 53 | * configuration change (e.g.: reloading the adapter after a device rotation). 54 | * Be careful: keys must be coherent with this list. 55 | * @param keys List of keys of items that will load the adapter before starting the listener. 56 | * Generally null or empty, but this can be useful when dealing with a 57 | * configuration change (e.g.: reloading the adapter after a device rotation). 58 | * Be careful: items must be coherent with this list. 59 | */ 60 | public FirebaseRecyclerAdapter(Query query, 61 | @Nullable ArrayList items, 62 | @Nullable ArrayList keys) { 63 | this.mQuery = query; 64 | if (items != null && keys != null) { 65 | this.mItems = items; 66 | this.mKeys = keys; 67 | } else { 68 | mItems = new ArrayList(); 69 | mKeys = new ArrayList(); 70 | } 71 | query.addChildEventListener(mListener); 72 | } 73 | 74 | private ChildEventListener mListener = new ChildEventListener() { 75 | @Override 76 | public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { 77 | String key = dataSnapshot.getKey(); 78 | 79 | if (!mKeys.contains(key)) { 80 | T item = getConvertedObject(dataSnapshot); 81 | int insertedPosition; 82 | if (previousChildName == null) { 83 | mItems.add(0, item); 84 | mKeys.add(0, key); 85 | insertedPosition = 0; 86 | } else { 87 | int previousIndex = mKeys.indexOf(previousChildName); 88 | int nextIndex = previousIndex + 1; 89 | if (nextIndex == mItems.size()) { 90 | mItems.add(item); 91 | mKeys.add(key); 92 | } else { 93 | mItems.add(nextIndex, item); 94 | mKeys.add(nextIndex, key); 95 | } 96 | insertedPosition = nextIndex; 97 | } 98 | notifyItemInserted(insertedPosition); 99 | itemAdded(item, key, insertedPosition); 100 | } 101 | } 102 | 103 | @Override 104 | public void onChildChanged(DataSnapshot dataSnapshot, String s) { 105 | String key = dataSnapshot.getKey(); 106 | 107 | if (mKeys.contains(key)) { 108 | int index = mKeys.indexOf(key); 109 | T oldItem = mItems.get(index); 110 | T newItem = getConvertedObject(dataSnapshot); 111 | 112 | mItems.set(index, newItem); 113 | 114 | notifyItemChanged(index); 115 | itemChanged(oldItem, newItem, key, index); 116 | } 117 | } 118 | 119 | @Override 120 | public void onChildRemoved(DataSnapshot dataSnapshot) { 121 | String key = dataSnapshot.getKey(); 122 | 123 | if (mKeys.contains(key)) { 124 | int index = mKeys.indexOf(key); 125 | T item = mItems.get(index); 126 | 127 | mKeys.remove(index); 128 | mItems.remove(index); 129 | 130 | notifyItemRemoved(index); 131 | itemRemoved(item, key, index); 132 | } 133 | } 134 | 135 | @Override 136 | public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) { 137 | String key = dataSnapshot.getKey(); 138 | 139 | int index = mKeys.indexOf(key); 140 | T item = getConvertedObject(dataSnapshot); 141 | mItems.remove(index); 142 | mKeys.remove(index); 143 | int newPosition; 144 | if (previousChildName == null) { 145 | mItems.add(0, item); 146 | mKeys.add(0, key); 147 | newPosition = 0; 148 | } else { 149 | int previousIndex = mKeys.indexOf(previousChildName); 150 | int nextIndex = previousIndex + 1; 151 | if (nextIndex == mItems.size()) { 152 | mItems.add(item); 153 | mKeys.add(key); 154 | } else { 155 | mItems.add(nextIndex, item); 156 | mKeys.add(nextIndex, key); 157 | } 158 | newPosition = nextIndex; 159 | } 160 | notifyItemMoved(index, newPosition); 161 | itemMoved(item, key, index, newPosition); 162 | } 163 | 164 | @Override 165 | public void onCancelled(DatabaseError databaseError) { 166 | Log.e("FirebaseListAdapter", "Listen was cancelled, no more updates will occur."); 167 | } 168 | 169 | }; 170 | 171 | @Override 172 | public abstract ViewHolder onCreateViewHolder(ViewGroup parent, int viewType); 173 | 174 | @Override 175 | public abstract void onBindViewHolder(ViewHolder holder, final int position); 176 | 177 | @Override 178 | public int getItemCount() { 179 | return (mItems != null) ? mItems.size() : 0; 180 | } 181 | 182 | /** 183 | * Clean the adapter. 184 | * ALWAYS call this method before destroying the adapter to remove the listener. 185 | */ 186 | public void destroy() { 187 | mQuery.removeEventListener(mListener); 188 | } 189 | 190 | /** 191 | * Returns the list of items of the adapter: can be useful when dealing with a configuration 192 | * change (e.g.: a device rotation). 193 | * Just save this list before destroying the adapter and pass it to the new adapter (in the 194 | * constructor). 195 | * 196 | * @return the list of items of the adapter 197 | */ 198 | public ArrayList getItems() { 199 | return mItems; 200 | } 201 | 202 | /** 203 | * Returns the list of keys of the items of the adapter: can be useful when dealing with a 204 | * configuration change (e.g.: a device rotation). 205 | * Just save this list before destroying the adapter and pass it to the new adapter (in the 206 | * constructor). 207 | * 208 | * @return the list of keys of the items of the adapter 209 | */ 210 | public ArrayList getKeys() { 211 | return mKeys; 212 | } 213 | 214 | /** 215 | * Returns the item in the specified position 216 | * 217 | * @param position Position of the item in the adapter 218 | * @return the item 219 | */ 220 | public T getItem(int position) { 221 | return mItems.get(position); 222 | } 223 | 224 | /** 225 | * Returns the position of the item in the adapter 226 | * 227 | * @param item Item to be searched 228 | * @return the position in the adapter if found, -1 otherwise 229 | */ 230 | public int getPositionForItem(T item) { 231 | return mItems != null && mItems.size() > 0 ? mItems.indexOf(item) : -1; 232 | } 233 | 234 | /** 235 | * Check if the searched item is in the adapter 236 | * 237 | * @param item Item to be searched 238 | * @return true if the item is in the adapter, false otherwise 239 | */ 240 | public boolean contains(T item) { 241 | return mItems != null && mItems.contains(item); 242 | } 243 | 244 | /** 245 | * ABSTRACT METHODS THAT MUST BE IMPLEMENTED BY THE EXTENDING ADAPTER. 246 | */ 247 | 248 | /** 249 | * Called after an item has been added to the adapter 250 | * 251 | * @param item Added item 252 | * @param key Key of the added item 253 | * @param position Position of the added item in the adapter 254 | */ 255 | protected void itemAdded(T item, String key, int position) { 256 | 257 | } 258 | 259 | /** 260 | * Called after an item changed 261 | * 262 | * @param oldItem Old version of the changed item 263 | * @param newItem Current version of the changed item 264 | * @param key Key of the changed item 265 | * @param position Position of the changed item in the adapter 266 | */ 267 | protected void itemChanged(T oldItem, T newItem, String key, int position) { 268 | 269 | } 270 | 271 | /** 272 | * Called after an item has been removed from the adapter 273 | * 274 | * @param item Removed item 275 | * @param key Key of the removed item 276 | * @param position Position of the removed item in the adapter 277 | */ 278 | protected void itemRemoved(T item, String key, int position) { 279 | 280 | } 281 | 282 | /** 283 | * Called after an item changed position 284 | * 285 | * @param item Moved item 286 | * @param key Key of the moved item 287 | * @param oldPosition Old position of the changed item in the adapter 288 | * @param newPosition New position of the changed item in the adapter 289 | */ 290 | protected void itemMoved(T item, String key, int oldPosition, int newPosition) { 291 | 292 | } 293 | 294 | /** 295 | * Converts the data snapshot to generic object 296 | * 297 | * @param snapshot Result 298 | * @return Data converted 299 | */ 300 | protected T getConvertedObject(DataSnapshot snapshot) { 301 | return snapshot.getValue(getGenericClass()); 302 | } 303 | 304 | /** 305 | * Returns a class reference from generic T. 306 | */ 307 | @SuppressWarnings("unchecked") 308 | private Class getGenericClass() { 309 | return (Class) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; 310 | } 311 | 312 | } 313 | 314 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/matteo/firebase_recycleview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.matteo.firebase_recycleview; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.view.Menu; 8 | import android.view.MenuItem; 9 | 10 | import com.firebase.client.Firebase; 11 | import com.firebase.client.Query; 12 | 13 | import org.parceler.Parcels; 14 | 15 | import java.util.ArrayList; 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | private final static String SAVED_ADAPTER_ITEMS = "SAVED_ADAPTER_ITEMS"; 20 | private final static String SAVED_ADAPTER_KEYS = "SAVED_ADAPTER_KEYS"; 21 | 22 | private Query mQuery; 23 | private MyAdapter mMyAdapter; 24 | private ArrayList mAdapterItems; 25 | private ArrayList mAdapterKeys; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_main); 31 | 32 | handleInstanceState(savedInstanceState); 33 | setupFirebase(); 34 | setupRecyclerview(); 35 | } 36 | 37 | // Restoring the item list and the keys of the items: they will be passed to the adapter 38 | private void handleInstanceState(Bundle savedInstanceState) { 39 | if (savedInstanceState != null && 40 | savedInstanceState.containsKey(SAVED_ADAPTER_ITEMS) && 41 | savedInstanceState.containsKey(SAVED_ADAPTER_KEYS)) { 42 | mAdapterItems = Parcels.unwrap(savedInstanceState.getParcelable(SAVED_ADAPTER_ITEMS)); 43 | mAdapterKeys = savedInstanceState.getStringArrayList(SAVED_ADAPTER_KEYS); 44 | } else { 45 | mAdapterItems = new ArrayList(); 46 | mAdapterKeys = new ArrayList(); 47 | } 48 | } 49 | 50 | private void setupFirebase() { 51 | Firebase.setAndroidContext(this); 52 | String firebaseLocation = getResources().getString(R.string.firebase_location); 53 | mQuery = new Firebase(firebaseLocation); 54 | } 55 | 56 | private void setupRecyclerview() { 57 | RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); 58 | mMyAdapter = new MyAdapter(mQuery, MyItem.class, mAdapterItems, mAdapterKeys); 59 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 60 | recyclerView.setAdapter(mMyAdapter); 61 | } 62 | 63 | @Override 64 | public boolean onCreateOptionsMenu(Menu menu) { 65 | getMenuInflater().inflate(R.menu.menu_main, menu); 66 | return true; 67 | } 68 | 69 | @Override 70 | public boolean onOptionsItemSelected(MenuItem item) { 71 | return item.getItemId() == R.id.action_settings || super.onOptionsItemSelected(item); 72 | } 73 | 74 | // Saving the list of items and keys of the items on rotation 75 | @Override 76 | protected void onSaveInstanceState(Bundle outState) { 77 | super.onSaveInstanceState(outState); 78 | outState.putParcelable(SAVED_ADAPTER_ITEMS, Parcels.wrap(mMyAdapter.getItems())); 79 | outState.putStringArrayList(SAVED_ADAPTER_KEYS, mMyAdapter.getKeys()); 80 | } 81 | 82 | @Override 83 | protected void onDestroy() { 84 | super.onDestroy(); 85 | mMyAdapter.destroy(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/matteo/firebase_recycleview/MyAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.matteo.firebase_recycleview; 2 | 3 | import android.support.annotation.Nullable; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.Log; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.TextView; 10 | 11 | import com.firebase.client.Query; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * Created by Matteo on 24/08/2015. 18 | */ 19 | public class MyAdapter extends FirebaseRecyclerAdapter { 20 | 21 | public static class ViewHolder extends RecyclerView.ViewHolder { 22 | 23 | TextView textViewName; 24 | TextView textViewAge; 25 | 26 | public ViewHolder(View view) { 27 | super(view); 28 | textViewName = (TextView) view.findViewById(R.id.textview_name); 29 | textViewAge = (TextView) view.findViewById(R.id.textview_age); 30 | } 31 | } 32 | 33 | public MyAdapter(Query query, @Nullable ArrayList items, 34 | @Nullable ArrayList keys) { 35 | super(query, items, keys); 36 | } 37 | 38 | @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 39 | View view = LayoutInflater.from(parent.getContext()) 40 | .inflate(R.layout.item, parent, false); 41 | 42 | return new ViewHolder(view); 43 | } 44 | 45 | @Override public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) { 46 | MyItem item = getItem(position); 47 | holder.textViewName.setText(item.getName()); 48 | holder.textViewAge.setText(String.valueOf(item.getAge())); 49 | } 50 | 51 | @Override protected void itemAdded(MyItem item, String key, int position) { 52 | Log.d("MyAdapter", "Added a new item to the adapter."); 53 | } 54 | 55 | @Override protected void itemChanged(MyItem oldItem, MyItem newItem, String key, int position) { 56 | Log.d("MyAdapter", "Changed an item."); 57 | } 58 | 59 | @Override protected void itemRemoved(MyItem item, String key, int position) { 60 | Log.d("MyAdapter", "Removed an item from the adapter."); 61 | } 62 | 63 | @Override protected void itemMoved(MyItem item, String key, int oldPosition, int newPosition) { 64 | Log.d("MyAdapter", "Moved an item."); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/matteo/firebase_recycleview/MyItem.java: -------------------------------------------------------------------------------- 1 | package com.example.matteo.firebase_recycleview; 2 | 3 | 4 | import org.parceler.Parcel; 5 | 6 | /** 7 | * Created by Matteo on 24/08/2015. 8 | */ 9 | @Parcel 10 | public class MyItem { 11 | 12 | String name; 13 | long age; 14 | 15 | public MyItem() { 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public long getAge() { 27 | return age; 28 | } 29 | 30 | public void setAge(long age) { 31 | this.age = age; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/matteo/firebase_recycleview/recyclerview-export.json: -------------------------------------------------------------------------------- 1 | // Example data from https://recyclerview.firebaseio.com/ 2 | { 3 | "person1" : { 4 | "age" : 1, 5 | "name" : "Tony" 6 | }, 7 | "person2" : { 8 | "age" : 1, 9 | "name" : "Fabio" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 |

5 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmazzarolo/firebase-recyclerview/892faf210226670c24cb273b688e2fa8aa03f29d/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmazzarolo/firebase-recyclerview/892faf210226670c24cb273b688e2fa8aa03f29d/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmazzarolo/firebase-recyclerview/892faf210226670c24cb273b688e2fa8aa03f29d/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmazzarolo/firebase-recyclerview/892faf210226670c24cb273b688e2fa8aa03f29d/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | firebase-recycleview 3 | 4 | Settings 5 | 6 | https://recyclerview.firebaseio.com/ 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.3.0' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /extras/firebase-recyclerview-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmazzarolo/firebase-recyclerview/892faf210226670c24cb273b688e2fa8aa03f29d/extras/firebase-recyclerview-logo.png -------------------------------------------------------------------------------- /firebase-recycleview.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmazzarolo/firebase-recyclerview/892faf210226670c24cb273b688e2fa8aa03f29d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Aug 24 18:41:49 CEST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------