Last active
September 21, 2019 19:26
-
-
Save mirrajabi/c70ec2b41b44fbe1c8cbf80771f9a618 to your computer and use it in GitHub Desktop.
Generic RecyclerView Adapter boilerplate.
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
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 | |
} | |
} |
the EnumsHelper class? and xml for item?
What is EnumsHelper ?
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
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(...);