Skip to content

Instantly share code, notes, and snippets.

@iChintanSoni
Last active February 1, 2018 14:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iChintanSoni/3c61aea787ae4bd49f26adee9dd40a08 to your computer and use it in GitHub Desktop.
Save iChintanSoni/3c61aea787ae4bd49f26adee9dd40a08 to your computer and use it in GitHub Desktop.
This implementation of generic recycler adapter is truely inspired from Firebase Database UI Adapter.
package com.chintansoni.android.mesamis.base;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseRecyclerAdapter<T, VH extends BaseViewHolder> extends RecyclerView.Adapter<VH> {
private int mModelLayout;
private Class<VH> mViewHolderClass;
private List<T> mList = new ArrayList<>();
public BaseRecyclerAdapter(Class<VH> viewHolderClass, int modelLayout) {
mModelLayout = modelLayout;
mViewHolderClass = viewHolderClass;
}
public void setmList(List<T> mList) {
this.mList = mList;
}
@Override
public int getItemCount() {
return mList.size();
}
public T getItem(int position) {
return mList.get(position);
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
try {
Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class);
return constructor.newInstance(view);
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public void onBindViewHolder(VH viewHolder, int position) {
T model = getItem(position);
populateViewHolder(viewHolder, model, position);
}
@Override
public int getItemViewType(int position) {
return mModelLayout;
}
abstract protected void populateViewHolder(VH viewHolder, T model, int position);
public void addItem(T t) {
int index = mList.size();
mList.add(index, t);
notifyItemInserted(index);
}
public void updateItem(int position, T t) {
mList.set(position, t);
notifyItemChanged(position);
}
public void removeItem(int position) {
mList.remove(position);
notifyItemRemoved(position);
}
public void setItems(List<T> items) {
mList = items;
notifyDataSetChanged();
}
}
@iChintanSoni
Copy link
Author

You can change the type of generics from
BaseRecyclerAdapter<T, VH extends BaseViewHolder> to
BaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder>

How to Use this:

Assume I have a POJO class User and a ViewHolder UserViewHolder

private BaseRecyclerAdapter<User, UserViewHolder> mBaseRecyclerAdapter = new BaseRecyclerAdapter<User, UserViewHolder>(UserViewHolder.class, R.layout.layout_list_item_friends) {
    @Override
    protected void populateViewHolder(UserViewHolder viewHolder, User model, int position) {
        // You can do here whatever you want
    }
};

@ashkanpower
Copy link

Just check my library, it is pretty simple to use and needs no code for adapters or holders :
https://github.com/ashkanpower/SURAdapter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment