Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generic RecyclerView Adapter boilerplate.
import android.content.Context;
import android.support.annotation.AnimRes;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import java.util.ArrayList;
import java.util.List;
public class BaseItemHolderAdapter<T> extends RecyclerView.Adapter<BaseItemHolderAdapter.ViewHolder> {
protected Context context;
private List<T> objects = new ArrayList<>();
private LayoutInflater layoutInflater;
private int lastShownItem = -1;
private int layout;
private View animatableView;
private Animation animation;
private View.OnClickListener onClickListener;
private int animatableViewRes;
private int animationRes;
private AdapterViewBinder<T> viewBinder;
public BaseItemHolderAdapter(Context context, @LayoutRes int layout, List<T> objects) {
this(context,layout,null,0,0,objects);
}
public BaseItemHolderAdapter(Context context,AdapterViewBinder<T> viewBinder, @LayoutRes int layout, List<T> objects) {
this(context,layout,viewBinder,0,0,objects);
}
public BaseItemHolderAdapter(Context context, @LayoutRes int layout, @Nullable AdapterViewBinder<T> viewBinder, @IdRes int animatableView, @AnimRes int animation, List<T> objects) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
this.objects = objects;
this.layout = layout;
this.animatableViewRes = animatableView;
this.animationRes = animation;
this.viewBinder = viewBinder;
}
public List<T> getObjects() {
return objects;
}
public void setObjects(List<T> objects) {
this.objects = objects;
notifyDataSetChanged();
lastShownItem = -1;
}
public void addObjects(EnumsHelper.ListPostion position, T... items){
for (T item : items) {
addObject(position,item);
}
}
public void addObjects(EnumsHelper.ListPostion position,ArrayList<T> items){
for (T item : items) {
addObject(position,item);
}
}
public void addObject(EnumsHelper.ListPostion position, T item) {
if(objects.contains(item)) return;
switch (position) {
case Top:
this.objects.add(0, item);
this.notifyItemInserted(0);
break;
case Bottom:
this.objects.add(item);
this.notifyItemInserted(this.objects.size() - 1);
break;
}
//notifyDataSetChanged();
}
public boolean removeObjectAtPosition(int position) {
if(position >= objects.size())
return false;
objects.remove(position);
this.notifyItemRemoved(position);
return true;
}
public T getItem(int position) {
return objects.get(position);
}
@Override
public int getItemCount() {
return objects.size();
}
@Override
public long getItemId(int position) {
return position;
}
public BaseItemHolderAdapter<T> setViewBinder(AdapterViewBinder<T> viewBinder) {
this.viewBinder = viewBinder;
return this;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View convertView = layoutInflater.inflate(layout, parent, false);
convertView.setTag(new ViewHolder(convertView));
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
return viewHolder;
}
@Override
public void onBindViewHolder(BaseItemHolderAdapter.ViewHolder holder, int position) {
initializeViews(getItem(position), holder, position);
}
private void initializeViews(final T object, final BaseItemHolderAdapter.ViewHolder holder, final int position) {
if(animatableViewRes != 0 && animationRes != 0) {
setAnimatableView(holder, animatableViewRes, animationRes);
setAnimation(position);
}
if(viewBinder != null)
viewBinder.bind(holder, object, position);
if (onClickListener != null)
holder.getBaseView().setOnClickListener(onClickListener);
}
@Override
public void onViewDetachedFromWindow(BaseItemHolderAdapter.ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
if(animatableView != null)
holder.clearAnimation(animatableView.getId());
}
public View.OnClickListener getOnClickListener(){
return onClickListener;
}
public void setOnClickListener(View.OnClickListener onClickListener){
this.onClickListener = onClickListener;
}
private void setAnimation(int position) {
if(animatableView != null && animation != null) {
if (position > lastShownItem) {
animatableView.startAnimation(animation);
lastShownItem = position;
}
}
}
public void setAnimatableView(ViewHolder holder,@IdRes int viewId,@AnimRes int animationId) {
animation = AnimationUtils.loadAnimation(context, animationId);
animatableView = holder.getViewById(viewId);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View _baseView;
public ViewHolder(View view) {
super(view);
_baseView = view;
}
public View getBaseView() {
return _baseView;
}
public View getViewById(@IdRes int id){
return _baseView.findViewById(id);
}
public void clearAnimation(@IdRes int id)
{
_baseView.findViewById(id).clearAnimation();
}
}
public interface AdapterViewBinder<T> {
void bind(ViewHolder holder, T item, int position);
}
public enum ListPostion {
TOP,
BOTTOM
}
}
@mirrajabi

This comment has been minimized.

Copy link
Owner Author

mirrajabi commented Dec 5, 2016

Sample usage :
BaseItemHolderAdapter<$MODEL$> adapter = new BaseItemHolderAdapter<$MODEL$>(getContext(), R.layout.$Layout$, new ArrayList<$MODEL$>());

Now you just need to set the view binder like this :
adapter.setViewBinder(...);

@cliffgr

This comment has been minimized.

Copy link

cliffgr commented May 20, 2017

the EnumsHelper class? and xml for item?

@denzcoskun

This comment has been minimized.

Copy link

denzcoskun commented Jul 21, 2017

What is EnumsHelper ?

@diogosq

This comment has been minimized.

Copy link

diogosq commented Aug 24, 2017

Has a error in if(animationRes != 0 && animationRes != 0). Maybe you wanted to say if (animatableViewRes != 0 && animationRes != 0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.