Skip to content

Instantly share code, notes, and snippets.

@tysheng
Created December 16, 2016 01:49
Show Gist options
  • Save tysheng/ef2bdb3ac060811eda888465babaaeca to your computer and use it in GitHub Desktop.
Save tysheng/ef2bdb3ac060811eda888465babaaeca to your computer and use it in GitHub Desktop.
a new MultiItemAdapter for BaseQuickAdapter(2.6.6)
/**
* Created by tysheng
* Date: 2016/11/18 10:59.
* Email: tyshengsx@gmail.com
*/
public abstract class BaseMultiItemAdapter<T> extends BaseRecyclerViewAdapter<T> {
private static final int DEFAULT_VIEW_TYPE = -255;
private SparseIntArray layouts;
public BaseMultiItemAdapter(List<T> data) {
super(data);
}
protected int getDefItemViewType(int position) {
T item = this.mData.get(position);
return item != null ? getItemType(item) : DEFAULT_VIEW_TYPE;
}
protected abstract int getItemType(T t);
protected StyBaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
return createBaseViewHolder(parent, this.getLayoutId(viewType));
}
private int getLayoutId(int viewType) {
return this.layouts.get(viewType);
}
private void addItemType(int type, @LayoutRes int layoutResId) {
if (this.layouts == null) {
this.layouts = new SparseIntArray();
}
this.layouts.put(type, layoutResId);
}
protected BaseMultiItemAdapter registerItemType(@LayoutRes int... layoutResIds) {
for (int i = 0; i < layoutResIds.length; i++) {
addItemType(i, layoutResIds[i]);
}
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment