Last active
February 1, 2018 14:00
-
-
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.
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
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(); | |
} | |
} |
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
You can change the type of generics from
BaseRecyclerAdapter<T, VH extends BaseViewHolder>
toBaseRecyclerAdapter<T, VH extends RecyclerView.ViewHolder>
How to Use this:
Assume I have a POJO class User and a ViewHolder UserViewHolder