Git Product home page Git Product logo

firebase-recyclerview's Introduction

Disclaimer: This repo is not mainained anymore

Firebase Recyclerview

Easily link your RecyclerView to a Firebase location.

 
 
A generic way of backing an Android RecyclerView with a Firebase location.

  • It handles all of the child events at the given Firebase location.
  • It marshals received data into the given class type.
  • Simplifies the management of configuration change (e.g.: device rotation) allowing the restoring of the list.

Installation

No modules, just copy FirebaseRecyclerAdapter in your project and extend it.

Usage

Create an adapter class extending FirebaseRecyclerAdapter and exposing a Viewholder and the Model of the Firebase childs: public class MyAdapter extends FirebaseRecyclerAdapter<MyAdapter.ViewHolder, MyItem> (example here).

FirebaseRecyclerAdapter constructor takes two parameters:

  • query: The Firebase location to watch for data changes
  • itemClass : The class of the items (childs)

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:

  • Declare a constructor that calls super(params...) with the default FirebaseRecyclerAdapter constructor parameters.
  • 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).
  • Implement the abstract methods itemAdded, itemChanged, itemRemoved, itemMoved that will notify you when the list changes.

Create your adapter just like you always do and pass the interested parameter to its constructor: mMyAdapter = new MyAdapter(mQuery, MyItem.class);

You're done!

Remember to call MyAdapter.destroy before destroying the adapter to remove the Firebase location listener!

Handling configurations changes

If you're interested in device rotation handling you should:

  • 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().
  • Call MyAdapter.destroy before destroying the adapter.
  • 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);.

Example

Here is a working example where I handled device rotation (I used Parceler to make myItem parcelable).

Thanks to...

Thanks to FirebaseListAdapter for the starting idea.

firebase-recyclerview's People

Contributors

jrvansuita avatar mmazzarolo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.