Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public abstract class BindViewHolder<T> extends RecyclerView.ViewHolder {
public BindViewHolder(View itemView) {
abstract void onBind(int position, T item);
public class IconViewHolder extends BindViewHolder<String> {
public SimpleDraweeView icon;
public IconViewHolder(View itemView) {
ButterKnife.inject(this, itemView);
public void onBind(int position, String item) {
public class ListRecyclerAdapter<T, VH extends BindViewHolder<T>> extends RecyclerView.Adapter<VH> {
private List<T> mList = Collections.emptyList();
protected Action3<VH, Integer, T> mOnBindViewHolder;
protected Func2<ViewGroup, Integer, VH> mOnCreateViewHolder;
public ListRecyclerAdapter(List<T> list) {
mList = list;
public static <R, VHH extends BindViewHolder<R>> ListRecyclerAdapter<R, VHH> create() {
return create(new ArrayList<>());
public static <R, VHH extends BindViewHolder<R>> ListRecyclerAdapter<R, VHH> create(List<R> list) {
return new ListRecyclerAdapter<>(list);
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
if (mOnCreateViewHolder != null) return, viewType);
return null;
protected ListRecyclerAdapter<T, VH> createViewHolder(Func2<ViewGroup, Integer, VH> onCreateViewHolder) {
mOnCreateViewHolder = onCreateViewHolder;
return this;
private boolean onBindViewHolderSupered;
public void onBindViewHolder(VH viewHolder, int position) { // final, DO NOT Override until certainly
onBindViewHolderSupered = false;
onBindViewHolder(viewHolder, position, mList.get(position));
if (!onBindViewHolderSupered) throw new IllegalArgumentException("super.onBindViewHolder() not be called");
// super me
public void onBindViewHolder(VH viewHolder, int position, T item) { // final, DO NOT Override until certainly
onBindViewHolderSupered = true;
if (mOnBindViewHolder == null) {
mOnBindViewHolder = (v, p, i) -> v.onBind(p, i);
}, position, item);
public ListRecyclerAdapter<T, VH> bindViewHolder(Action3<VH, Integer, T> onBindViewHolder) {
mOnBindViewHolder = onBindViewHolder;
return this;
public int getItemCount() {
return mList.size();
ListRecyclerAdapter<String, IconViewHolder> listAdapter = ListRecyclerAdapter.create();
listAdapter.createViewHolder((parent, viewType) -> new IconViewHolder(LayoutInflater.from(context).inflate(R.layout.item_icon, parent, false)));
icons.setLayoutManager(new LinearLayoutManager(activity));
listAdapter.notifyDataSetChanged(); // TODO hook List.add(), List.addAll(), etc. modifitable operations

This comment has been minimized.

Copy link
Owner Author

yongjhih commented Jun 29, 2015

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.