Skip to content

Instantly share code, notes, and snippets.

@alorma
Last active December 26, 2015 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alorma/c02d6647f4773f5a9e4e to your computer and use it in GitHub Desktop.
Save alorma/c02d6647f4773f5a9e4e to your computer and use it in GitHub Desktop.
public abstract class ArrayAdapterRecycler<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private List<T> items;
protected ArrayAdapterRecycler(List<T> items) {
this.items = items;
}
@Override
public int getItemCount() {
return items != null ? items.size() : 0;
}
public T getItem(int position) {
return items.get(position);
}
/**
* Adds the specified object at the end of the array.
*
* @param object The object to add at the end of the array.
*/
public void add(T object) {
items.add(object);
notifyItemInserted(items.size() - 1);
}
/**
* Adds the specified Collection at the end of the array.
*
* @param collection The Collection to add at the end of the array.
*/
public void addAll(Collection<? extends T> collection) {
items.addAll(collection);
notifyDataSetChanged();
}
/**
* Removes the specified object from the array.
*
* @param position The position to remove.
*/
public void remove(int position) {
if (position >= 0 && position < getItemCount()) {
items.remove(position);
notifyItemRemoved(position);
}
}
/**
* Removes the specified object from the array.
*
* @param object The object to remove.
*/
public void remove(T object) {
int index = items.indexOf(object);
remove(index);
}
/**
* Remove all elements from the list.
*/
public void clear() {
items.clear();
notifyDataSetChanged();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment