-
-
Save wirasetiawan29/896e7082b3ffa6dde8fd3f15bc48ac6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class FirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { | |
Class<T> mModelClass; | |
protected int mModelLayout; | |
Class<VH> mViewHolderClass; | |
FirebaseArray mSnapshots; | |
public FirebaseRecyclerAdapter(Class<T> modelClass, int modelLayout, Class<VH> viewHolderClass, Query ref) { | |
mModelClass = modelClass; | |
mModelLayout = modelLayout; | |
mViewHolderClass = viewHolderClass; | |
mSnapshots = new FirebaseArray(ref); | |
mSnapshots.setOnChangedListener(new FirebaseArray.OnChangedListener() { | |
@Override | |
public void onChanged(EventType type, int index, int oldIndex) { | |
switch (type) { | |
case Added: | |
notifyItemInserted(index); | |
break; | |
case Changed: | |
notifyItemChanged(index); | |
break; | |
case Removed: | |
notifyItemRemoved(index); | |
break; | |
case Moved: | |
notifyItemMoved(oldIndex, index); | |
break; | |
default: | |
throw new IllegalStateException("Incomplete case statement"); | |
} | |
} | |
}); | |
} | |
public FirebaseRecyclerAdapter(Class<T> modelClass, int modelLayout, Class<VH> viewHolderClass, Firebase ref) { | |
this(modelClass, modelLayout, viewHolderClass, (Query) ref); | |
} | |
public void cleanup() { | |
mSnapshots.cleanup(); | |
} | |
@Override | |
public int getItemCount() { | |
return mSnapshots.getCount(); | |
} | |
public T getItem(int position) { | |
return parseSnapshot(mSnapshots.getItem(position)); | |
} | |
protected T parseSnapshot(DataSnapshot snapshot) { | |
return snapshot.getValue(mModelClass); | |
} | |
public Firebase getRef(int position) { return mSnapshots.getItem(position).getRef(); } | |
@Override | |
public long getItemId(int position) { | |
// http://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter | |
return mSnapshots.getItem(position).getKey().hashCode(); | |
} | |
@Override | |
public VH onCreateViewHolder(ViewGroup parent, int viewType) { | |
ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(mModelLayout, parent, false); | |
try { | |
Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class); | |
return constructor.newInstance(view); | |
} catch (NoSuchMethodException e) { | |
throw new RuntimeException(e); | |
} catch (InvocationTargetException e) { | |
throw new RuntimeException(e); | |
} catch (InstantiationException e) { | |
throw new RuntimeException(e); | |
} catch (IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
public void onBindViewHolder(VH viewHolder, int position) { | |
T model = getItem(position); | |
populateViewHolder(viewHolder, model, position); | |
} | |
abstract protected void populateViewHolder(VH viewHolder, T model, int position); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment