Created
February 26, 2016 18:59
-
-
Save sdsantos/7bc421f805267dcb14ed to your computer and use it in GitHub Desktop.
Generic Android Recycler List Adapter with click listener
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.support.annotation.LayoutRes; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.util.List; | |
public class RecyclerAdapter<T, VH extends RecyclerViewHolder<T>> extends RecyclerView.Adapter<VH> { | |
private final List<T> adapterList; | |
private final @LayoutRes int layoutRes; | |
private final Class<VH> klass; | |
private final RecyclerItemClickListener clickListener; | |
public RecyclerAdapter(Class<VH> klass, List<T> list, @LayoutRes int layoutRes) { | |
this(klass, list, layoutRes, null); | |
} | |
public RecyclerAdapter(Class<VH> klass, List<T> list, @LayoutRes int layoutRes, | |
RecyclerItemClickListener clickListener) { | |
adapterList = list; | |
this.layoutRes = layoutRes; | |
this.klass = klass; | |
this.clickListener = clickListener; | |
} | |
@Override public VH onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false); | |
try { | |
VH holder = klass.getConstructor(View.class, RecyclerItemClickListener.class) | |
.newInstance(view, clickListener); | |
return holder; | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override public void onBindViewHolder(VH holder, int position) { | |
holder.bind(getItem(position), position); | |
} | |
@Override public int getItemCount() { | |
return adapterList.size(); | |
} | |
protected T getItem(int position) { | |
return adapterList.get(position); | |
} | |
} |
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 interface RecyclerItemClickListener<T> { | |
void onItemClick(T item); | |
} |
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.support.annotation.CallSuper; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public abstract class RecyclerViewHolder<T> extends RecyclerView.ViewHolder { | |
private final RecyclerItemClickListener<T> clickListener; | |
public RecyclerViewHolder(View itemView, RecyclerItemClickListener<T> clickListener) { | |
super(itemView); | |
this.clickListener = clickListener; | |
} | |
@CallSuper public void bind(final T item, int position) { | |
if (clickListener != null) { | |
itemView.setOnClickListener(new View.OnClickListener() { | |
@Override public void onClick(View v) { | |
clickListener.onItemClick(item); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@alphaDroid89 I now recommend to use a library like Epoxy (https://github.com/airbnb/epoxy) to simplify the usage of RecyclerViews.