公共适配器的设计
背景:
电商类的应用可能会有千变万幻的业务需求。举个例子首页是整个应用的重要入口,所以这个页面产品对于业务区块的设计是多种多样的,现在已经差不多有将近20多种的样式view组成后面有可能会继续增加,而现在很多view的代码实现是在Adapter实现的,造成的后果是Adapter的类相当的臃肿,而且判断分支多,增加一个类型对于原有的类就要进行修改,不符合设计的开闭原则,而且view的复用性相对较差。在此背景下重新设计一个通用的适配。
需求和目标:
1.全站统一使用一个Adapter。
2.view和数据model构建一个Holder单一的对象用于复用,脱离和Adapter的绑定
核心类设计:
1.MultiTypeFactory是一个holde创建的工厂类,主要的功能是根据Modle类型的管理和holder类型管理,然后根据adapter传回的Modle类型进行已经注册的Holder的进行生产不同类型的holder创建。通过
public class MultiTypeFactory implements TypeFactory {
private List<Class<Type>> mTypes = new ArrayList<>();
private List<Class<BaseViewHolder>> mHolders = new ArrayList<>();
private List<Integer> mLayoutReses = new ArrayList<>();
private Map<Integer, Class<View>> mViewMap = new HashMap<>();
@Override
public BaseViewHolder createViewHolder(Context context, int type) {
try {
View view;
Class h = mHolders.get(type);
Class c = mViewMap.get(mLayoutReses.get(type));
if (c != null && !c.equals(NullView.class)) {
Constructor constructor = c.getConstructor(Context.class);
view = (View) constructor.newInstance(context);
} else {
view = View.inflate(context, mLayoutReses.get(type), null);
}
Constructor constructor = h.getConstructor(View.class);
return (BaseViewHolder) constructor.newInstance(view);
} catch ( NoSuchMethodException e ) {
e.printStackTrace();
} catch ( InstantiationException e ) {
e.printStackTrace();
} catch ( IllegalAccessException e ) {
e.printStackTrace();
} catch ( InvocationTargetException e ) {
e.printStackTrace();
}
return null;
}
@Override
public int indexOfType(Class c) {
return mTypes.indexOf(c);
}
private TypeFactory regist(Class c, Class h, int layoutRes) {
mTypes.add(c);
mHolders.add(h);
mLayoutReses.add(layoutRes);
return this;
}
private TypeFactory regist(Class c, Class h, Class v, int layoutRes) {
regist(c, h, layoutRes);
mViewMap.put(layoutRes, v);
return this;
}
@Override
public TypeFactory regist(Class<? extends BaseViewHolder> h) {
if (h == null) {
throw new NullPointerException("holder should not be null");
}
if (mHolders.contains(h)) {
throw new IllegalStateException("this holder has been Registed");
}
ModelView mv = h.getAnnotation(ModelView.class);
Class c = mv.model();
Class v = mv.view();
int l = mv.resId();
regist(c, h, v, l);
return this;
}
@Override
public int typeSize() {
return mTypes.size();
}
}
2.LvMultiTypeAdapter listview的公共适配器设计,可全站工程使用不用再费劲的创建一个个adapter了
public class LvMultiTypeAdapter extends BaseAdapter implements Adapter {
private TypeFactory typeFactory;
private List<Type> models=new ArrayList<>();
private DotContext mDotContext = null;
private Handler mExternalSafeHandler;
private Handler mInsideSafeHandler;
private LoadMoreModel mMoreModel = new LoadMoreModel();
public LvMultiTypeAdapter(List<Type> models, TypeFactory typeFactoryt) {
this.models = models;
this.typeFactory = typeFactoryt;
}
public LvMultiTypeAdapter(TypeFactory typeFactory) {
this.typeFactory = typeFactory;
typeFactory.regist(LoadMoreHolder.class);
}
public void addData(List<Type> types) {
if (ObjectUtils.isObjectNotNull(models)) {
models.addAll(types);
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return isModelsNotNull() ? models.size() : 0;
}
@Override
public Type getItem(int position) {
return isModelsNotNull() ? models.get(position) : null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return isModelsNotNull() ? gainViewType(position) : -1;
}
private int gainViewType(int position) {
return typeFactory.indexOfType(models.get(position).getClass());
}
@Override
public int getViewTypeCount() {
return typeFactory.typeSize();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BaseViewHolder baseViewHolder = typeFactory.createViewHolder(parent.getContext(), gainViewType(position));
baseViewHolder.bindVM(models.get(position), position, this);
return baseViewHolder.itemView;
}
....
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
3.MultiTypeAdapter当然也少不了recycleview的实现,强烈建议全站使用recycleview去实现
public class MultiTypeAdapter extends RecyclerView.Adapter<BaseViewHolder> implements Adapter {
private TypeFactory typeFactory;
public List<Type> getModels() {
return models;
}
public void setModels(List<Type> models) {
this.models = models;
}
private List<Type> models = new ArrayList<>();
private DotContext mDotContext = null;
private Handler mExternalSafeHandler;
private Handler mInsideSafeHandler;
private LoadMoreModel mMoreModel = new LoadMoreModel();
public MultiTypeAdapter(List<Type> models, TypeFactory typeFactory) {
this.models = models;
this.typeFactory = typeFactory;
typeFactory.regist(LoadMoreHolder.class);
}
public MultiTypeAdapter(TypeFactory typeFactory) {
this.typeFactory = typeFactory;
typeFactory.regist(LoadMoreHolder.class);
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return typeFactory.createViewHolder(parent.getContext(), viewType);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
holder.bindVM(models.get(position), position, this);
}
....
public void clear() {
if (ObjectUtils.isObjectNotNull(models)) {
models.clear();
}
notifyDataSetChanged();
}
}
4.BaseViewHolder全局基础holder,调用者只需关心数据模型和视图的绑定添加了 model数据和view的注解实现,可以让工厂累获取通过反射拿到不同类型holder的实例
public abstract class BaseViewHolder<T extends Type> extends RecyclerView.ViewHolder {
private SparseArray<View> views;
public View mItemView;
protected BaseDotBuilder dotBuilder;
protected DotContext doter;
public <E extends View> E getView(int resID) {
E view = (E) views.get(resID);
if (view == null) {
view = (E) mItemView.findViewById(resID);
views.put(resID, view);
}
return view;
}
public abstract void bindVM(T model, int position, Adapter adapter);
public void bindHolder(T model, int position, Adapter adapter) {
bindAction(adapter, position);
bindVM(model, position, adapter);
}
public BaseViewHolder(View itemView) {
super(itemView);
views = new SparseArray<>();
this.mItemView = itemView;
}
...
protected ExposureTrack bindExposureTrack(T t, ExposureTrack e) {
return null;
}
}
5.Adapter 为了BaseViewHolde适配listview 和 recycleView创建适配器接口
public interface Adapter {
int getItemSize();
int getType(int position);
DotContext getDoter();
void setDoter(DotContext dotContext);
int getItemCounts();
Handler getExternalHandler();
HolderAction getHolderCallBack();
void registInsideHandler(Handler safeHandler);
Handler getInsideHandler();
void notifyDataChanged();
}
- HolderAction事件回调接口给外部调用,暴露holde给调用者,可以在数据绑定的时候提供给修改功能增加扩展性
public interface HolderAction {
void onBindAction(BaseViewHolder holder, int pos);
void onAfterAction(BaseViewHolder holder, int pos, int actionType);
}
- Type 所有Holder对应的数据模型都实现这个类型接口
/**
* 适配器item基础类型
* Created by hzzhudiqing on 2017/2/23.
*/
public interface Type {
}
- DataType<T> 数据模型的包装类用于父子级数据关联,方便 type模型数据的构造
/**
* Created by hzzhudiqing on 2017/3/3.
*/
public class DataType<T> implements Type {
T t;
public T getT() {
return t;
}
public DataType<T> setT(T t) {
this.t = t;
return this;
}
}
使用方式
模型数据的创建
public class DeliveryGood extends DataType<Delivery> implements Type, Serializable {
.....
}
业务相关holder创建
@ModelView(model = DeliveryGood.class, resId = R.layout.pay_delivery_good)
public class DeliveryGoodHolder extends BaseViewHolder<DeliveryGood> {
public static final int PRE_SALE = 1;
private TextView mPayDeliveryGoodName;
private KaolaImageView mPayDeliveryGoodSaleReturnIcon;
private TextView mPayDeliveryGoodSaleReturnDesc;
private TextView mPayDeliveryGoodPriceTv;
private TextView mPayDeliveryGoodNumTv;
private TextView mPaySkutv;
public DeliveryGoodHolder(View itemView) {
super(itemView);
}
@Override
public void bindVM(DeliveryGood model, int position, Adapter adapter) {
...
}
...
}
Adapter的注册调用
List<Type> types = DeliverProcesser.bulidDeliveryTypes(deliveries);
final TypeFactory factory = new MultiTypeFactory()
.regist(DeliveryGoodHolder.class)
.regist(DeliveryWarehouseHolder.class)
.regist(RemarkHolder.class);
final MultiTypeAdapter multiTypeAdapter = new MultiTypeAdapter(types, factory);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 1);
mDeliveryRv.setLayoutManager(layoutManager);
mDeliveryRv.setAdapter(multiTypeAdapter);
效果图: