Last active
December 26, 2015 23:08
-
-
Save alorma/c02d6647f4773f5a9e4e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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