Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ListRecyclerAdapter.java
public abstract class BindViewHolder<T> extends RecyclerView.ViewHolder {
public BindViewHolder(View itemView) {
super(itemView);
}
abstract void onBind(int position, T item);
}
public class IconViewHolder extends BindViewHolder<String> {
@InjectView(R.id.icon)
public SimpleDraweeView icon;
public IconViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
}
@Override
public void onBind(int position, String item) {
icon.setImageUIconViewHolderRI(Uri.parse(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);
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
if (mOnCreateViewHolder != null) return mOnCreateViewHolder.call(parent, viewType);
return null;
}
protected ListRecyclerAdapter<T, VH> createViewHolder(Func2<ViewGroup, Integer, VH> onCreateViewHolder) {
mOnCreateViewHolder = onCreateViewHolder;
return this;
}
private boolean onBindViewHolderSupered;
@Override
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);
}
mOnBindViewHolder.call(viewHolder, position, item);
}
public ListRecyclerAdapter<T, VH> bindViewHolder(Action3<VH, Integer, T> onBindViewHolder) {
mOnBindViewHolder = onBindViewHolder;
return this;
}
@Override
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));
icons.setAdapter(listAdapter);
listAdapter.getList().addAll(Arrays.asList("http://example.com/a.png"));
listAdapter.notifyDataSetChanged(); // TODO hook List.add(), List.addAll(), etc. modifitable operations
@yongjhih

This comment has been minimized.

Copy link
Owner Author

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.