Created
November 12, 2017 08:12
-
-
Save quangson91/8738ae2bebda64edfa4343a029e4890e to your computer and use it in GitHub Desktop.
Custom multi view types for RecycleView
This file contains hidden or 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
| @SuppressWarnings("unchecked") | |
| public class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.ViewHolder> { | |
| private final List<Wrapper<?>> items; | |
| public BaseAdapter() { | |
| this.items = new ArrayList<>(); | |
| } | |
| @Override | |
| public int getItemViewType(int position) { | |
| return items.get(position).viewType; | |
| } | |
| @Override | |
| public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
| return ViewType.values()[viewType].onCreateViewHolder(parent); | |
| } | |
| @Override | |
| public void onBindViewHolder(ViewHolder holder, int position) { | |
| holder.onBind(items.get(position)); | |
| } | |
| @Override | |
| public int getItemCount() { | |
| return items.size(); | |
| } | |
| public void simulateAddItems() { | |
| items.add(new Wrapper<>(ViewType.USER.viewTYpe(), new User())); | |
| items.add(new Wrapper<>(ViewType.USER.viewTYpe(), new User())); | |
| items.add(new Wrapper<>(ViewType.USER.viewTYpe(), new User())); | |
| items.add(new Wrapper<>(ViewType.BOTTOM.viewTYpe(), new BottomData())); | |
| notifyDataSetChanged(); | |
| } | |
| public enum ViewType { | |
| USER { | |
| @Override | |
| public ViewHolder onCreateViewHolder(ViewGroup parent) { | |
| return new UserViewHolder(itemView); | |
| } | |
| }, | |
| BOTTOM { | |
| @Override | |
| public ViewHolder onCreateViewHolder(ViewGroup parent) { | |
| return new BottomViewHolder(itemView); | |
| } | |
| }; | |
| public abstract ViewHolder onCreateViewHolder(ViewGroup parent); | |
| public int viewTYpe() { | |
| return ordinal(); | |
| } | |
| } | |
| static class Wrapper<T> { | |
| final int viewType; | |
| final T data; | |
| public Wrapper(int viewType, T data) { | |
| this.viewType = viewType; | |
| this.data = data; | |
| } | |
| } | |
| public static class ViewHolder<T> extends RecyclerView.ViewHolder { | |
| private T item; | |
| public ViewHolder(View itemView) { | |
| super(itemView); | |
| } | |
| public void onBind(T item) { | |
| } | |
| } | |
| public static class UserViewHolder extends ViewHolder<User> { | |
| public UserViewHolder(View itemView) { | |
| super(itemView); | |
| } | |
| @Override | |
| public void onBind(User item) { | |
| super.onBind(item); | |
| } | |
| } | |
| public static class BottomViewHolder extends ViewHolder<BottomData> { | |
| public BottomViewHolder(View itemView) { | |
| super(itemView); | |
| } | |
| @Override | |
| public void onBind(BottomData item) { | |
| super.onBind(item); | |
| } | |
| } | |
| static class User { | |
| // user fields & method | |
| } | |
| static class BottomData { | |
| // bottom data fields & method | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can see the adapter have something special:
The data of Adapter is generic & extends from
Wrapperclass. TheWrapperclass simply POJO class contain two fieldsviewType&itemdata. TheviewTypewill be passing viagetItemViewType(int position)and theitemis data for each view holder.The generic
ViewHolderwithTis kind of data for the view holder. You can see two custom view holderUserViewHolderandBottomViewHolderextends fromViewHolderwith this data. (So inonBindof each view holder will have exact data they want to use -> no need to cast. Because we already in cast).The enum
ViewTypecontain all view type of adapter. Also, in view type, we have methodViewHolder onCreateViewHolder(ViewGroup parent)so we can easier make viewHolder fromViewType. I think it is easier for our eyes to see the Holder relative to the ViewType.Also, we have method
viewType()it simply returnordinal()of enum.As a reference in
java docsthat method may not use. But in this case, I think it is fair enough to use (because it is unique).So in the method
onCreateViewHolderof Adapter we can simple call:return ViewType.values()[viewType].onCreateViewHolder(parent);and in
onBindViewHolderwe will callholder.onBind(items.get(position));.Finally, I made
simulateAddItemsfor demo how we add item to the adapter.I hope that useful & help other people