Created
March 15, 2016 15:13
-
-
Save eymar/e8856a8bd5922f72e9a4 to your computer and use it in GitHub Desktop.
Base Array Adapter for Recycler Views
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 BaseRecyclerArrayAdapter<T> extends RecyclerView.Adapter<BaseRecyclerArrayAdapter.ViewHolder<T>> { | |
private List<T> dataSet; | |
private Context context; | |
public BaseRecyclerArrayAdapter(List<T> dataSet, Context context) { | |
super(); | |
this.dataSet = new ArrayList<>(dataSet); | |
this.context = context; | |
} | |
public Context context() { | |
return context; | |
} | |
public static abstract class ViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener { | |
public ViewHolder(View itemView) { | |
super(itemView); | |
itemView.setClickable(true); | |
itemView.setOnClickListener(this); | |
} | |
public abstract void setEntity(T entity); | |
@Override | |
public void onClick(View v) { | |
int position = getAdapterPosition(); | |
} | |
} | |
public void applyFilterResult(List<T> results) { | |
animateTo(results); | |
} | |
public void onItemSelected(T entity) {} | |
public abstract int getLayoutId(); | |
public T getEntity(int position) { | |
return dataSet.get(position); | |
} | |
@Override | |
public ViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()) | |
.inflate(getLayoutId(), parent, false); | |
return newViewHolderInstance(v); | |
} | |
public abstract ViewHolder<T> newViewHolderInstance(View v); | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
holder.setEntity(dataSet.get(position)); | |
} | |
@Override | |
public int getItemCount() { | |
return dataSet.size(); | |
} | |
public void removeItem(T item) { | |
int i = dataSet.indexOf(item); | |
removeItem(i); | |
} | |
public void addItem(T item) { | |
addItem(dataSet.size(), item); | |
} | |
public T removeItem(int position) { | |
final T model = dataSet.remove(position); | |
notifyItemRemoved(position); | |
return model; | |
} | |
public void addItem(int position, T model) { | |
dataSet.add(position, model); | |
notifyItemInserted(position); | |
} | |
public void moveItem(int fromPosition, int toPosition) { | |
final T model = dataSet.remove(fromPosition); | |
dataSet.add(toPosition, model); | |
notifyItemMoved(fromPosition, toPosition); | |
} | |
public void animateTo(List<T> models) { | |
applyAndAnimateRemovals(models); | |
applyAndAnimateAdditions(models); | |
applyAndAnimateMovedItems(models); | |
} | |
private void applyAndAnimateRemovals(List<T> newModels) { | |
for (int i = dataSet.size() - 1; i >= 0; i--) { | |
final T model = dataSet.get(i); | |
if (!newModels.contains(model)) { | |
removeItem(i); | |
} | |
} | |
} | |
private void applyAndAnimateAdditions(List<T> newModels) { | |
for (int i = 0, count = newModels.size(); i < count; i++) { | |
final T model = newModels.get(i); | |
if (!dataSet.contains(model)) { | |
addItem(i, model); | |
} | |
} | |
} | |
private void applyAndAnimateMovedItems(List<T> newModels) { | |
for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) { | |
final T model = newModels.get(toPosition); | |
final int fromPosition = dataSet.indexOf(model); | |
if (fromPosition >= 0 && fromPosition != toPosition) { | |
moveItem(fromPosition, toPosition); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment