Last active
August 29, 2015 14:18
-
-
Save yongjhih/d8db8c69190293098eec to your computer and use it in GitHub Desktop.
ListRecyclerAdapter.java
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 abstract class BindViewHolder<T> extends RecyclerView.ViewHolder { | |
public BindViewHolder(View itemView) { | |
super(itemView); | |
} | |
abstract void onBind(int position, 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
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)); | |
} | |
} | |
} |
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 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(); | |
} | |
} |
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/yongjhih/recyclerlist