公共適配器的設(shè)計(jì)
背景:
電商類的應(yīng)用可能會(huì)有千變?nèi)f幻的業(yè)務(wù)需求。舉個(gè)例子首頁(yè)是整個(gè)應(yīng)用的重要入口,所以這個(gè)頁(yè)面產(chǎn)品對(duì)于業(yè)務(wù)區(qū)塊的設(shè)計(jì)是多種多樣的,現(xiàn)在已經(jīng)差不多有將近20多種的樣式view組成后面有可能會(huì)繼續(xù)增加,而現(xiàn)在很多view的代碼實(shí)現(xiàn)是在Adapter實(shí)現(xiàn)的,造成的后果是Adapter的類相當(dāng)?shù)挠纺[,而且判斷分支多,增加一個(gè)類型對(duì)于原有的類就要進(jìn)行修改,不符合設(shè)計(jì)的開閉原則,而且view的復(fù)用性相對(duì)較差。在此背景下重新設(shè)計(jì)一個(gè)通用的適配。
需求和目標(biāo):
1.全站統(tǒng)一使用一個(gè)Adapter。
2.view和數(shù)據(jù)model構(gòu)建一個(gè)Holder單一的對(duì)象用于復(fù)用,脫離和Adapter的綁定
核心類設(shè)計(jì):
1.MultiTypeFactory是一個(gè)holde創(chuàng)建的工廠類,主要的功能是根據(jù)Modle類型的管理和holder類型管理,然后根據(jù)adapter傳回的Modle類型進(jìn)行已經(jīng)注冊(cè)的Holder的進(jìn)行生產(chǎn)不同類型的holder創(chuàng)建。通過
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的公共適配器設(shè)計(jì),可全站工程使用不用再費(fèi)勁的創(chuàng)建一個(gè)個(gè)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當(dāng)然也少不了recycleview的實(shí)現(xiàn),強(qiáng)烈建議全站使用recycleview去實(shí)現(xiàn)
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全局基礎(chǔ)holder,調(diào)用者只需關(guān)心數(shù)據(jù)模型和視圖的綁定添加了 model數(shù)據(jù)和view的注解實(shí)現(xiàn),可以讓工廠累獲取通過反射拿到不同類型holder的實(shí)例
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創(chuàng)建適配器接口
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事件回調(diào)接口給外部調(diào)用,暴露holde給調(diào)用者,可以在數(shù)據(jù)綁定的時(shí)候提供給修改功能增加擴(kuò)展性
public interface HolderAction {
void onBindAction(BaseViewHolder holder, int pos);
void onAfterAction(BaseViewHolder holder, int pos, int actionType);
}
- Type 所有Holder對(duì)應(yīng)的數(shù)據(jù)模型都實(shí)現(xiàn)這個(gè)類型接口
/**
* 適配器item基礎(chǔ)類型
* Created by hzzhudiqing on 2017/2/23.
*/
public interface Type {
}
- DataType<T> 數(shù)據(jù)模型的包裝類用于父子級(jí)數(shù)據(jù)關(guān)聯(lián),方便 type模型數(shù)據(jù)的構(gòu)造
/**
* 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;
}
}
使用方式
模型數(shù)據(jù)的創(chuàng)建
public class DeliveryGood extends DataType<Delivery> implements Type, Serializable {
.....
}
業(yè)務(wù)相關(guān)holder創(chuàng)建
@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的注冊(cè)調(diào)用
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);
效果圖: