Skip to content

Instantly share code, notes, and snippets.

@renaudcerrato
Last active April 7, 2021 14:50
Show Gist options
  • Save renaudcerrato/bffb4ea49d32400319b1 to your computer and use it in GitHub Desktop.
Save renaudcerrato/bffb4ea49d32400319b1 to your computer and use it in GitHub Desktop.
Lightweight ArrayAdapter for RecyclerView.
package recyclerview.adapter;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public abstract class ArrayRecyclerAdapter<T, VH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<VH> {
/**
* Contains the list of objects that represent the data of this ArrayAdapter.
* The content of this list is referred to as "the array" in the documentation.
*/
private List<T> mObjects;
/**
* Lock used to modify the content of {@link #mObjects}. Any write operation
* performed on the array should be synchronized on this lock.
*/
private final Object mLock = new Object();
/**
* Indicates whether or not {@link #notifyDataSetChanged()} must be called whenever
* {@link #mObjects} is modified.
*/
private boolean mNotifyOnChange = true;
public ArrayRecyclerAdapter() {
this(new ArrayList<T>());
}
public ArrayRecyclerAdapter(T[] objects) {
this(Arrays.asList(objects));
}
public ArrayRecyclerAdapter(List<T> objects) {
mObjects = objects;
}
/**
* 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) {
int pos;
synchronized (mLock) {
pos = getItemCount();
mObjects.add(object);
}
if (mNotifyOnChange) notifyItemInserted(pos);
}
/**
* 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) {
int pos;
synchronized (mLock) {
pos = getItemCount();
mObjects.addAll(collection);
}
if (mNotifyOnChange) notifyItemRangeInserted(pos, collection.size());
}
/**
* Adds the specified items at the end of the array.
*
* @param items The items to add at the end of the array.
*/
public void addAll(T ... items) {
int start;
synchronized (mLock) {
start = getItemCount();
Collections.addAll(mObjects, items);
}
if (mNotifyOnChange) notifyItemRangeInserted(start, items.length);
}
/**
* Inserts the specified object at the specified index in the array.
*
* @param object The object to insert into the array.
* @param index The index at which the object must be inserted.
*/
public void insert(T object, int index) {
synchronized (mLock) {
mObjects.add(index, object);
}
if (mNotifyOnChange) notifyItemInserted(index);
}
/**
* Removes the specified object from the array.
*
* @param object The object to remove.
*/
public void remove(T object) {
int pos;
synchronized (mLock) {
pos = getPosition(object);
if(pos == -1) return;
mObjects.remove(pos);
}
if (mNotifyOnChange) notifyItemRemoved(pos);
}
/**
* Remove all elements from the list.
*/
public void clear() {
synchronized (mLock) {
mObjects.clear();
}
if (mNotifyOnChange) notifyDataSetChanged();
}
/**
* Sorts the content of this adapter using the specified comparator.
*
* @param comparator The comparator used to sort the objects contained
* in this adapter.
*/
public void sort(Comparator<? super T> comparator) {
synchronized (mLock) {
Collections.sort(mObjects, comparator);
}
if (mNotifyOnChange) notifyDataSetChanged();
}
/**
* Control whether methods that change the list ({@link #add},
* {@link #insert}, {@link #remove}, {@link #clear}) automatically call
* {@link #notifyDataSetChanged}. If set to false, caller must
* manually call notifyDataSetChanged() to have the changes
* reflected in the attached view.
*
* The default is true, and calling notifyDataSetChanged()
* resets the flag to true.
*
* @param notifyOnChange if true, modifications to the list will
* automatically call {@link
* #notifyDataSetChanged}
*/
public void setNotifyOnChange(boolean notifyOnChange) {
mNotifyOnChange = notifyOnChange;
}
/**
* {@inheritDoc}
*/
public int getItemCount() {
return mObjects.size();
}
/**
* {@inheritDoc}
*/
public T getItem(int position) {
return mObjects.get(position);
}
/**
* Returns the position of the specified item in the array.
*
* @param item The item to retrieve the position of.
*
* @return The position of the specified item.
*/
public int getPosition(T item) {
return mObjects.indexOf(item);
}
/**
* {@inheritDoc}
*/
public long getItemId(int position) {
return position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment